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