Undefined Reference

I am getting an error 'Undefined Reference' and I cannot fix the problem. I am using Boost libraries and am trying to do serialization/deserialization.
Here is the code:

Manifest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "Manifest.h"
using namespace boost::serialization;
using namespace boost::archive;

template<class Archive>
void Manifest::serialize(Archive & ar, const unsigned int version) {
	ar & make_nvp("version", versionNum);
}

Manifest::Manifest() {}
Manifest::Manifest(std::string _vers) : versionNum(_vers) {}
Manifest::~Manifest() {}


Manifest.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "msgBase.h"
using namespace boost::archive;

class Manifest : public msgBase {
private:
	friend class boost::serialization::access;
	template<class Archive>
	void serialize(Archive & ar, const unsigned int someInt);
protected:
	std::string versionNum;
public:
	Manifest();
	Manifest(std::string);
	~Manifest();

	std::string getVersionNumber();
};


Here is the exact error:

/usr/local/include/boost/serialization/access.hpp:118: undefined reference to `void Manifest::serialize<boost::archive::xml_iarchive>(boost::archive::xml_iarchive&, unsigned int)'

I have looked up other forums and most say to "forward declare" the class or namespace. I am not completely sure how this is done, but I also believe this isnt my problem.

Thank you in advance.

-zonx
Last edited on
Oh and I believe I have everything linked correctly. I have multiple projects that I have up and running of which are using boost libraries and using almost this exact same class, but they are working just fine. I have tried to replicate the class in this project and it does not work for some reason. I am using eclipse on xubuntu.
When compiling Manifest.cpp, how would the compiler know that some other file is going to attempt to link against Manifest::serialize<boost::archive::xml_iarchive>?

In short, don't put templates in .cpp files.

(or was this printed when compiling Manifest.cpp?)
Last edited on
This was printed when compiling...I am actually compiling a main.cpp of which references Manifest.h. I have a function which is called load_xml where I am passing the Manifest class as an argument.

Update:

I actually just got it to compile, but now eclipse is arguing that it cannot find the binary when I try to run the program.
closed account (Dy7SLyTq)
your missing an arguement to serialize in access.hpp
That is what I thought at the beginning, but I checked on that to make sure. This is the line in access.hpp that it is referencing:

t.serialize(ar, file_version);

ar is type template<class Archive> and file_version is unsigned int

This corresponds with what I have in my code.
closed account (Dy7SLyTq)
my mistake i thought it had three. could it be that the prototype has different parameters than the actual defenition?
I would say no. I have used these libraries multiple times in the past, for some reason I am running into this issue for the first time...I have a feeling that it has to do with either Eclipse not linking correctly. But I am not 100% sure on that.
closed account (Dy7SLyTq)
did u check?
Cubbi already mentioned it but I'll reiterate, you need to put the implementation of the template function in the header.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Manifest.h
#include "msgBase.h"
using namespace boost::archive;

class Manifest : public msgBase {
private:
	friend class boost::serialization::access;
	template<class Archive>
	void serialize(Archive & ar, const unsigned int version) {
	    ar & make_nvp("version", versionNum);
        }
protected:
	std::string versionNum;
public:
	Manifest();
	Manifest(std::string);
	~Manifest();

	std::string getVersionNumber();
};
Last edited on
When i do this, I get an error of:

../Manifest.h:11:7: error: extra qualification ‘Manifest::’ on member ‘serialize’ [-fpermissive]
../Manifest.cpp:7:6: error: redefinition of ‘void Manifest::serialize(Archive&, unsigned int)’
../Manifest.h:11:7: error: ‘void Manifest::serialize(Archive&, unsigned int)’ previously declared here

Oops, remove the Manifest:: from the function. See the edited the code in my previous post.
Last edited on
Ok I see now...thank you. This seemed to fix my immediate problem.

I had no idea that you cant define the template in the cpp. It's odd because I have written templates like that for a while and I havnt come across that error until just now.

Regardless, thank you!
Topic archived. No new replies allowed.