gpt4 book ai didi

C++运算符<<类的成员

转载 作者:行者123 更新时间:2023-11-28 07:09:40 24 4
gpt4 key购买 nike

<分区>

我对 C++ 中的 << 运算符有疑问。我需要这个操作工作:

  cout << comp2 << comp1;


class Compo
{
string name;
int power;
string app;

public:
Compo(string s, string a, int p):name(s), power(p),app(a){};
//~Compo();

string GetAsString()const{
ostringstream oss;
oss << name << " [ " << power << " ] desc: " << app << endl;
return oss.str();
};


string &operator<<(Compo& aux)const{
return aux.GetAsString();
};
};

void main()
{
Compo comp1("Pencil","Best",12);
Compo comp2("Notes","Not Best",22);
cout << comp2 << comp1;
cin.get();
}

我在 cout << comp2 上有错误

错误描述是,

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Compo' (or there is no acceptable conversion)

和一个警告警报,

returning address of local variable or temporary

我需要改变这个作品。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com