Solved

Compiling with templates

Posted on 1997-06-10
2
426 Views
Last Modified: 2008-03-03
I am using Sunsoft's  C++ SPARCcompiler and having problems
with templates.

I have created a template class which implements arrays and
used seperate header and source files for the template. If
the compiled object file which uses the template is put in
a common archive, there are problems of undefined symbols
during link time. This does not occur if the object file is
not put in the archive.

CC -I../hdr -pti../templates -ptr../templates main.cc -L/home/rsaini/temp/src -lutil -lrwtool
Undefined                       first referenced
 symbol                             in file
Array<float>::operator [](int)                    /home/rsaini/temp/src/libutil.a(pref.o)
Array<RWCString>::get_Size(void)   /home/rsaini/temp/src/libutil.a(pref.o)
Array<RWCString>::operator [](int)          /home/rsaini/temp/src/libutil.a(pref.o)
Array<float>::get_Size(void)             /home/rsaini/temp/src/libutil.a(pref.o)
ld: fatal: Symbol referencing errors. No output written to a.out

If I use the object file directly instead of putting it in
the archive it works ok.

CC -I../hdr -pti../templates -ptr../templates main.cc pref.o -lrwtool

What is the difference?

Rajiv
0
Comment
Question by:rajiv061097
2 Comments
 
LVL 3

Accepted Solution

by:
LucHoltkamp earned 60 total points
ID: 1163876
If a template is compiled no code is generated. The compiler generates the code when it encounters an instance of the template and then places this code automatically in the object file.
For this reason you have to put all template definitions in the headerfiles and don't use a separate sourcefile for your definitions.
For example:

//headerfile test.hpp

// declarations
template <class T>
class Example
{
  T &doSomething();
};

// definitions
// place 'doSomething' in header also:
template <class T>
T &Example<T>::doSomething() { /* code goes here */ }

// end of header file

Good luck
.luc.

0
 

Author Comment

by:rajiv061097
ID: 1163877
Edited text of question
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question