我是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
我在这里错过了什么吗?
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;
}
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;
}
答案 0 :(得分:9)
您不应该在另一个cpp
文件或标头中包含cpp
文件。这几乎不可避免地导致重复的对象定义。
将声明放入头文件,将 definitions 放入cpp文件。在cpp文件中包含标题,您需要访问其他cpp文件中定义的对象方法。
以Publisher.cpp
为例:包含};
行的顶部部分需要进入Publisher.h
文件。其他一切都必须留在Publisher.cpp
。此外,您需要将#include Publisher.h
添加到Publisher.cpp
的顶部,以及需要包含的其他标题。
您还需要从标题中删除using namespace std;
,并在声明中使用std::string
。不过,可以将using
放在cpp
文件中。
答案 1 :(得分:4)
如果您真的需要将一些cpp添加到另一个cpp,请使用#IFNDEF
来避免重复的对象定义。
像
#include <iostream>
using namespace std;
#ifndef CPPADDED
#define CPPADDED
class Publisher
{...}
#endif
答案 2 :(得分:1)
除了Dasblinkenlight所说的,
您的问题是您没有使用ifndef
警卫。您应该使用它来避免此错误。快速搜索将为您提供如何添加标题的提示。
答案 3 :(得分: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中。