2013-01-19 42 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&作爲數學運算的輸入,因爲您要改變輸入。

相關問題