对象已经定义但我不知道在哪里

Hel*_*der 3 c++ visual-studio fatal-error

我是C++的新手,错误代码对我来说并不是很吸引人.

我正在尝试构建一个简单的C++ OOP程序作为家庭作业,但会收集然后显示有关书等的信息.

我收到一个错误:

1>Book.obj : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Author::getFullName(void)" (?getFullName@Author@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in Author.obj
1>Book.obj : error LNK2005: "public: void __thiscall Author::setFirstName(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setFirstName@Author@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Author.obj
1>Book.obj : error LNK2005: "public: void __thiscall Author::setLastName(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setLastName@Author@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Author.obj
1>Publisher.obj : error LNK2005: "public: __thiscall Publisher::Publisher(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0Publisher@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@00@Z) already defined in Book.obj
1>Publisher.obj : error LNK2005: "public: __thiscall Publisher::Publisher(void)" (??0Publisher@@QAE@XZ) already defined in Book.obj
1>Publisher.obj : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Publisher::getPublisherInfo(void)" (?getPublisherInfo@Publisher@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in Book.obj
1>Publisher.obj : error LNK2005: "public: void __thiscall Publisher::setAddress(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setAddress@Publisher@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Book.obj
1>Publisher.obj : error LNK2005: "public: void __thiscall Publisher::setCity(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setCity@Publisher@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Book.obj
1>Publisher.obj : error LNK2005: "public: void __thiscall Publisher::setName(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setName@Publisher@@QAEXV?$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
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么吗?

Book.cpp

#include <iostream>
using namespace std;

#include "Author.cpp"
#include "Publisher.cpp"

class Book
{
    public:
        Book();
        Book(string title, Author *pAuthor, Publisher *pPublisher, double price);
        ~Book();
        void setTitle(string title);
        void setAuthorName(string first, string last);
        void setPublisher(string name, string address, string city);
        void setPrice(double price);
        string convertDoubleToString(double number);
        string getBookInfo();

    private:
        string title;
        double price;
        Author *pAuthor;
        Publisher *pPublisher;
};

Book::Book()
{
}

Book::Book(string title, Author *pAuthor, Publisher *pPublisher, double price)
{
    title = title;
    price = price;
}

Book::~Book()
{
}

void Book::setTitle(string  title)
{
}

void Book::setAuthorName(string first, string last)
{
}

void Book::setPublisher(string name, string address, string city)
{
}

void Book::setPrice(double price)
{
}

string Book::convertDoubleToString(double number)
{
    return 0;
}

string Book::getBookInfo()
{
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

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)
{
    name = name;
    address = address;
    city = city;
}

string Publisher::getPublisherInfo()
{
    return "0";
}

void Publisher::setName(string name)
{
    name = name;
}

void Publisher::setAddress(string address)
{
    address = address;
}

void Publisher::setCity(string city)
{
    city = city;
}
Run Code Online (Sandbox Code Playgroud)

das*_*ght 9

您永远不应该cpp在另一个cpp文件或标题中包含文件.这几乎不可避免地导致重复的对象定义.

声明放入头文件,并将定义放入cpp文件中.在cpp文件中包含标头,您需要访问其他cpp文件中定义的对象方法.

Publisher.cpp一个例子为例:包含该};行的顶部部分需要进入该Publisher.h文件.其他一切都必须留在Publisher.cpp.此外,您需要添加#include Publisher.h到顶部Publisher.cpp,以及您需要包含的其他标头.

您还需要using namespace std;从标题中删除,并std::string在声明中使用.但是,放入using你的cpp文件是可以的.