C ++ [运算符<<过载困难] [类+模板交互错误] LNK1120和LNK2019错误

时间:2015-11-13 02:09:36

标签: c++ class oop templates lnk2019

我一直在学习一些标准的OOP,比如使用类等等。而且我遇到了麻烦,代码似乎没有被误解,但是编译器一直在告诉你一些糟糕的错误。过了一会儿,我决定在那里问一个问题。我之前做了一些“研究”并试图自己解决,但我的所有尝试都没有成功。当我尝试使用模板来练习构建适当的类时出现问题(当我使用普通类型如int,float等时,一切正常)。代码看起来很漂亮,IDE没有看到任何错误,但是当编译开始时会出现一些错误,说有没有解析的外部符号。

代码:main()

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

void main()
{
Rectangle<int> b(0, 0, 0, 5, 10, 5, 10, 0);
cout << "Your rectangle: \n" << b << endl;
cout << "It's perimeter: " << b.perimeter() << " cm" << endl;
cout << "Area of your rectangle = " << b.area() << " cm^2" << endl;
system("pause");
}

代码:Rectangle.h

#include <iostream>
using namespace std;
template <typename R> class Rectangle
{
public:
   Rectangle(R, R, R, R, R, R, R, R);
   ~Rectangle();
   float area();
   float perimeter();
   friend ostream& operator<<(ostream&, Rectangle&);
private:
   R vect[4][2];
   float length(R,R,R,R);
};

代码:Rectangle.cpp

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

template <typename R> Rectangle<R>::Rectangle(R x1,R y1,R x2,R y2,R x3,R y3,R x4,R y4)
{
vect[0][0] = x1;
vect[0][1] = y1;
vect[1][0] = x2;
vect[1][1] = y2;
vect[2][0] = x3;
vect[2][1] = y3;
vect[3][0] = x4;
vect[3][1] = y4;
}

template <typename R> Rectangle<R>::~Rectangle()
{
cout << "The rectangle object has been deleted." << endl;
}

template <typename R> float Rectangle<R>::length(R a1,R b1,R a2,R b2)
{
float len;
len = (a2 - a1)*(a2 - a1) + (b2 - b1)*(b2 - b1);
len = sqrt(len);
return len;
}

template <typename R> float Rectangle<R>::area()
{
float Area;
Area = length(vect[0][0], vect[0][1], vect[1][0], vect[1][1]);
Area *= length(vect[0][0], vect[0][1], vect[3][0], vect[3][1]);
return Area;
}

template <typename R> float Rectangle<R>::perimeter()
{
float Perimeter;
Perimeter = 2*length(vect[0][0], vect[0][1], vect[1][0], vect[1][1]);
Perimeter += 2*length(vect[0][0], vect[0][1], vect[3][0], vect[3][1]);
return Perimeter;
}

// Problem remains with this operator overload (when functions are moved to Rectangle.h)
template <typename R> ostream &operator<<(ostream &output, Rectangle <R> &p)
{
cout << "(" << p.vect[0][0] << "," << p.vect[0][1] << ")" << endl;
cout << "(" << p.vect[1][0] << "," << p.vect[1][1] << ")" << endl;
cout << "(" << p.vect[2][0] << "," << p.vect[2][1] << ")" << endl;
cout << "(" << p.vect[3][0] << "," << p.vect[3][1] << ")" << endl;
return output;
}

错误列表:

Error   1   error LNK2019: unresolved external symbol "public: __thiscall Rectangle<int>::Rectangle<int>(int,int,int,int,int,int,int,int)" (??0?$Rectangle@H@@QAE@HHHHHHHH@Z) referenced in function _main  C:\Users\Alex\Desktop\C++ Projects\OOP_Test_1\OOP_Test_1\Main.obj   OOP_Test_1
Error   2   error LNK2019: unresolved external symbol "public: __thiscall Rectangle<int>::~Rectangle<int>(void)" (??1?$Rectangle@H@@QAE@XZ) referenced in function _main    C:\Users\Alex\Desktop\C++ Projects\OOP_Test_1\OOP_Test_1\Main.obj   OOP_Test_1
Error   3   error LNK2019: unresolved external symbol "public: float __thiscall Rectangle<int>::area(void)" (?area@?$Rectangle@H@@QAEMXZ) referenced in function _main  C:\Users\Alex\Desktop\C++ Projects\OOP_Test_1\OOP_Test_1\Main.obj   OOP_Test_1
Error   4   error LNK2019: unresolved external symbol "public: float __thiscall Rectangle<int>::perimeter(void)" (?perimeter@?$Rectangle@H@@QAEMXZ) referenced in function _main    C:\Users\Alex\Desktop\C++ Projects\OOP_Test_1\OOP_Test_1\Main.obj   OOP_Test_1
Error   5   error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Rectangle<int> &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAV?$Rectangle@H@@@Z) referenced in function _main  C:\Users\Alex\Desktop\C++ Projects\OOP_Test_1\OOP_Test_1\Main.obj   OOP_Test_1
Error   6   error LNK1120: 5 unresolved externals   C:\Users\Alex\Desktop\C++ Projects\OOP_Test_1\Debug\OOP_Test_1.exe  OOP_Test_1

我发现将我的函数从单独的 class.cpp 文件移动到 class.h ,其中声明了它们的原型,有帮助,但并非总是如此。输出操作符重载仍然不起作用,即使使用 class.h中的函数也会出现同样的错误(但只有1)。

错误列表(将函数移动到头文件后):

Error   2   error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Rectangle<int> &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAV?$Rectangle@H@@@Z) referenced in function _main  C:\Users\Alex\Desktop\C++ Projects\OOP_Test_1\OOP_Test_1\Main.obj   OOP_Test_1
Error   3   error LNK1120: 1 unresolved externals   C:\Users\Alex\Desktop\C++ Projects\OOP_Test_1\Debug\OOP_Test_1.exe  1   1   OOP_Test_1

UPD: 我明白了,class.cpp中的模板实现及其在class.h中的定义有什么问题。但我还是不知道,如何处理运算符过载。我现在正在阅读一些C ++参考资料,不过我会很高兴得到一些提示。 我会感激任何帮助。谢谢。

0 个答案:

没有答案