我正在写一个Line类来创建数值方法,我希望这些运算符(*,+, - ) 能使我的代码更易读易懂。重载*,+,-'operators for vector <double>类
#include <vector>
using namespace std;
typedef vector<double> Vector;
class Line : public Vector
{
public:
Line();
~Line();
Line operator+(Line);
Line operator-(Line);
Line operator*(double);
};
Line Line::operator*(double alfa)
{
Line temp;
int n = size();
temp.resize(n);
for (int i = 0; i < n; i++)
{
temp.at(i) = this->at(i)*alfa;
}
return temp;
}
Line Line::operator+(Line line)
{
int n = size();
Line temp;
temp.resize(n);
for (int i = 0; i < n; i++)
{
temp.at(i) = this->at(i) + line[i];
}
return temp;
}
Line Line::operator-(Line line)
{
int n = size();
Line temp;
temp.resize(n);
for (int i = 0; i < n; i++)
{
temp.at(i) = this->at(i) - line[i];
}
return temp;
}
int main()
{
return 0;
}
是否有可能从Vector类中重载这些运算符?我应该只是做功能(或方法),而不是操作员?任何其他建议?
ps1:我使用Visual Studio 11作为编译器。
ps2:我还没有启动项目作为'win32项目',它是控制台应用程序。
我歌厅以下错误:
Error 1 error LNK2019: unresolved external symbol "public: __thiscall Line::Line(void)" ([email protected]@[email protected]) referenced in function "public: class Line __thiscall Line::operator*(double)" ([email protected]@[email protected]@Z) C:\Users\Lucas\Documents\Visual Studio 11\Projects\test\test\test.obj test
Error 2 error LNK2019: unresolved external symbol "public: __thiscall Line::~Line(void)" ([email protected]@[email protected]) referenced in function "public: class Line __thiscall Line::operator*(double)" ([email protected]@[email protected]@Z) C:\Users\Lucas\Documents\Visual Studio 11\Projects\test\test\test.obj test
继承自'std :: vector'是一个非常糟糕的主意。另外,你从来没有定义你的ctor/dtor。 – chris
我应该只是做功能,或者你有另一个想法? –
标准容器的组成通常会更好。 – chris