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

x
?
Solved

Class Template - Linker Error

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 clear a vector as well as how to detect empty vectors in C++.

688 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