472,344 Members | 1,259 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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(unsigned 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 2618
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(unsigned 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(unsigned 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_instantiation_. 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
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)...
7
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...
12
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...
3
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...
3
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...
1
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,...
8
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...
1
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...
7
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...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

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.