问题
I'm writing a couple of classes for an exercise on copy control members in the C++ primer book.
I put the classes definitions in their respective header files,and implementation in code files. I seem to have done all things like they should be done, but I get an error at link time I can't understand (and I already spent half an hour thinking about it)
here's the message class (headers and code files)
message.h
#include <string>
#include "folder.h"
class message {
friend void swap(message&,message&);
public:
explicit
message(const std::string& =std::string());
// copy control
message(const message&);
~message();
message &operator=(const message&);
// interface
void save(folder&);
void remove(folder&);
private:
// data members
std::string text;
std::set<folder*> folders;
// utility functions
void add_to_folders(const message&);
void remove_from_folders();
};
void swap(message &m1,message &m2)
{
swap(m1.text,m2.text);
swap(m1.folders,m2.folders);
}
message.cpp
#include "message.h"
message::message(const std::string &t) : text(t) {}
message::message(const message &other) : text(other.text),folders(other.folders)
{
add_to_folders(other);
}
message::~message()
{
remove_from_folders();
}
message& message::operator=(const message &other)
{
remove_from_folders();
text=other.text;
folders=other.folders;
add_to_folders(other);
return *this;
}
void message::save(folder &f)
{
folders.insert(&f);
f.addMsg(this);
}
void message::remove(folder &f)
{
folders.erase(&f);
f.remMsg(this);
}
void message::add_to_folders(const message& m)
{
for (auto i:m.folders)
i->addMsg(this);
}
void message::remove_from_folders()
{
for (auto i:folders)
i->remMsg(this);
folders.clear();
}
and here's the folder class
folder.h
#include <set>
class message;
class folder {
public:
/*folder();
folder(const folder&);
~folder();
folder &operator=(const folder&);*/
void addMsg(message*);
void remMsg(message*);
private:
std::set<message*> messages;
};
folder.cpp
#include "folder.h"
void folder::addMsg(message *m)
{
messages.insert(m);
}
void folder::remMsg(message *m)
{
messages.erase(m);
}
Every message holds a collection of pointer to folders and vice versa. Each file compiles fine separately, but when I link the three of them I get an error saying that the swap function (that I defined in the message class header) is redefined.
main.cpp
#include "message.h"
int main()
{
folder f1;
message m1;
}
What am I doing wrong?
回答1:
You are including the header file message.h
in both message.cpp
and main.cpp
. Since you have:
void swap(message &m1,message &m2)
{
swap(m1.text,m2.text);
swap(m1.folders,m2.folders);
}
In message.h
you now have the swap function declared in both the main.cpp
translation unit and the message.cpp
translation unit. This will cause a multiple definition error. You need to move swap()
into message.cpp
回答2:
Did you remember to put include guards in your header files?
#ifndef __MY_HEADER__
#define __MY_HEADER__
...
#endif
or if you're comfortable using features outside the C++ standard:
#pragma once
...
回答3:
The solution to your problem is very simple: you have declared void swap( data1, data2 ) as a friend function in your message class then you define it at the bottom of your *.h file. Take the implementation out of the *.h file and place it into your message.cpp file before any of your classes functions. Make sure you have the definition(implementation) before your classe's constructor in the *.cpp file since your class object will rely on this swap function. This should resolve your issue on the build process. Yes each of the 3 files will compile successfully but the solution will not build because it can not resolve this method since you have it defined in your *.h file. Also you are using the class prototype message in your folder.h file you should also have the #include "message.h" in your folder.cpp file, but this will require you to have your *.h files with the #ifndef ... #define... #endif directives. This should help you!
来源:https://stackoverflow.com/questions/32504570/multiple-definition-error-at-link-time