2016-01-13 81 views
0

我有一個自定義類,並且正在嘗試爲=創建運算符重載函數。不幸的是,我得到的錯誤沒有指向特定的行或錯誤,我只是得到以下錯誤。1嘗試超載時出現無法解析的外部錯誤=運算符

解析外部符號 「公共:整數__thiscall序列::大小(無效)常量」 在函數引用「公共(大小@ @@序列QBEHXZ?):無效__thiscall序列::運算=(類序列常量& )」(?? 4sequence @@ QAEXABV0 @@ Z)

它是在文件Sequence2.obj線1.這不是我編輯文件時,我有點不確定的錯誤是在什麼樣的功能。

Sequence2.cpp

void sequence::operator=(const sequence & source) 
{ 
    if (size() <= source.size()) { 

     delete[] data; 

     data = new value_type[source.size()]; 
     current_index = -1; 
     used = 0; 
    } 
    else { 

     delete[] data; 

     data = new value_type[source.size() * 2]; 
     current_index = -1; 
     used = 0; 
    } 

    for (int i = 0; i < source.size(); i++) 
    { 
     data[i] = source.data[i]; 
     used++; 
     current_index++; 
    } 
} 

大小功能,只需返回序列的大小。在Main.cpp上,我只有以下幾點。

sequence test; // A sequence that we’ll perform tests on 
sequence test2; 

test.insert(33); 
test.insert(35); 
test.insert(36); 

test2 = test; 
+9

該錯誤與'operator ='本身無關。編譯器(鏈接器)告訴你,你忘了定義'size()'函數。 'size()'的定義在哪裏? – AnT

回答

0

在編譯的鏈接步驟中出現「未解決的外部」錯誤。有關更多詳細信息,請參閱this question。請注意,在編譯步驟中,編譯器從Sequence2.cpp製作了Sequence2.obj(並將其包括在內)。

是的,鏈接器錯誤比實際編譯源代碼時發生的那些編譯器錯誤有點棘手。

我想這地方在你的代碼,你有類似

class sequence 
{ 
    // ... some declarations 

    int size(); 

    // ... more declarations 
} 

,但沒有相應的

int sequence::size() 
{ 
    // implementation of size() 
} 

或者出現的size()的實現,但它不會被編譯。在這種情況下檢查你的項目設置/ makefile。或者它已經被編譯,但結果'.obj'文件沒有被鏈接器使用。

錯誤消息至少聲稱鏈接程序不知道.obj文件,該文件包含size()實現的已翻譯(即已編譯)對應文件。

相關問題