Solved

Compiling with templates

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

910 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

22 Experts available now in Live!

Get 1:1 Help Now