Error compiling template based class

M

maverick

Hello

I am getting an error when i am trying to instantiate an object of the
template based class

#include "foo.h"
int main()
{



CFoo<int> myFoo ;
return 1 ;
}





now I have 2 files
foo.h
---------------------------------------------------

template < class CFooTemp> class CFoo
{
CFoo();
~CFoo() ;


};




foo.cpp
--------------------------------------------
#include "foo.h"



template < class CFooTemp>
CFoo<CFooTemp>::CFoo()
{

}




template < class CFooTemp>
CFoo<CFooTemp>::CFoo()
{

}




------------------------------------------------------------

I get the following errors

main.cpp:12: undefined reference to `CFoot<int>::CFoo()'

main.cpp:14: undefined reference to `CFoo<int>::~CFoo()'




However this error is not seen when i include "foo.cpp" instead of foo.h
 
M

mlimber

maverick said:
Hello

I am getting an error when i am trying to instantiate an object of the
template based class

#include "foo.h"
int main()
{



CFoo<int> myFoo ;
return 1 ;
}





now I have 2 files
foo.h
---------------------------------------------------

template < class CFooTemp> class CFoo
{
CFoo();
~CFoo() ;


};




foo.cpp
--------------------------------------------
#include "foo.h"



template < class CFooTemp>
CFoo<CFooTemp>::CFoo()
{

}




template < class CFooTemp>
CFoo<CFooTemp>::CFoo()
{

}




------------------------------------------------------------

I get the following errors

main.cpp:12: undefined reference to `CFoot<int>::CFoo()'

main.cpp:14: undefined reference to `CFoo<int>::~CFoo()'




However this error is not seen when i include "foo.cpp" instead of foo.h

Please consult this group's FAQs:

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12

Cheers! --M
 
M

Martin Steen

maverick said:
main.cpp:12: undefined reference to `CFoot<int>::CFoo()'

main.cpp:14: undefined reference to `CFoo<int>::~CFoo()'
This is a linker error. The linker doesn't know where
the code for CFoo is.

Add the file foo.cpp to your projekt/makefile. You have
to tell the compiler what files to compile and to link.

However this error is not seen when i include "foo.cpp" instead of foo.h


Of course you can include all .cpp-files into the mainfile (main.cpp).
But this would result in long compile times and a code that is
very hard to maintain.

-Martin
 
G

Greg Comeau

I am getting an error when i am trying to instantiate an object of the
template based class

#include "foo.h"
int main()
{
CFoo<int> myFoo ;
return 1 ;
}

now I have 2 files
foo.h
---------------------------------------------------

template < class CFooTemp> class CFoo
{
CFoo();
~CFoo() ;
};

foo.cpp
--------------------------------------------
#include "foo.h"

template < class CFooTemp>
CFoo<CFooTemp>::CFoo()
{
}

template < class CFooTemp>
CFoo<CFooTemp>::CFoo()
{
}

------------------------------------------------------------

I get the following errors

main.cpp:12: undefined reference to `CFoot<int>::CFoo()'
main.cpp:14: undefined reference to `CFoo<int>::~CFoo()'

However this error is not seen when i include "foo.cpp" instead of foo.h

Check out http://www.comeaucomputing.com/techtalk/templates/#whylinkerror
 
R

red floyd

Martin said:
Ups, of course you're right. That's why it's good to have templates
in a .hpp include file.

No biggie, we've all made oopses here. :)

I'm sure you'll correct one of my posts next time :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,237
Latest member
AvivMNS

Latest Threads

Top