473,725 Members | 2,197 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template class - Compilation problem

Hi everybody,
This is my first time with the template class and I have an strange problem. I
have spent all the afternoon trying to understand it, but I don't get the problem...
I have three files:

matrix.cpp (template class)
matrix.h
test.cpp

when I compile as:
g++ -I . *.cpp -shared -o test.so //There is no problem

if I compile as:
g++ -I . *.cpp -o test.o //There is no problem

Then i get this strange message:
g++ -I . *.cpp -o test.o
/tmp/ccrI7vlF.o: In function `main':
test.cpp:(.text +0x77): undefined reference to `Matrix<float>: :Matrix(unsigne d int)'
collect2: ld returned 1 exit status

I don't know why I get this problem. My main file is:

int main(){
Matrix<float> *vect;
(*vect) = Matrix<float>(3 );
}
Can you give my some help ?
THANKS A LOT,

Marcelo

PS: I am attaching the files if someone wants to get a closer view to my
template class.

Jan 14 '06 #1
4 2726
TB
Marcelo sade:
Hi everybody,
This is my first time with the template class and I have an strange
problem. I have spent all the afternoon trying to understand it, but I
don't get the problem...
I have three files:

matrix.cpp (template class)
matrix.h
test.cpp

when I compile as:
g++ -I . *.cpp -shared -o test.so //There is no problem

if I compile as:
g++ -I . *.cpp -o test.o //There is no problem

Then i get this strange message:
g++ -I . *.cpp -o test.o
/tmp/ccrI7vlF.o: In function `main':
test.cpp:(.text +0x77): undefined reference to
`Matrix<float>: :Matrix(unsigne d int)'
collect2: ld returned 1 exit status

I don't know why I get this problem. My main file is:

<code snipped>

See a few hours earlier thread "template in error"
http://groups.google.com/group/comp....be3e61b3?hl=en

TB
Jan 14 '06 #2
> when I compile as:
g++ -I . *.cpp -shared -o test.so //There is no problem
Then i get this strange message:
g++ -I . *.cpp -o test.o
test.cpp:(.text +0x77): undefined reference to `Matrix<float>: :Matrix(unsigne d int)'


As the template is in a different source file from the main() function,
it is compiled separately. So, the compiler doesn't know it needs to
instantiate a Matrix<float> form of the template at the time when it is
actually building the template; hence, this is not created and you have
an unresolved external. If you compile with -shared, the linker is
happy to believe that the symbol might be resolved by linking against
something that provides it sometime later, so no error appears at this
stage, but if you compile to an executable, you're left with a missing
symbol.

To solve it, either put your template class in the same translation
unit as the stuff using it, or use an explicit instantiation in
matrix.cpp, like "template class Matrix<float>;" which will cause the
compiler to instantiate the required form. That'll also show you a
number of errors in the matrix code (which never actually got compiled
before). Finally, in your 'main', you set the value of vect, but it
isn't pointing anywhere in particular, so you'll get undefined
behaviour (probably a segfault).

Jan 14 '06 #3
Neo
Marcelo,

You are trying to deploy what we would ( in strict C++ ) terms call
as "Template Separation Compilation Model" i.e; to keep template
signature & actual implementation in separate files.

Since g++ still doesn't implement the "export" keyword, the only way
out is to be fall-back on "Template Inclusion Compilation Model" which
is a compile time overhead or else explicitly instantiate the class
instances from outside the main.cpp file.

You may put the same in the translation unit carrying implementation
of that Matrix constructor.

But well, of-course if you are trying to build libraries then only
way is "Template Inclusion Compilation Model"!

Jan 14 '06 #4
Neo wrote:
You are trying to deploy what we would ( in strict C++ ) terms call
as "Template Separation Compilation Model" i.e; to keep template
signature & actual implementation in separate files.

Since g++ still doesn't implement the "export" keyword, the only way
out is to be fall-back on "Template Inclusion Compilation Model" which
is a compile time overhead or else explicitly instantiate the class
instances from outside the main.cpp file.

You may put the same in the translation unit carrying implementation
of that Matrix constructor.

But well, of-course if you are trying to build libraries then only
way is "Template Inclusion Compilation Model"!


There is another option.

If the library developer knows that the template is going to be used
only for a [relatively small] limited set of template arguments, the
templates can be instantiated during building of the library by means
of _explicit_insta ntiation_. Simply declare (explicitly instantiate)
every possible template when compiling the library. The code will be
generated and placed in the library where later the linker will find
it while trying to resolve references in the user's code.

Look up "explicit instantiation" in the news archives, I am certain
you can find the details of what I am talking about.

V
Jan 14 '06 #5

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

Similar topics

2
11326
by: Alex Vinokur | last post by:
========================================= Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927 (prerelease) ========================================= Here is some program which is compiled and works fine. ############################################
7
5947
by: Patrick Kowalzick | last post by:
Dear all, I just wondered if it is possible to count the number of classes created via a template class at compile time. To show what I mean I post an example, which is not working but carries the idea: static int counter = 0; // this variable can be changed only at runtime... template <typename T> struct want_to_be_counted;
12
2617
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in an embedded environment with multiple processors. I created a policy for classes, which, I had hoped, would automatically register the class with the appropriate factory: // In some header file... #include <cassert>
3
1777
by: martin.druon | last post by:
Hi, I created a template class to represent hypermatrix. I would like to add methods where the number of parameters are checked during the compilation time. For example : template <size_t dim> class Matrix { protected :
3
3494
by: Yohan | last post by:
Hello, I have a question concerning the template classes and their parameters. Is it possible to set a condition on the template parameters in a way that could block the compilation if the condition isn't true? (e.g. by throwing an error message to the compiler). And is it possible to use this type of condition to make the compiler choose the best method (between some others). Here is an example to illustrate my question:
1
3618
by: toton | last post by:
Hi, Is there any tool for template debugging, during compilation process ? Just like gdb or visual studio debugger do it during program execution, a step by step debug for the program, I am looking for something which will do the process during compilation and show what is getting substituted for which call. Otherwise sometimes, compilation fails saying some value_type can't be substituted by value_type& and etc inside a template call,...
8
7627
by: nishit.gupta | last post by:
I was having a problem with template class memer function definition , so i serched the net and find that template class member fuction definition should be in header file else compilation will be successful but not linking. Can somebody help me in telling the exact reason why compiler cannot find the reference of template class member function definition defined in cpp file?? following problem is not linking (undefined reference of...
1
2666
by: Alex Vinokur | last post by:
Hi, I have compilation problem on SUN CC compiler with template while using option -m64 (64-bit addressing model). No problem while using option -m32 (32-bit addressing model) $ CC -V CC: Sun C++ 5.9 SunOS_sparc Patch 124863-01 2007/07/25
7
5770
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
8888
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
8752
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
9401
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...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
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
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.