Solved

Class Template - Linker Error

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
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 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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now