Jump to content
  • Advertisement
Sign in to follow this  
TheUnbeliever

Template constructor problem

This topic is 3762 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm getting linker problems (I understand what the error means, but not why I'm getting it) with a program. I've managed to reduce it to the following (i.e. the following is obviously not my whole code, but it is the minimum of it that will produce the error).
#ifndef EMULATOR_H
#define EMULATOR_H

#include <boost/array.hpp>

namespace OISC
{
	typedef unsigned int word_t;
	typedef boost::array<word_t, 0>::size_type size_t;

	template <size_t memoryWords> class Emulator
	{
	public:
		template <class Iterator> Emulator(Iterator first, Iterator last);
	};
}

#endif
Emulator.cpp
#include "Emulator.h"

template <size_t memoryWords> template <class Iterator> OISC::Emulator<memoryWords>::Emulator(Iterator first, Iterator last)
{
}
main.cpp
#include <vector>
#include "Emulator.h"

int main(int argc, char* argv[])
{
	const OISC::size_t MEMORY_SIZE = 3072;

	std::vector<OISC::word_t> program(MEMORY_SIZE);

	OISC::Emulator<MEMORY_SIZE> emu(program.begin(), program.end());
}
Error:
main.obj : error LNK2019: unresolved external symbol "public: __thiscall OISC::Emulator<3072>::Emulator<3072><class std::_Vector_iterator<unsigned int,class std::allocator<unsigned int> > >(class std::_Vector_iterator<unsigned int,class std::allocator<unsigned int> >,class std::_Vector_iterator<unsigned int,class std::allocator<unsigned int> >)" (??$?0V?$_Vector_iterator@IV?$allocator@I@std@@@std@@@?$Emulator@$0MAA@@OISC@@QAE@V?$_Vector_iterator@IV?$allocator@I@std@@@std@@0@Z) referenced in function _main
What am I missing?

Share this post


Link to post
Share on other sites
Advertisement
Unless your compiler supports the export keyword, and seeing as that looks like a MSVC error code, I'm willing to bet that yours doesn't, then you can't put the definition of a template in a separate source file without explicit instantiation for specific types. Without explicit instantiation, the complete definition of the template needs to be available at point of instantiation, which means, in effect, that the definition needs to go into the header. (Or an inline file of some sort, etc.)

Share this post


Link to post
Share on other sites
Ah, thank you. That also explains why my test to check my sanity worked – I had the constructor inline.

Oh, and it is indeed MSVC; I should have said.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!