Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't define template member function in .cpp file 2

Status
Not open for further replies.

djimm100

Programmer
May 19, 2003
8
0
0
US
For the life of me, I can't figure out why this doesn't work. I've carefully double checked my work. Here's the problem:

I have a header file that looks something like this:
Code:
  // Example.h //
  #include <iostream>
  using namespace std;

  #ifndef Example_h
  #define Example_h

  template <class Type> class Example {
  protected:
    Type t;
  public:
    Example (void) {}
    Type getT (void);
    void setT ( Type n ) { t = n; }
  };

  #endif

My source file looks something like this:
Code:
   // Example.cpp //
  #include &quot;Example.h&quot;

  template<class Type> 
  Type Example<Type>::getT (void) { return t; }
But trying to access the function I get an error message:
Code:
   // Driver.cpp //
  #include <iostream>
  using namespace std;
  #include &quot;Example.h&quot;

  void main (void)
  {
    Example<int> x;
    cout << &quot;Example x.t is &quot; << x.getT() << &quot;.\n&quot;;
  }
The error message I get (from VC++ 6.0, Service Pack 3) is:
Code:
   error LNK2001: unresolved external symbol &quot;public: int __thiscall Example<int>::getT(void)&quot;

If I define the function in the header file, either inline or externally in the same file, it works just fine.
What is my problem?
 
VC++ is well known for its poor handling of templates. You did the right thing by defining the function in the header.
 
I instead propose that you did nothing wrong with the code. However, my guess is that the cpp file is not a part of the project. Go to the file tab, expand the project in question and verify that the cpp file is indeed in the source folder. If not, just right click it and add the file. Recompile and you should be all set.

There is another alternative and that is to #include the cpp file in the header file after the class declaration. I have seen this done before but I am not sure of the repercussions.

Matt
 
Well actually it doesn't matter if Example.cpp is part of the project. The compiler wouldn't do anything with it anyway, because templates are compiled only when they are instantiated. (which is what you want to do in Driver.cpp)

That's why you MUST put the implementation of the template class members into the header file (Example.h), or into another file which you must then include into Example.h.

I hope this helps,
Tobi

 
Kudos to Matt (Zyrenthian) and Toby (LF28).
The problem can be solved in at least two ways, by putting #include &quot;Example.cpp&quot; in either the driver file or by putting it in the Example.h file after the class declaration.

I think putting it in the header is more reliable, but just to be safe I also bracketed code in the Example.cpp file with
Code:
  #ifndef Example_cpp_source
  #define Example_cpp_source

  // insert code

  #endif
Thanks, guys.
 
Hello like you walk djimm100

Do you rot me to explain like you solved the problem of the template?
I have the same problem with a class, implemented by my... I am using Visual Studio.NET AND VC++ 7.0
The error that he appears me is the following one:

fatal error C1010: unexpected end of file while looking for precompiled header directive

Do rot me to send your summarized code of like you solved the problem?

Greetings, Edwin from Argentina...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top