简体   繁体   中英

Object already defined but I'm not sure where

I'm new to C++ and the error codes aren't really compelling to me.

I am trying to build a simple C++ OOP program as a homework assignment but which will gather then display information about book etc.

I am getting an error:

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

Am I missing something here?

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;
}

You should never include a cpp file in another cpp file or in a header. This almost inevitably leads to duplicate object definitions.

Put declarations into the header file, and definitions into the cpp files. Include headers in cpp files where you need to have access to methods of objects defined in other cpp files.

Take Publisher.cpp as an example: the top part up to and including the }; line need to go into the Publisher.h file. Everything else has to remain in the Publisher.cpp . In addition, you need to add #include Publisher.h to the top of Publisher.cpp , along with other headers that you need to include.

You also need to remove using namespace std; from the header, and use std::string in your declarations. It is OK to put using in your cpp file, though.

Use #IFNDEF to avoid duplicate object definitions if you are really in need to add some cpp to another cpp.

Like

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

class Publisher
{...}

#endif

In addition to what Dasblinkenlight said,

Your problem is that you did not use ifndef guard. You should use that to avoid this error. A quick search will give you the hint how to add headers.

Create header file example: resource.h and put there prototypes of functions also class. So it will look like:

#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
};

Edit Book.cpp to this:

#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;
}

And Publisher.cpp to this:

#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;
}

And try to do same with Author.cpp,so you put function's prototypes (class) to "resource.h" and include "resource.h" to Author.cpp.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM