Solved

Compiling with templates

Posted on 1997-06-10
2
406 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now