Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Compiling with templates

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

856 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