Solved

Class Template - Linker Error

Posted on 1998-08-14
3
250 Views
Last Modified: 2011-10-03
Hi,
I wrote a code in c++, using a template class.
When I put all function and declaration in one file (no project) it was just fine. But when I divided it to few files (using project) I had a linker error:
"undefined symbol a<int>::show_num() in module appl.cpp"
when "a" is the class template, and "show_num()" is a public function in it.
the same goes very well when I don't use the template.

Any idea ????????

please if can add an example.
Thank you.
0
Comment
Question by:s_lavie
3 Comments
 
LVL 3

Accepted Solution

by:
Norbert earned 30 total points
ID: 1170264
templates must have global scope.
You can't split the functions of a template class to a cpp file
Templates must be inside a header file and all cpp files that uses the templates must include the header file
example
//Templ.h
template<class T> class CA
{
public:
          T SetValue(T Value);
          T GetValue();
private:
          T m_Value;
}
template <class T> T CA<T>::GetValue()
{
      return m_Value;
}
template <class T> T CA<T>::SetValue(T NewValue)
{
                T OldValue=m_Value;
                 m_Value=NewValue;
      return OldValue;
}
// end of templ.h
Somefile.cpp
#include "Templ.h"
void Something()
{
          CA<int> MyInt;
          CA<char *> MyChar;
          char* Val;
          MyInt.SetValue(5);
          if(5!=MyA.SetValue(6))
              Error
          MyChar.SetValue(new char[20]);
           Val=MyChar.GetValue();
           strcpy(Val,"This is a Test");
           printf(MyChar.GetValue());
           delete[] SetValue(NULL);
}

This should work. I wrote it on the fly so maybe there are some typos
0
 
LVL 22

Expert Comment

by:nietod
ID: 1170265
>> You can't split the functions of a template class to a cpp file
You can, but only if it will be used in only that .cpp file.   If a template is used by multiple .cpp files, but the entire template in a .h file.  If the template is useed on only one .cpp file, you can place it in the .cpp file  (In that case, I would put everything for the template in the ,cpp file, you wouldn't want a .h for that.)
0
 
LVL 2

Author Comment

by:s_lavie
ID: 1170266
Adjusted points to 30
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 62
Best book to learn C++ 4 84
What is sub-make ? 2 71
Why  my code (program) build with old compiler? 11 75
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…
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 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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

840 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