[SOLVED] link error w/ generic class

Hey everyone,

Are there any known precautions I should take when building code with a generic class? I've made sure to fully define my classes in the source file, i.e:

template<typename T> void C1<T>::foo(){...}

The reason I ask is that I'm getting undefined reference errors at link time, such as:

error: undefined reference to C1<int>::foo()

My header file is included, and the namespaces match up. Any other things I should be watching out for?
Last edited on
The declaration looks okay. Is the definition of foo() compiled with the rest of the code?
Actually, that is the definition, I just truncated out the code. The declaration is in the header, where it's declared as:

1
2
3
4
5
template<typename T> class C1
{
...
void foo();
};


Would it help if I posted the header and source on the net? Be advised that it's about 150 lines in total, and there's a couple of other classes in there too...
Last edited on
It might be better to upload it to http://pastebin.com/
http://pastebin.com/d7d5572b8

Header from line 1-64; source begins at line 65
Last edited on
Uh... How am I supposed to know what class is C1 and what function is foo()?
...through magic telepathic abilities, of course...

My apologies. The class is question is ObjectManager, and unfortunately all of its methods are problematic. Each one gives me an undefined reference error.
Actually, now that I look closer, ObjectManager is the only class there. :-P

Are you sure you're compiling the cpp along with the rest of the project? I don't see any discrepancies between the declaration and definition signatures.

You can use this example to see how template methods should be declared and defined:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
template <typename T>
struct A{
	A(T &a);
	A(T a);
	T &get();
private:
	T d;
};

template <typename T>
A<T>::A(T &a){
	this->d=a;
}

template <typename T>
A<T>::A(T a){
	this->d=a;
}

template <typename T>
T &A<T>::get(){
	return this->d;
}

int main(){
	A<int> a(12);
	return 0;
}
I'm dead certain. It's in the compile log and everything. I guess I'll just keep this topic open and see if we can't get some more input.
Typically, template functions are defined inline, in the header file. Are yours defined in a seperate CPP file?

Line 64 seems to imply end of header file/start of cpp file. Is this the case?
Man, I always forget that!
Whose idea was to put something so counter-intuitive in the standard?
ahh, for real? Yeah, they're defined in a header/source pair. Jeez, that is weird. I'll give it a shot and put them in the header. Thanks a lot for the tip!
Topic archived. No new replies allowed.