Solved

Class Template - Linker Error

Posted on 1998-08-14
3
252 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
[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
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

636 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