473,320 Members | 2,000 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,320 software developers and data experts.

g++ linking problem with using template

hi all,
i have a template class that looks like:

******
foo.h:
******

#ifndef _FOO_H_
#define _FOO_H_

template<class T>
class Foo{
public:
...
privdate:
...
};

#endif

*********
foo.cpp:
*********

#include "foo.h"

template<class T>
Foo<T>::Foo(){
...
}

template<class T>
Foo<T>::~Foo(){
...
}

i compiled that into a foo.o with:

g++ -c foo.cpp

and then i have a driver:

#include "foo.h"

int main(int argc,char** argv){
Foo<int> f;
}

and i compile the driver with:

g++ driver.cpp foo.o -o driver

but there is error:

/tmp/c8KKKLKD.o(.text+0x19): In function `main':
:undefined reference to `Foo<int>::Foo[in-charge]()'
.... etc

if i send the same problem to a VC++ compiler, it compiles ok. why is
g++ not able to compile the program? why can't it find the
constructor, i already supply foo.o so it should find the function
there.

any help? thanks!
Jul 22 '05 #1
1 1255
pembed2003 wrote:

i have a template class that looks like:

******
foo.h:
******

#ifndef _FOO_H_
#define _FOO_H_

template<class T>
class Foo{
public:
...
privdate:
...
};

#endif

*********
foo.cpp:
*********

#include "foo.h"

template<class T>
Foo<T>::Foo(){
...
}

template<class T>
Foo<T>::~Foo(){
...
}

i compiled that into a foo.o with:

g++ -c foo.cpp

and then i have a driver:

#include "foo.h"

int main(int argc,char** argv){
Foo<int> f;
}

and i compile the driver with:

g++ driver.cpp foo.o -o driver

but there is error:

/tmp/c8KKKLKD.o(.text+0x19): In function `main':
:undefined reference to `Foo<int>::Foo[in-charge]()'
... etc

if i send the same problem to a VC++ compiler, it compiles ok. why is
g++ not able to compile the program? why can't it find the
constructor, i already supply foo.o so it should find the function
there.


Type

info gcc

and search for Template Instantiation.

Post this question to the gnu.g++.help newsgroup.

Explicitly instantiate Foo<int> in your foo.cpp source file.

Jul 22 '05 #2

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

Similar topics

1
by: Tobias Langner | last post by:
I try to program a smart-pointer using policies. I rely heavyly on templates during this. The program compiles fine - but I get a linker error: test.o(.text+0x3e): In function `testSmartPtr()':...
5
by: Yoon-Soo Lee | last post by:
I am using Visual C++ .NET 2003 and running into some linking error from the following template code. The error messages is error LNK2019: unresolved external symbol "class...
15
by: Rob Ratcliff | last post by:
I'm compiling the latest version of a CORBA ORB called MICO on a Cray X1. It makes heavy use of templates and namespaces. Up until the link step, the C++ source code compiled flawlessly. But, when...
15
by: Improving | last post by:
I have a template class that has static members, so in the .cpp file I have defined them with templated definitions. Now when in a different ..cpp file that includes the header file for the...
5
by: sleepydj | last post by:
Hello, I have a simple program to create strings from the corresponding double/integer values. For any data type similar to int, I have a template set up in a file misc.h: ...
4
by: Gary Hughes | last post by:
Hi all, sometime I posted a problem in here where I was getting the following error from the linker in VS C++ 2003. Linking... GCClass.obj : error LNK2022: metadata operation failed (80131188)...
0
by: Pat Sagaser via .NET 247 | last post by:
I'm using a repeater with a dynamic template. I don't know the fields to display (or how many) until runtime. I have everything working except for linking Button events to the repeaters ItemCommand...
0
by: ivan.leben | last post by:
I am writing this in a new thread to alert that I found a solution to the problem mentioned here: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/7970afaa089fd5b8 and to avoid...
2
by: pssraju | last post by:
Hi, At present application was built on solaris 9 using sun studio 9 (Sun C++ 5.6) & rouguewave sorce pro 5. We are planning to port the same application onto SuSE Linux 9.5.0 using GCC 3.3.3 & RW...
9
by: Peskov Dmitry | last post by:
It is a very basic question.Surely i got something wrong in my basic understanding. //Contents of file1.cpp using namespace std; #include <iostream> template <typename T> class my_stack;
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.