Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-19-2004, 04:32 PM   #1
Registered: Jul 2001
Distribution: Slackware 12
Posts: 511

Rep: Reputation: 32
C++ - class template - linker errors?

I'm trying to write a linked-list type queue class with a template. My code compiles without errors or warnings, but when it links I get this:

main.o(.text+0x19): In function `main':
: undefined reference to `WaitQueue<int>::WaitQueue[in-charge]()'
main.o(.text+0x6e): In function `main':
: undefined reference to `WaitQueue<int>::push(int const&)'
main.o(.text+0x93): In function `main':
: undefined reference to `WaitQueue<int>::~WaitQueue [in-charge]()'
main.o(.text+0xb1): In function `main':
: undefined reference to `WaitQueue<int>::~WaitQueue [in-charge]()'

My code in main.cpp is
#include <iostream>
using namespace std;
#include "WaitQueue.h"

int main()
	WaitQueue<int> wc;
	int tmp;

	cout << "Enter some ints (-1 to stop):" << endl;
	cin >> tmp;

	while (tmp != -1)
		cin >> tmp;
	return 0;
Does anyone know how to fix this?
Old 10-19-2004, 04:50 PM   #2
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Well, since you didn't post the contents of WaitQueue.h, I can only guess. My guess is that you are trying to only declare the WaitQueue class in the .h, and trying to implement that in a WaitQueue.cpp file.

If that is the case, it won't work with template classes. You have to have the entire class definition in the header. There was a thread on this here recently with a more detailed explanation of why that is, that you might want to search for...
Old 10-19-2004, 04:54 PM   #3
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Since I have a bit of time, I decided to search for that thread for you. And here is where it is...


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ class template doesn't work!! tagigogo Programming 2 11-02-2005 05:45 PM
Template class with a template member... Nicholas Bishop Programming 3 02-21-2005 09:27 PM
function prototypes in template class' (c++) qwijibow Programming 4 12-13-2004 10:34 AM
template class ckcheung0927 Programming 3 11-28-2004 04:59 PM
C++ template class syntax rong Programming 12 08-13-2003 01:26 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:13 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration