Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
//XX.h
template <class T> class XX
{
public:
  XX();
  ~XX();
  static T* get() {//def}
  static void set(T* data){//def}
  static void init();
 private:
  static T *data;
};

//XX.C
template <class T> T* XX<T>::data = 0;
template <class T> 
void XX<T>::init()
{
  data = 0;
}

//Test.h
#include <XX.h>
class Sample{
public:
static void someFunc(int *x){XX<int>::set(x);}
};

//Test.C
int main()
{
int x = 100;
Sample::someFunc(&x);
return 0;
}



This is sample piece of code from my app, i am getting undefined reference error for XX<int>::data and XX<int>init() both. Can anybody help me resolve this error. Thanks in advance!!
Posted
Comments
Member 8576081 27-Feb-13 1:06am    
Thanks everyone for your response!!!

You can't move template code into a C file - it must be in the header file! Or at least it must be accessible from the file that uses this class (so you could in theory include the .c file along with the .h file)

The reason is that the class will be compiled only when the template gets instantiated, and at that point the file that instantiates it must have access to the full implementation.
 
Share this answer
 
your syntax error is here

C++
//XX.h
template <class t=""> class XX
{
public:
  XX();
  ~XX();
  static T* get() {//def} //here
  //solution
  static T* get() {}//def
  static void set(T* data){//def} //here
  static void init();
 private:
  static T *data;
};
 
</class>
 
Share this answer
 
Obviously you have not defined the construtor and destructor of class XX<t> anywhere.

Either remove the declaration, which will then lead to the generation of default constructors/destructor. Or define the function somewhere.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900