丿忆梦灬微殇 2020-06-27 11:50 采纳率: 0%
浏览 329
已采纳

C++的类模板如果把实现和定义分开就会报错,而放到一个文件里面就不会,是为什么呢?

定义

#include <iostream>
using namespace std;

#pragma once

template <typename T>
class MyVector
{
public:
    friend ostream & operator<< <T> (ostream &out, MyVector &obj);
public:
    MyVector(T size = 0);
    MyVector(const MyVector &obj);
    ~MyVector();
public:
    int getlen();
public:
    T& operator[](int my_index);
    MyVector& operator=(MyVector &obj);
private:
    int my_len;
    T   *my_space;
};

实现

#include "MyVector.h"
#include <iostream>
using namespace std;

template <typename T>
ostream & operator<< (ostream &out, MyVector<T> &obj)
{
    int i;

    for (i = 0; i < obj.getlen(); i++)
    {
        out << obj.my_space[i] << endl;
    }
    return out;
}

template <typename T>
MyVector<T>::MyVector(T my_size = 0)
{
    my_len = my_size;
    my_space = new T[my_size];
}

template <typename T>
MyVector<T>::MyVector(const MyVector &obj)
{
    int i;

    this->my_len = obj.my_len;
    this->my_space = new T[this->my_len];

    for (i = 0; i < this->my_len;i++)
    {
        my_space[i] = obj.my_space[i];
    }
}

template <typename T>
MyVector<T>::~MyVector()
{
    if (my_space != NULL)
    {
        delete[]my_space;
        my_space = NULL;
        my_len = 0;
    }
}

template <typename T>
int MyVector<T>::getlen()
{
    return my_len;
}

template <typename T>
T& MyVector<T>::operator[](int my_index)
{
    return my_space[my_index];
}

template <typename T>
MyVector<T>& MyVector<T>::operator=(MyVector<T> &obj)
{
    int i;

    if (my_space != NULL)
    {
    delete[] my_space;
    my_space = NULL;
    }

    this->my_space = new T[this->my_len];

    for (i = 0; i < this->my_len; i++)
    {
        this->my_space[i] = obj.my_space[i];
    }
    return *this;
}

主函数

#include <iostream>
#include "MyVector.h"

using namespace std;

int main()
{
    int             i;
    MyVector<int>   a(10);

    for (i = 0; i < a.getlen(); i++)
    {
        a[i] = i + 2;
    }

    cout << a << endl;

    MyVector<int>   b = a;

    for (i = 0; i < b.getlen(); i++)
    {
        b[i] += 2;
    }

    cout << b << endl;

    MyVector<int>   c(3);

    c = b;

    for (i = 0; i < c.getlen(); i++)
    {
        c[i] += 2;
    }

    cout << c << endl;

    cout << "hello world" << endl;

    system("pause");
    return 0;
}

错误信息
错误 5 error LNK2019: 无法解析的外部符号 "public: int __thiscall MyVector::getlen(void)" (?getlen@?$MyVector@H@@QAEHXZ),该符号在函数 _main 中被引用 G:\test\VS\cplusplus_up\day01\day01\MyVector_test.obj day01
错误 6 error LNK2019: 无法解析的外部符号 "public: int & __thiscall MyVector::operator" (??A?$MyVector@H@@QAEAAHH@Z),该符号在函数 _main 中被引用 G:\test\VS\cplusplus_up\day01\day01\MyVector_test.obj day01
错误 7 error LNK2019: 无法解析的外部符号 "public: class MyVector & __thiscall MyVector::operator=(class MyVector &)" (??4?$MyVector@H@@QAEAAV0@AAV0@@Z),该符号在函数 _main 中被引用 G:\test\VS\cplusplus_up\day01\day01\MyVector_test.obj day01
错误 2 error LNK2019: 无法解析的外部符号 "public: __thiscall MyVector::MyVector(int)" (??0?$MyVector@H@@QAE@H@Z),该符号在函数 _main 中被引用 G:\test\VS\cplusplus_up\day01\day01\MyVector_test.obj day01
错误 3 error LNK2019: 无法解析的外部符号 "public: __thiscall MyVector::MyVector(class MyVector const &)" (??0?$MyVector@H@@QAE@ABV0@@Z),该符号在函数 _main 中被引用 G:\test\VS\cplusplus_up\day01\day01\MyVector_test.obj day01
错误 4 error LNK2019: 无法解析的外部符号 "public: __thiscall MyVector::~MyVector(void)" (??1?$MyVector@H@@QAE@XZ),该符号在函数 _main 中被引用 G:\test\VS\cplusplus_up\day01\day01\MyVector_test.obj day01
错误 1 error LNK2019: 无法解析的外部符号 "class std::basic_ostream > & __cdecl std::<<(class std::basic_ostream > &,class MyVector &)" (?<<@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@1@AAV21@AAV?$MyVector@H@@@Z),该符号在函数 _main 中被引用 G:\test\VS\cplusplus_up\day01\day01\MyVector_test.obj day01
错误 8 error LNK1120: 7 个无法解析的外部命令 G:\test\VS\cplusplus_up\day01\Debug\day01.exe day01

如果将定义和实现放到同一个文件中就能正常运行,前面查资料有人说是主函数找函数体的时候找不到,是默认去系统函数库去找,但是要如何解决呢?

  • 写回答

1条回答 默认 最新

  • qq_45389553 2020-06-27 12:05
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题