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

如果您尝试使用运算符 viastd::cout << a或类似的东西,问题是您将指针传递给对象,而<<运算符被定义为引用对象。您要么需要声明a为常规(非指针)A,要么使用std::cout << *a.

于 2012-02-02T20:22:04.340 回答