I keep getting the following:
[Linker error] undefined reference to `ComponentClass::POSIZIONENULLA’
ld returned 1 exit status
[Build Error] [main.exe] Error 1
can somebody help me with this. Here is my code:
ComponentClass.h
#ifndef _ComponentClass_H
#define _ComponentClass_H
template< class T>
class ComponentClass
{
public:
typedef ComponentClass* posizione;
static const posizione POSIZIONENULLA;
ComponentClass();
};
template< class T>
ComponentClass<T>::ComponentClass()
{
const posizione POSIZIONENULLA=(ComponentClass*)-1;
}
#endif
ProjectClass.h
#ifndef _ProjectClass_H
#define _ProjectClass_H
#include "ComponentClass.h"
template<class T>
class ProjectClass
{
public:
typedef typename ComponentClass<T>::posizione posizione;
ProjectClass();
posizione dummyFunction();
private:
posizione dummyposition;
};
template<class T>
ProjectClass<T>::ProjectClass()
{
dummyposition= (posizione)ComponentClass<T>::POSIZIONENULLA;
}
template<class T>
typename ProjectClass<T>::posizione ProjectClass<T>::dummyFunction()
{
posizione tempPosition;
tempPosition=(posizione)ComponentClass<T>::POSIZIONENULLA;
return tempPosition;
}
#endif
main.cpp
#include <cstdlib>
#include <iostream>
#include <string>
#include "ProjectClass.h"
using std::cout;
using std::endl;
using std::string;
int main(int argc, char *argv[])
{
ProjectClass<int> pc;
}
This code isn’t actually doing anything useful:
It’s just declaring a local variable in the constructor, initializing it and then not using it. It’s not doing anything with your
static const posizione POSIZIONENULLA.I think you meant:
That ought to fix your link error. As a side note, using
-1as a “special” pointer value isn’t a great idea, as others have pointed out in the comments above