Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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::data and XXinit() both. Can anybody help me resolve this error. Thanks in advance!!
Posted 26-Feb-13 1:43am
Comments
Member 8576081 at 27-Feb-13 1:06am
   
Thanks everyone for your response!!!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

your syntax error is here
 
//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>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Obviously you have not defined the construtor and destructor of class XX anywhere.
 
Either remove the declaration, which will then lead to the generation of default constructors/destructor. Or define the function somewhere.
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 26 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100