Template --> Headache!


reiboul

Peace sells... but who's buying?
Joined
Jul 16, 2006
Messages
587
Age
37
Location
France
Website
Visit site
Hi all!

I'm a computer student, and am doing some C++ exercices, but i'm stuck : I am using templates to print queue type, and have compilation errors!!
If someone knows some good tutos, i'm intererested lol ;)

Here's my code :

in exercice.cpp
Code:
template<typename T>
void printQueue(const queue<T>& q)
	{
	queue<T> tmp = q;
	while ( !tmp.empty() )
		{
		cout << tmp.front() << " ";
		tmp.pop();
		}
	cout << endl;
	}


prototype in exercice.h
Code:
template<typename T>
void printQueue(const queue<T>&);

and in main.cpp
Code:
queue<int> q1;
promptQueue(q1);
cout << "queue 1 : "; printQueue(q1);


I'm sure it's easy for a casual GP2X developper but hey, everybody's got to learn, right? ;)

many thanks to the community ;)

*edit* Forgot g++' error ;)

/tmp/ccp6u9n1.o: In function `main':
main.cpp:(.text+0x1ec): référence indéfinie vers « void printQueue<int>(std::queue<int, std::deque<int, std::allocator<int> > > const&)»
main.cpp:(.text+0x20e): référence indéfinie vers « void printQueue<int>(std::queue<int, std::deque<int, std::allocator<int> > > const&)»
main.cpp:(.text+0x252): référence indéfinie vers « void printQueue<int>(std::queue<int, std::deque<int, std::allocator<int> > > const&)»
main.cpp:(.text+0x282): référence indéfinie vers « void printQueue<int>(std::queue<int, std::deque<int, std::allocator<int> > > const&)»
main.cpp:(.text+0x2d7): référence indéfinie vers « void printQueue<int>(std::queue<int, std::deque<int, std::allocator<int> > > const&)»
collect2: ld returned 1 exit status
 
reiboul posted on Dec 6 2006 at 06:25 PM said:
Hi all!

I'm a computer student, and am doing some C++ exercices, but i'm stuck : I am using templates to print queue type, and have compilation errors!!
If someone knows some good tutos, i'm intererested lol ;)

Here's my code :

in exercice.cpp
Code:
template<typename T>
void printQueue(const queue<T>& q)
	{
	queue<T> tmp = q;
	while ( !tmp.empty() )
		{
		cout << tmp.front() << " ";
		tmp.pop();
		}
	cout << endl;
	}


prototype in exercice.h
Code:
template<typename T>
void printQueue(const queue<T>&);

and in main.cpp
Code:
queue<int> q1;
promptQueue(q1);
cout << "queue 1 : "; printQueue(q1);


I'm sure it's easy for a casual GP2X developper but hey, everybody's got to learn, right? ;)

many thanks to the community ;)

*edit* Forgot g++' error ;)

/tmp/ccp6u9n1.o: In function `main':
main.cpp:(.text+0x1ec): référence indéfinie vers « void printQueue<int>(std::queue<int, std::deque<int, std::allocator<int> > > const&)»
main.cpp:(.text+0x20e): référence indéfinie vers « void printQueue<int>(std::queue<int, std::deque<int, std::allocator<int> > > const&)»
main.cpp:(.text+0x252): référence indéfinie vers « void printQueue<int>(std::queue<int, std::deque<int, std::allocator<int> > > const&)»
main.cpp:(.text+0x282): référence indéfinie vers « void printQueue<int>(std::queue<int, std::deque<int, std::allocator<int> > > const&)»
main.cpp:(.text+0x2d7): référence indéfinie vers « void printQueue<int>(std::queue<int, std::deque<int, std::allocator<int> > > const&)»
collect2: ld returned 1 exit status

printQueue implementation needs to be in header not source file
 
Last edited by a moderator:
... I dont believe it was as simple as that!!! :|
But why does it have to be in the header and not source? more generally, do you know about a good tuto on what to put on header, what tu put on source, etc?

thanks for your help ;) (BTW, it's working ;) )

*GP2X Developper-wanabee*
 
The compiler needs to know the complete template at the time the template is used. If you split the template declaration over a .h and a .cpp, then the compiler only sees half the template and it cannot generate the code for the template instantiation.
 
Back
Top