error C2593: 'operator <<' is ambiguous
#include <iostream>
using namespace std;
class Test
{
public:
Test(int age = 0,char *name = "\0")
{
Test::age = age;
strcpy(Test::name,name);
}
void outmembers(ostream &out)
{
out<<"Age:"<<age<<endl<<"Name:"<<this->name<<endl;
}
friend ostream& operator <<(ostream& ,Test&);
protected:
int age;
char name[50];
};
ostream& operator <<(ostream& out,Test &temp)
{
temp.outmembers(out);
return out;
}
int main()
{
Test a(24,"管宁");
cout<<a;
system("pause");
return 0;
}
会出现
error C2593: 'operator <<' is ambiguous
Error executing cl.exe.
date22_1.obj - 1 error(s), 0 warning(s)
错误,是编译器的问题吗?
[解决办法]
#include <iostream> using std::ostream; using std::cout;using std::endl; class Test{ public: Test(int age = 0,char *name = "\0") { Test::age = age; strcpy(Test::name,name); } void outmembers(ostream &out) { out<<"Age:"<<age<<endl<<"Name:"<<this->name<<endl; } friend ostream& operator <<(ostream& ,Test&); protected: int age; char name[50]; }; ostream& operator <<(ostream& out,Test &temp) { temp.outmembers(out); return out;} int main() { Test a(24,"管宁"); cout<<a; system("pause"); return 0;}