2015-04-06 45 views
-2

我有一個==操作符類成員在這個頭文件中的FILEDIR類:錯誤:布爾運算符==必須正好有兩個參數

#include <sstream> 

class FileDir { 

public: 

    FileDir(std::string nameVal, long sizeVal = 4, bool typeVal = false); 

    FileDir(const FileDir &obj); 

    ~FileDir();   // destructor 

    long getSize() const;  

    std::string getName() const; 

    bool isFile() const; 

    std::string rename(std::string newname);  

    long resize(long newsize);  

    std::string toString();  

    bool operator== (const FileDir &dir1);   

private: 

    std::string name; 

    long size; 

    bool type; 

}; 

而這正是實現:

bool operator== (const FileDir &dir1) { 

    if (this->name == dir1.name && this->size == dir1.size && this->type == dir1.type) 

     return true; 

    else 

     return false; 

} 

這是我從編譯器得到的錯誤:

FileDir.cpp:101:37: error: ‘bool operator==(const FileDir&)’ must take exactly two arguments 
bool operator== (const FileDir &dir1) { 
            ^
make: *** [fdTest] Error 1 

我認爲既然運營商是一個類的成員,它應該有Ø只有一個明確的參數。那麼爲什麼錯誤?

+3

'bool FileDir :: operator ==(...)'。既然你是在類之外定義它,你需要告訴編譯器它是一個類成員。 –

+0

您應該也可以創建'operator ==''const'。 – Brian

回答

4

就像任何成員函數一樣,當你在函數體外定義它時,你需要在函數的名字前加上Class::。但在這種情況下,該函數的名稱是operator==。您需要:

bool FileDir::operator== (const FileDir &dir1) { 
    //... 
} 
0

嘗試:

bool FileDir::operator== (const FileDir &dir1) const{ 
    if (this->name == dir1.name && 
     this->size == dir1.size && 
     this->type == dir1.type) 
    return true; 
else 
    return false;   
} 

OR

攜帶類定義您的實現。