Open Lantry28 opened 7 years ago
Person.h:
using namespace::std;
class Person{ public: Person() = default; Person(const string &s, const string &r) : names(s), adds(r){ } Person(istream& is){ read(is, *this); } string names; string adds;
const string& getName() const{ return names; }; const string& getAdds() const{ return adds; }; friend istream& read(istream& is, Person& person); friend ostream& print(const ostream& os, const Person& person);
};
istream& read(istream &is, Person& person){ is >> person.names >> person.adds; if (!is) person = Person(); //奇怪的if return is; }
ostream& print(ostream &os, const Person& person){ os << person.names << " " << person.adds << endl; return os; }
test:
int main(){ Person mine; print(cout, mine) << endl; system("pause"); return 0; }
error LNK2005: "class std::basic_ostream<char,struct std::char_traits > & __cdecl print(class std::basic_ostream<char,struct std::char_traits > &,class Person const &)" (?print@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV12@ABVPerson@@@Z) 已经在 Person.obj 中定义 D:\C++Test\Project7\Project7\test.obj Project7
error LNK2005: "class std::basic_istream<char,struct std::char_traits > & __cdecl read(class std::basic_istream<char,struct std::char_traits > &,class Person &)" (?read@@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV12@AAVPerson@@@Z) 已经在 Person.obj 中定义 D:\C++Test\Project7\Project7\test.obj Project7
error LNK1169: 找到一个或多个多重定义的符号 D:\C++Test\Project7\Debug\Project7.exe Project7
建议:
方便别人查阅&回答&学习.
Person.h:
include
include
using namespace::std;
class Person{ public: Person() = default; Person(const string &s, const string &r) : names(s), adds(r){ } Person(istream& is){ read(is, *this); } string names; string adds;
};
istream& read(istream &is, Person& person){ is >> person.names >> person.adds; if (!is) person = Person(); //奇怪的if return is; }
ostream& print(ostream &os, const Person& person){ os << person.names << " " << person.adds << endl; return os; }
test:
include "Person.h"
int main(){ Person mine; print(cout, mine) << endl; system("pause"); return 0; }
error LNK2005: "class std::basic_ostream<char,struct std::char_traits > & __cdecl print(class std::basic_ostream<char,struct std::char_traits > &,class Person const &)" (?print@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV12@ABVPerson@@@Z) 已经在 Person.obj 中定义 D:\C++Test\Project7\Project7\test.obj Project7
error LNK2005: "class std::basic_istream<char,struct std::char_traits > & __cdecl read(class std::basic_istream<char,struct std::char_traits > &,class Person &)" (?read@@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV12@AAVPerson@@@Z) 已经在 Person.obj 中定义 D:\C++Test\Project7\Project7\test.obj Project7
error LNK1169: 找到一个或多个多重定义的符号 D:\C++Test\Project7\Debug\Project7.exe Project7