2012-05-02 14 views
0

達人、はオペレーターに結合することはできません<< CPPで

は、次のように定義された2つのクラス(属性、メソッド、および実装は省略)を考える:私は次のようにクラスを使用

struct A { friend std::ostream& operator << (std::ostream& o, const A& c); }; 
struct B { friend std::ostream& operator << (std::ostream& o, const B& c); }; 

を:

ln 1: A *arrayA = new A[10]; 
ln 2: B *arrayB = new B[10]; 
ln 3: /* some codes to initialize arrayA and arrayB */ 
ln 4: for (int i = 0; i < 10; i++) { std::cout << arrayA[i]; } // this work 
ln 5: for (int j = 0; j < 10; j++) { std::cout << arrayB[j]; } // this complain 

私のコンパイラは

error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue 
to 'std::basic_ostream<char>&&' 

../lib/gcc/mingw32/4.6.1/include/c++/ostream:581:5 error initializing argument 1 
of 'std::basic_ostream<_CharT, _Traits>& 
std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) 
[with _CharT = char, _Traits = std::char_traits<char>, _Tp = ClsB] 
ように、クラスBのために文句を言い

私は5行目で何が問題なのか分かりません。メインプログラムの5行目をコメントアウトすると、私の良いコンパイルが得られます。つまり、オペレータの定義は、クラスBの< <です。どんな指示や感謝もお願いします。 7

  • MINGW32に勝つ:ヤム本町は

    • プラットフォーム

      バージョン2011-11-08

    • GNU 3.82
    • G ++バージョン4.6.1

    を作る[編集1 ] 私のプログラムは実際には2つ以上のクラスを持ち、すべてのクラスに演算子があります< <がオーバーロードされていますデバッグ用。私はすべてのクラスに対して同じシグネチャ(適切な第2引数を使用)を使用しました。クラスBのみがこのエラーを出します。

    [編集2]私のクラスの フルバージョン:

    struct CPeople { // this is class B 
    int age; 
    int ageGroup; 
    
    int zipcode; 
    int communityID; 
    int areaID; 
    int familyID; 
    int contactID; 
    int contactType; /* P, D, E, M, H, W */ 
    int state; 
    int vaccinated; /* 0 = unvac, 1 = vaccinated */ 
    
    friend std::ostream& operator<< (std::ostream& o, const CPeople& c) 
    { 
        o << "CPeople (" << static_cast<void const *>(&c) << "): " 
         << "\tAge Group: " << c.ageGroup 
         << "\tZip Code: " << c.zipcode 
         << "\tCommunityID: " << c.communityID 
         << "\tArea ID: "  << c.areaID 
         << "\tFamily ID: " << c.familyID 
         << "\tSchool Type: " << c.contactType 
         << "\tContact ID: " << c.contactID 
         << "\tState: "  << c.state 
         << "\tVaccination: " << c.vaccinated; 
        return (o << std::endl); 
    } 
    }; 
    
    struct CWorkGroup : public CContact { // this is class A 
    /* to which community this member belongs */ 
    std::vector<long> member_com; 
    CStatistics statistics; 
    
    friend std::ostream& operator<< (std::ostream& o, const CWorkGroup& c) 
    { 
        o << "CWorkGroup (" << static_cast<void const *>(&c) << "): "; 
        o << "avflag = " << c.avflag << "; member: " << c.size(); 
        for (int i = 0; i < c.size(); i++) 
        { 
         o << "; (" << i << " = " << c.member[i] << ")"; 
        } 
        o << std::endl; 
        return (o << c.statistics); 
    } 
    }; 
    

    使用法A:

    ​​3210

    使用法B(これはエラーである):

    CPeople *people_total = new CPeople[cntTotalPop]; 
    for (pIdx = 0; pIdx < cntTotalPop; pIdx++) 
    { 
        std::cout << people_total[pIdx]; 
    } 
    
  • +0

    'operator <<'(Bの)実装のための関数ヘッダーはどのように見えますか? – chris

    +0

    授業の最後に ';'を忘れましたか?また、3行目の関連コードを投稿することもできます。 – iammilind

    +0

    実際にすべての演算子<<は友人として定義されており、クラス定義内に実装されています –

    答えて

    1

    クラスと構造体セミコロンで終了する必要があるので、セミコロンを両方の行の最後に追加してください:

    struct A { friend std::ostream& operator << (std::ostream& o, const A& c); }; 
    struct B { friend std::ostream& operator << (std::ostream& o, const B& c); }; 
    
    +0

    コメントありがとうございました –

    +0

    @ YamHon.CHAN、あなたの問題を解決しましたか?そうでない場合は、同じエラーが発生していますか? – chris

    +0

    紛失したカンマは単に入力ミスです。それを指摘してくれてありがとう。同じエラーがまだ発生します –

    関連する問題