-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
我認爲既然運營商是一個類的成員,它應該有Ø只有一個明確的參數。那麼爲什麼錯誤?
'bool FileDir :: operator ==(...)'。既然你是在類之外定義它,你需要告訴編譯器它是一個類成員。 –
您應該也可以創建'operator ==''const'。 – Brian