Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Compiling with templates

Posted on 1997-06-10
2
Medium Priority
?
430 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
[X]
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
2 Comments
 
LVL 3

Accepted Solution

by:
LucHoltkamp earned 120 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

670 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