我知道这个问题可能很愚蠢,但我是 C++ 新手,我在重新定义操作数时遇到了很多问题。我想要做的是重新定义操作数 << 以打印我的类“Person”信息,但它出现编译错误:
class Person {
private:
string name;
unsigned long personId;
string address;
public:
Person(string name2,unsigned long id,string adr) {
name = name2;
personId = id;
address = adr;
}
void operator<<(Person person) {
cout<<"Name: "<<person.name<<" Person ID: "<<person.personId<<" Person address: "<<person.address<<endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
Person person("Steven",1212121212,"USA");
cout<<person; //compiling error here
return 0;
}
这样做的正确方法是什么?