Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Class Template - Linker Error

Posted on 1998-08-14
3
Medium Priority
?
254 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 120 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

772 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