2011-07-30 8 views
0

演算子の定義方法を見てみるのは面倒です。次のコードは、「候補関数が見つかりません」というエラーを表示しています。私自身の演算子の定義に問題があります...

根本原因以外の批判も歓迎です。ありがとう!

#include <iostream> 

using std::cout; using std::cin; using std::endl; 

using std::string; 

class SomeClass { 
public: 
    SomeClass(int newNum, string newString) { num=newNum; str = newString; } 

    SomeClass& operator=(const SomeClass& rh) { 
     string newVal(rh.getStr()); 
     str = newVal; 
    } 

    void setStr(string newString) { str = newString; } 
    const string getStr() { return str; } 
    string toString() { return str+str; } 

private: 
    string str; 
    int num; 
}; 

int main() { 
    SomeClass a(5, "five"); 
    SomeClass b(3, "three"); 

    cout << a.toString() << endl << b.toString() << endl; 

    a=b; 

    cout << a.toString() << endl << b.toString() << endl; 
} 
+0

ここでどのようなエラーが表示されますか? –

+0

次回は、もしあなたが私たちに骨を投げて、少なくともそれが何であるか教えてくれればいいと思います。また、正確なエラーメッセージのコピーも持っているといいです。 –

答えて

2
const string getStr() { return str; } 

const string& getStr() const { return str; } 

する必要がありますそうしないと、プライベート、パブリックおよび保護された可視性がクラスレベルであることを

SomeClass& operator=(const SomeClass& rh) 
+0

これを明確にする:FIRST 'const'は、関数がconst文字列を返すことを意味し、SECOND 'const'は関数自体がconstであることを意味します。 (その場合、const関数の意味は分かりません) – loneboat

+0

第2の 'const'は' this'がconstオブジェクトを指していることを意味します。つまり、関数を呼び出すインスタンスのメンバを変更することはできないので、constインスタンスに対して関数を呼び出すことができます。 – ybungalobill

0

すべてはあなたがあなたにもこれを含める必要が

#include <string> 

を含めていませんでしたことを除いて、これまでに正常に見えます。

ああ、私はまた、あなたが関数から何かを返していないことを見た:

SomeClass& operator=(const SomeClass& rh) { 
     string newVal(rh.getStr()); 
     str = newVal; 
     return *this; //DO THIS AS WELL 
    } 

そしてまた、rhはこの機能ではconstオブジェクトであり、それを使用して、あなたが非でgetStr()を呼んでいます問題を引き起こしている-const関数ので、修正はこれです:

SomeClass& operator=(const SomeClass& rh) { 
     str = rh.str; //no need to create a local (temporary) variable! 
     return *this; //DO THIS AS WELL 
    } 

私はそのよりよい解決策を考える:

const string getStr() const { return str; } 
//     ^^^^^ make the function const! 

そして、代わりに次のように、あなたはあなたのoperator=を書いたことができます!

2

ノートのconstのパラメータに非const関数を呼び出すことはできません、インスタンスレベルではありません。したがって、getStr()機能は必要ありません。あなたは書くことができます:

SomeClass& operator=(const SomeClass& rh) { 
    this->str = rh.str; 
    return *this; 
} 
+0

興味深い - インスタンスAは同じクラスのインスタンスBのすべてのメンバーにアクセスできますか? – loneboat

0

をあなただけのconstオブジェクトが、constメンバ関数を呼び出すことができるだけで、あなたがこれを行うにしようとしていたので、これは

const string getStr() { return str; } 

const string getStr() const { return str; } //the 2nd const make getStr() a const member function 
         ^^^^ 

に変更する必要があります。

SomeClass& operator=(const SomeClass& rh) { //here you declared rh to be const 
     //so in order to call getStr() from rh, you need declare getStr function to be const 
     string newVal(rh.getStr()); 
     str = newVal; 
     return *this; 
    } 
0

あなたのクラスは単純に次のようになります:

class SomeClass { 
public: 
    SomeClass(int newNum, string newString):num(newNum), str(newString) {/* Empty */} 
    SomeClass& operator=(const SomeClass& rh) { 
    str=rh.str; 
    return *this; 
    } 
    string toString() { return str+str; } 
private: 
    string str; 
    int num; 
}; 
関連する問題