我是 C++ 新手。
所以我正在创建一个程序,它收集然后显示有关书籍等的信息。首先,我想了解更多关于我正在做的代码的信息,但我不熟悉 c++ 错误代码。
我创建了 2 个类 Book 和 Publisher,每个类都包含自己的构造函数和方法。在我尝试让我的程序包括我在类图中给出的所有类和方法之后,当我收到错误消息时我停止了:
1>Publisher.obj : error LNK2005: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl getPublisherInfo(void)" (?getPublisherInfo@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in Book.obj
1>Publisher.obj : error LNK2005: "void __cdecl setAddress(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setAddress@@YAXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Book.obj
1>Publisher.obj : error LNK2005: "void __cdecl setCity(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setCity@@YAXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Book.obj
1>Publisher.obj : error LNK2005: "void __cdecl setName(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setName@@YAXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Book.obj
1>C:\Users\pc\Desktop\School\ITS 340\Labs\Lab 1\Lab 1\Debug\Lab 1.exe : fatal error LNK1169: one or more multiply defined symbols found
这是我的 Publisher.cpp 文件:
#include <iostream>
using namespace std;
class Publisher
{
public:
Publisher();
Publisher(string name, string address, string city);
string getPublisherInfo();
void setName(string name);
void setAddress(string address);
void setCity(string city);
private:
string name;
string address;
string city;
};
Publisher::Publisher()
{
}
Publisher::Publisher(string name, string address, string city)
{
}
string getPublisherInfo()
{
return 0;
}
void setName(string name)
{
}
void setAddress(string address)
{
}
void setCity(string city)
{
}
我怎样才能避免这个错误?