3

我是 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

我在这里错过了什么吗?

书本.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;
}

发布者.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;
}
4

4 回答 4

10

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

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

Publisher.cpp个例子:顶部到包括该};行的部分需要进入Publisher.h文件。其他所有内容都必须保留在Publisher.cpp. 此外,您需要添加#include Publisher.h到顶部Publisher.cpp,以及您需要包含的其他标题。

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

于 2012-09-01T18:09:15.747 回答
4

#IFNDEF如果您确实需要将一些 cpp 添加到另一个 cpp,请使用它来避免重复的对象定义。

#include <iostream>
using namespace std;
#ifndef CPPADDED
#define CPPADDED

class Publisher
{...}

#endif
于 2012-09-01T18:15:49.510 回答
1

除了Dasblinkenlight所说的,

你的问题是你没有使用ifndef警卫。您应该使用它来避免此错误。快速搜索将为您提供如何添加标题的提示。

于 2012-09-01T18:14:31.567 回答
1

创建头文件示例:resource.h 并将函数原型也放入类中。所以它看起来像:

#include <iostream>
#include <string>
using namespace std;
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;
};
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;
};
class Author{
    //... prototypes
};

将 Book.cpp 编辑为此:

#include "resource.h"
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;
}

和 Publisher.cpp 到这个:

#include "resource.h"
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;
}

并尝试对 Author.cpp 执行相同操作,因此将函数的原型(类)放入“resource.h”并将“resource.h”包含到 Author.cpp。

于 2012-09-01T18:24:09.663 回答