0

私は次のことを続けています:

[リンカーエラー]`ComponentClass::POSIZIONENULLA'への未定義の参照が1つの終了ステータスを返しました[ビルドエラー][main.exe]エラー1

誰かがこれを手伝ってくれる?これが私のコードです:

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;
}
4

1 に答える 1

2

このコードは実際には何の役にも立ちません。

template< class T>
ComponentClass<T>::ComponentClass()
{
    const posizione POSIZIONENULLA=(ComponentClass*)-1;
}

コンストラクターでローカル変数を宣言し、初期化してから使用しないだけです。それはあなたと何もしていませんstatic const posizione POSIZIONENULLA

私はあなたが意味したと思います:

template< class T>
const typename ComponentClass<T>::posizione ComponentClass<T>::POSIZIONENULLA=(ComponentClass*)-1;

これでリンクエラーが修正されるはずです。-1ちなみに、上記のコメントで他の人が指摘しているように、「特別な」ポインタ値として使用することは良い考えではありません。

于 2012-12-05T02:22:11.657 に答える