我想重載賦值運算符作爲成員函數將字符串作爲參數並將它的值分配給當前對象A
。我在下面的評論中發佈了錯誤。如何重載運算符=
有人能告訴我我做錯了什麼嗎?我認爲這與參數有關,可能是定義中的代碼。
,我不知道我是否正確聲明,但我宣佈它是這樣的:
WORD operator=(const string& other);
我定義它是這樣的:
WORD WORD::operator=(const string& other) //<---not sure if I did the parameters Correctly
{
(*this) = other;
return (*this);
}
這裏是整個文件,如果有幫助:
#include <iostream>
using namespace std;
#pragma once
class alpha_numeric //node
{
public:
char symbol; //data in node
alpha_numeric *next;//points to next node
};
class WORD
{
public:
WORD() : front(0) {length=0;}; //front of list initially set to Null
WORD(const WORD& other);
bool IsEmpty();
int Length();
void Insert(WORD bword, int position);
WORD operator=(const string& other);
private:
alpha_numeric *front; //points to the front node of a list
int length;
};
WORD WORD::operator=(const string& other) //<---not sure if I did the parameters Correctly
{
(*this) = other;
return (*this);
}
請張貼「無法讀取的混亂」。 –
錯誤錯誤LNK2005:「public:class WORD __thiscall WORD :: operator =(class std :: basic_string,class std :: allocator > const&)」(?? 4WORD @@ QAE?AV0 @ ABV?$ basic_string @ DU?$ char_traits @ D @ std @@ V?$ allocator @ D @ 2 @@ std @@@ Z)已經在word.obj中定義了\t C:\ Users \ Mike \ Desktop \ School Programs \ Data Structures Assignment 2 \ Data Structures Assignment 2 \ word_driver.obj –
Mike
Error error LNK2019:無法解析的外部符號「public:__thiscall WORD :: WORD(class WORD const&)」(?? 0WORD @ (class @ std :: char_traits,class std :: allocator > const&)「((@ QAE @ ABV0 @@ Z)在函數」public:class WORD __thiscall WORD :: operator =(class std :: basic_string
Mike