473,385 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Error compiling template based class

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<intmyFoo ;
return 1 ;
}

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

template < class CFooTempclass 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

Oct 23 '06 #1
8 1481
maverick wrote:
Hello

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

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 23 '06 #2

maverick wrote:
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<intmyFoo ;
return 1 ;
}

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

template < class CFooTempclass 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-lit...html#faq-35.12

Cheers! --M

Oct 23 '06 #3
maverick schrieb:
>
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

Oct 23 '06 #4
Martin Steen wrote:
maverick schrieb:
>>
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.
No, Martin, it's a template instantiation issue.
http://www.parashift.com/c++-faq-lit...html#faq-35.12
Oct 23 '06 #5
red floyd schrieb:
>
No, Martin, it's a template instantiation issue.
http://www.parashift.com/c++-faq-lit...html#faq-35.12
Ups, of course you're right. That's why it's good to have templates
in a .hpp include file.

-Martin
Oct 23 '06 #6
In article <11*********************@f16g2000cwb.googlegroups. com>,
maverick <ra*****@gmail.comwrote:
>I am getting an error when i am trying to instantiate an object of the
template based class

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

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

template < class CFooTempclass 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/techt.../#whylinkerror
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 23 '06 #7
Martin Steen wrote:
red floyd schrieb:
>>
No, Martin, it's a template instantiation issue.
http://www.parashift.com/c++-faq-lit...html#faq-35.12

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 :-)

Oct 23 '06 #8
In article <eh*************@news.t-online.com>,
Martin Steen <A0***************@spamgourmet.comwrote:
>maverick schrieb:
>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.
That's insufficient because instantiation throws in some
curves over the traditional linker. See
http://www.comeaucomputing.com/techt.../#whylinkerror
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 24 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: sebastien NO Maraux SPAM | last post by:
I am using Ghost Lib 4.0, which is SDK for Phantom haptic device. this lib does not compile under .net, seemingly because of a union of this type : union A { union A* aList; char b; };
1
by: Avery Fong | last post by:
The following program will result in a compile error when building under Debug but will compile under Release. Why does is work under Release mode but not under Debug This program is developed...
1
by: Karine Pinault | last post by:
I am having trouble trying to compile existing code with the /clr flag. The following error occurs and I can't figure it out how to fix this problem. I have the following declaration:...
2
by: David Williams | last post by:
Hi all, I get errors when compiling the .cpp file below, both on Visual Studio and G++. 01: //------------------------------------------------- 02: template <typename Type> 03: class Outer 04:...
4
by: KK | last post by:
Hello all, I have class 'atr' which is based on templates. My idea was to initialize it two scenarios 1. std::string case 2. other data types I have defined the Init for the above different...
2
by: Nick | last post by:
I'm learning C++ and ran into a compile error using Visual C++ 2005 Express on the following example program (located at http://www.cplusplus.com/doc/tutorial/templates.html): // template...
7
by: JustBeSimple | last post by:
Hi Everyone, I'm having a problem compiling useing the VS2005 .NET I need help to resolve those error, I try to create a new project it doesn't help any suggestion? I got the following errors:...
1
by: BSand0764 | last post by:
I'm getting an error that I can't seem to resolve. When I compile the Functor related logic in a test program, the files compile and execute properly (see Listing #1). However, when I...
6
by: hsmit.home | last post by:
Hello, I came across a strange error and it's really been bugging me. Maybe someone else has come across this and any insight would be appreciated. What I'm trying to accomplish is using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.