2017-11-05 14 views
0

オーバーロードのオペレータの問題が発生しているようです。私は、エラーが何を言おうとしているのか本当に分からない。ここではエラーがあります:オーバーロードの問題C++

Error: no match for 'operator<<' in 
'std::cout << s1.Set::operator+(((const Set&)((const Set*)(& s2))))' 

ここに私のコードは次のとおりです。

#include "Set.cpp" 
int main(int argc, char *argv[]){ 
    Set s1(10),s2(6),s3(3),s4; 
    cout<<"First set ({x,y,z}): "; 
    cin>>s1;  
    cout<<"A: "<<s1<<endl; 
    cout<<"Second set: "; 
    cin>>s2; 
    cout<<"B: "<<s2<<endl; 

    cout<<s1+s2<<endl; 

} 

class Set { 
private: 
    bool elements[255]; 
    int capacity; //Yes, an unsigned char, short, or even size_t, would be better 
    Set(const bool elements[255], int capacity); //Helpful for immutable types 
public: 
    Set(); 
    Set(short capacity); 

    friend std::ostream& operator<<(std::ostream &out, Set &set); 
    friend std::istream& operator>>(std::istream &in, Set &set); 

    int getCapacity() const; //Cardinality of universe. i.e. |Universe| (or just 'capacity') 

}; 
Set::Set(const bool elements[255], int capacity){ 
    this->capacity = capacity; 
    for(int i=0; i<255;i++){ 
     if(elements[i] == true && i <= capacity){ 
      this->elements[i] = true; 
     }  
     else{ 
      this->elements[i] = false; 
     }   
    } 

} 
Set::Set(short capacity){ 
    this->capacity = capacity; 
} 
std::ostream& operator<<(std::ostream &out, Set &set) { 
    int capacity = set.getCapacity(); 
    out<<"{"; 
    for(int i=0; i < 255; i++){  
     if(set.elements[i] == true){ 
      out<<i<<","; 
     } 

    } 
    out<<"}"; 
    return out; 
} 
std::istream& operator>>(std::istream &in, Set &set) { 
    bool arr[255]; 
    int cap=set.getCapacity(); 
    char open; 
    in>>open; 
    if (in.fail() || open!='{') { 
     in.setstate(std::ios::failbit); 
     return in; 
    } 
    for (int i=0;i<cap;i++) 
     arr[i]=false; 
    std::string buff; 
    std::getline(in,buff,'}'); 
    std::stringstream ss(buff); 
    std::string field; 
    while (true) { 
     std::getline(ss,field,','); 
     if (ss.fail()) break; 
     int el; 
     std::stringstream se(field);   
     se>>el; 

     if (el>=0&&el<cap){ 
      arr[el]=true;   
     } 
     else{ 
      arr[el]=false; 
     } 
    } 
    set=Set(arr,cap); 
} 
Set Set::operator+(const Set &other) const{ 
    bool arr[255]; 

    for(int i=0; i<255;i++){ 
     if(this->elements[i] == true || other.elements[i]==true) 
      arr[i] == true; 
    } 

    int capacity = this->capacity>=other.capacity?this->capacity:other.capacity; 
    return Set(arr,capacity); 
} 

私は+と>>演算子の両方をオーバーロードします。コードを実行するとき、まずオーバーロードされた+演算子を実行してから、>>演算子を実行しません。

説明が必要です。あなたは非const参照としてのあなたの最後のパラメータを取っている

friend std::ostream& operator<<(std::ostream &out, Set &set); 

お知らせ:ありがとう

+0

'operator +'はどこですか? – Pixelchemist

+0

'operator +()'を使いたい場合は、それを宣言する必要があります。そうではありません。 – Peter

答えて

2

はここにあなたのオーバーロードされたストリーム挿入演算子の署名です。つまり、この関数は2番目のパラメータとしてlvaluesしか取ることができません。これは、あなたはこの1つを除いてリストアップしましたすべての場合の罰金です:

cout << s1+s2 << endl; 

私はあなたが上記のコードでは、あなたのoperator+関数のシグネチャが含まれて信じていませんが、私はそれ(適切に)と思われるだろう値でSetを返します。表現s1 + s2は左辺値に評価されないので、このコードは、

問題をトリガー
(operator<< (cout, s1 + s2)) << endl; 

に翻訳されます。この問題を解決するには

、あなたのoperator<<関数t

friend std::ostream& operator<<(std::ostream &out, const Set &set); 

の署名を変更するザ・あなたが安全にs1 + s2をプリントアウトすることができます一時、を含むものの最後のパラメータバインドを、することができますここでconstを追加しました。さらに、それは(正確に)Setを印刷する行為が実際にそのセットを変更しないことを発信者に示す。

他の.cppファイルの先頭に.cppファイルを含めるのは非常に奇妙です。おそらくSetタイプのヘッダを定義し、それを含めるべきです。それ以外の場合、複数のファイルにSet.cppファイルを含めると、各関数の複数の定義のためにリンカーエラーが発生します。

+0

あなたはお尻になることを想定していませんでした。ありがとうございました!それは問題を修正し、+私が含めることを望んでいなかったと思う+操作の上書きを含めるように私の質問を更新しました。 –