簡體   English   中英

C ++錯誤:只能指定限定詞

[英]c++ error: qualifiers can only be specified

我不完全確定為什么會生成該錯誤:

const class MyString {
public:
MyString() { _len = 0; _str = NULL; }
MyString(const char* in);
MyString(const MyString&);
~MyString();

int set(const char*);
int set(const MyString&);

int setLength(int len) { _len = len; return 0; }
int getLength() { return _len; }

char * getStr() { return _str; }
int getStr(char* out) const;

MyString operator+(const MyString & in);
MyString operator+(const char* in);
MyString operator+(const char in) {const char* temp = ∈ return *this + temp; }

MyString operator=(const MyString & in)
    { this->set(in); return *this; }
MyString operator=(const char* in)
    { if(in) this->set(in); return *this; }
MyString operator=(const char in) {const char* temp = ∈ return *this = temp; }

MyString operator+=(const MyString & in)
    { this->set(*this + in); return *this; }
MyString operator+=(const char* in)
    { if(in) this->set(*this + in); return *this; }
MyString operator+=(const char in) { return (*this + in); }

int operator==(const MyString& in);
int operator!=(const MyString& in);
int operator==(const char* in);
int operator!=(const char* in);

friend ostream& operator<<(ostream& os, const MyString & in)
    { os << in._str; return os; }

protected:
char * _str;
int _len;
};

錯誤發生在最后一行。 該定義之前的唯一代碼是“標准” #includes,並使用命名空間std。

您發布的錯誤消息並不完整,但是請不要緊記。

長話短說:刪除類聲明頂部的const限定詞,即class關鍵字之前的那個。 您只能在變量或方法上添加cv限定詞( const / volatile )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM