2013-01-19 39 views
3

我正在写一个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 
+3

继承自'std :: vector'是一个非常糟糕的主意。另外,你从来没有定义你的ctor/dtor。 – chris

+0

我应该只是做功能,或者你有另一个想法? –

+1

标准容器的组成通常会更好。 – chris

回答

4

你必须在全局范围内超负荷运营商:

vector<double> operator*(const vector<double>& v, double alfa) 
{ 
    ... 
} 

vector<double> operator+(const vector<double>& v1, const vector<double>& v2) 
{ 
    ... 
} 

vector<double> operator-(const vector<double>& v1, const vector<double>& v2) 
{ 
    ... 
} 

对于连接错误,它只是看起来像你没有实现线路构造函数和析构函数。

+0

非常感谢你,解决了我所有的问题* _ *。 –

+0

在我的不请自来的意见中,重载操作符并不是一个好主意,它只接受那些不属于你自己的类型的参数,特别是在全局范围内,其他一些愚蠢的类库可能会做同样的事情。 –

1

链接器错误告诉你,你的代码是你定义了两个成员函数缺少定义 - 构造函数和析构函数:

Line::Line() { 
    // Code of the constructor goes here 
} 

Line::~Line() { 
    // Code of the destructor goes here 
} 
1

你永远不应该继承std - 不适用于继承的类。继承不具有虚拟析构函数的类是非常危险的。

我建议你使用聚合:使你的Line类包含vector类型的成员,例如名为myVector_,并以他们使用此成员变量的方式实现所需的运算符。

所以你更换到size()所有呼叫myVector.size()等:

Line Line::operator*(double alfa) 
{ 
    Vector temp; 
    int n = myVector_.size(); 
    temp.resize(n); 
    for (int i = 0; i < n; i++) 
    { 
     temp.at(i) = myVector_.at(i)*alfa; 
    } 
    return temp; 
} 
+0

我已经试过,但主要问题是[]运算符我无法让它返回指针。 例如: A [i] [j] =东西.... 没有工作, 我将不得不输入类似于: A [i] .v.at(j)= something ... 使其工作。 ty for ctor/dtor我不知道这件事。 –

+0

你究竟是什么意思? 'return myVector_ [i];'如果函数的返回类型正确,应该没有问题 –

+0

或者你的意思是你想访问内部数组?只要使用'&myVector_ [0]'或'myVector_.data()'如果你使用C++ 11 –

0

肯定正确的事情是有一个Vector对象内线,并从向量不“继承”?通常从std::容器继承不是一个很好的数据......我很确定一个“线”实际上不是一个矢量,它是一个“有”矢量。 [“继承时”的规则是“X是Y”,在“X有Y”时制作复合对象 - 因此X内有Y.]

您需要声明您的构造函数和析构函数来摆脱链接错误。

我也会使用const Line&作为数学运算的输入,因为您要改变输入。

相关问题