[英]Compilation Error in overloading operator
無法理解為什么會出錯
錯誤:'operator<<' 不匹配(操作數類型是 'std::ostream' {aka 'std::basic_ostream'} 和 'MyStruct') cout << st;
在下面的代碼中
#include <iostream>
using namespace std;
struct MyStruct
{
int a;
string b;
double c;
MyStruct(int a, string b, double c):a(a),b(b),c(c){}
ostream& operator<<(ostream& os)
{
os << "a " << a << " b" << b << " c" << c;
return os;
}
};
template <typename T, typename ... Args>
T create(Args&& ... Arg)
{
return T(Arg...);
}
int main() {
// your code goes here
MyStruct st = create<MyStruct>(5, "My Struct", 2.5);
cout << st;
return 0;
}
操作員的正確簽名是:
friend ostream& operator<<(ostream& os, const MyStruct& dt);
現在,您的 output stream 運算符僅以允許st << cout
的方式重載。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.