0

だから私はこのコードを自分で書いていますが、他のサンプルコードから取っています...

class A{
    friend std::ostream& operator<< (std::ostream& out, A& a);
    // Constructors, destructor, and variables have been declared
    // and initialized and all good.
}

std::ostream& operator<< (std::ostream& out, A& a){
    out << " this gets written " << endl; // it doesn't get executed
    return out;
}

int main(){
    A *_a = new A();
    return 0;
}

まあ、これはコンソールに印刷されていないだけです" this gets written "

4

1 に答える 1

1

または同様のものを介して演算子を使用しようとしている場合、オブジェクトへの参照を取得するように演算子が定義されているのに、オブジェクトへのポインターstd::cout << aを渡していることが問題です。通常の (非ポインター) として宣言するか、 を使用する必要があります。<<aAstd::cout << *a

于 2012-02-02T20:22:04.340 に答える