473,804 Members | 3,225 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

gcc doesn't compile templates

I am trying to write a wrapper class, which can store any data type to
wrap it into a class. My compiler is gcc (for c++) version 3.3.1 on a
SuSE Linux 9.0 system.
I start with the following code:
template <typename T>
class Wrapper {
public:
Wrapper(T avalue);
private:
int value;
};
in the header wrapper.h, and in wrapper.cpp:
#include "wrapper.h"

template <typename T>
Wrapper<T>::Wra pper(T avalue) {
value = avalue;
}
When I compile now, using
$ gcc -c wrapper.cpp
gcc doesn't report any errors.
Linking with the resulting wrapper.o:
$ gcc -o testwrapper testwrapper.cpp wrapper.o
/tmp/abcdefgh.o(...) : In function `main':
undefined reference to Wrapper<int>::W rapper[in-charge](T)
collect2: ld returned 1 status
What have I done wrong?

--
Michael Schutte <who doesn't want to get spam>
Jul 22 '05 #1
5 5129
Michael Schutte wrote in news:408a405e$0 $15770$91cee783
@newsreader02.h ighway.telekom. at in comp.lang.c++:
I am trying to write a wrapper class, which can store any data type to
wrap it into a class. My compiler is gcc (for c++) version 3.3.1 on a
SuSE Linux 9.0 system.
I start with the following code:
template <typename T>
class Wrapper {
public:
Wrapper(T avalue);
private:
int value;
};
in the header wrapper.h, and in wrapper.cpp:
#include "wrapper.h"

Move all the (template) code into your header file
template <typename T>
Wrapper<T>::Wra pper(T avalue) {
value = avalue;
}
You can omit this (if wrapper.cpp is now empty)
When I compile now, using
$ gcc -c wrapper.cpp
gcc doesn't report any errors.
Linking with the resulting wrapper.o:
$ gcc -o testwrapper testwrapper.cpp wrapper.o


Assuming wrapper.cpp is now empty:

$ gcc -o testwrapper testwrapper.cpp
Read the FAQ:

http://www.parashift.com/c++-faq-lite/

It will take a while, but it will be worth it.

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

"Michael Schutte" <idonotsay@myem ailaddr> wrote in message
news:40******** *************** @newsreader02.h ighway.telekom. at...
I am trying to write a wrapper class, which can store any data type to
wrap it into a class. My compiler is gcc (for c++) version 3.3.1 on a
SuSE Linux 9.0 system.
I start with the following code:
template <typename T>
class Wrapper {
public:
Wrapper(T avalue);
private:
int value;
};
in the header wrapper.h, and in wrapper.cpp:
#include "wrapper.h"

template <typename T>
Wrapper<T>::Wra pper(T avalue) {
value = avalue;
}
When I compile now, using
$ gcc -c wrapper.cpp
gcc doesn't report any errors.
Linking with the resulting wrapper.o:
$ gcc -o testwrapper testwrapper.cpp wrapper.o
/tmp/abcdefgh.o(...) : In function `main':
undefined reference to Wrapper<int>::W rapper[in-charge](T)
collect2: ld returned 1 status
What have I done wrong?


Put the template in the header file. Templates belong in header files unless
your compiler supports the export keyword (very few do).

We must get this question about three times a week. It's in the FAQ, its a
good idea to check the FAQ before posting

http://www.parashift.com/c++-faq-lit...html#faq-34.14

john
Jul 22 '05 #3
I'm sorry that I haven't checked the FAQs -- I know about the
netiquette. But I mustn't stay long in the WWW (I'm 13 years old and
must use the account of my parents) and have already searched for a long
time...

--
Michael Schutte <who doesn't want to get spam>
Jul 22 '05 #4
Michael Schutte wrote:
I'm sorry that I haven't checked the FAQs -- I know about the
netiquette. But I mustn't stay long in the WWW (I'm 13 years old and
must use the account of my parents) and have already searched for a long
time...


Download the faq to your local computer and search off line.
Jul 22 '05 #5
Why don't u try using 'g++', instead of gcc.

Michael Schutte wrote:
I am trying to write a wrapper class, which can store any data type to
wrap it into a class. My compiler is gcc (for c++) version 3.3.1 on a
SuSE Linux 9.0 system.
I start with the following code:
template <typename T>
class Wrapper {
public:
Wrapper(T avalue);
private:
int value;
};
in the header wrapper.h, and in wrapper.cpp:
#include "wrapper.h"

template <typename T>
Wrapper<T>::Wra pper(T avalue) {
value = avalue;
}
When I compile now, using
$ gcc -c wrapper.cpp
gcc doesn't report any errors.
Linking with the resulting wrapper.o:
$ gcc -o testwrapper testwrapper.cpp wrapper.o
/tmp/abcdefgh.o(...) : In function `main':
undefined reference to Wrapper<int>::W rapper[in-charge](T)
collect2: ld returned 1 status
What have I done wrong?


--
Rakesh Kumar
** Remove nospamplz from my email address for my real email **
Jul 22 '05 #6

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

Similar topics

2
1751
by: Dhruv | last post by:
I was wondering if anyone could compile this code for me on VC. The problem that I've run into is that VC++6 does not support templates fully, and so, I cannot get this to compile, which relies heavily on templates. So, if someone can compile the explicit specialication for me with VC++7, and then I could try to use it in VC++6 (probably), if object files are interconvertible, as in I can use object files of VC++7 on VC++6, then? ...
6
3337
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for my particular application. One of the advantages is that the _compiler_ can force inner matrix dimensions used in multiplication to agree. A _complie-time_ error will be triggered if you write A * B and the number of coluns in A does not equal the...
3
1657
by: Steve | last post by:
Sorry to be stupid, but I've been poring over TC++PL, and according to what I see on page 351 (3rd ed.) the following code (split over three files) should work. Instead, when I try to compile with g++ 3.3.3 (cygwin special), I get the following error. What am I doing wrong? // the error: ----------------------------------------------- ~/cpplib/tests g++ template_test.cpp template_main.cpp...
5
2019
by: PengYu.UT | last post by:
Hi, I heard that debug some C++ templates is very difficult. I'm wondering whether it is possible to compile C++ program with templates to pure C or C++ program without templates? Best wishes, Peng
4
9622
by: andrewcw | last post by:
I am moving some code forward from .NET 1.1. I was able to load the XSL file and perform the transform. The MSDN documentation looks like it should be easy. But I get a compile error. Ideas ? How can I share my XSLT ( there are 2 helper stylesheets, and I check that they are also available... the loaded xsl makes reference to the helper xsl files. Where should I start looking ? // Load the stylesheet. errBefore =...
16
5435
by: desktop | last post by:
I have read that using templates makes types know at compile time and using inheritance the types are first decided at runtime. The use of pointers and casts also indicates that the types will first be know at runtime. But is there some strict definitions that defines runtime code and compile time code that can be used in general?
6
3679
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
6
1702
by: liam_herron | last post by:
Any idea why the following code doesn't compile? #include <iostream> #include <vector> class A { public:
7
5776
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it compiled no problem. But I can't find the what the problem is with templates? Please help. The main is in test-linked-list.cpp. There are two template classes. One is List1, the other one is ListNode. The codes are below: // test-linked-list.cpp :...
0
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9588
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10589
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10327
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.