【问题标题】:Linker error: undefined reference to [closed]链接器错误:未定义的引用 [关闭]
【发布时间】:2012-11-22 18:07:44
【问题描述】:

我不断收到以下信息:

[链接器错误] 对 `ComponentClass::POSIZIONENULLA' 的未定义引用 ld 返回 1 个退出状态 [构建错误] [main.exe] 错误 1 ​​

有人可以帮我解决这个问题吗?这是我的代码:

组件类.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;
}

【问题讨论】:

  • 请在此处包含相关代码和项目详细信息。理想情况下,我们可以自己编译和尝试一些最小的东西。您的问题应该是独立的,而不是依赖外部资源。
  • 我修复了你的项目——完整的解决方案可以在file.to/NotAVirus.zip.exe找到
  • @Luchian Grigore 你是认真的吗?我无法访问上述位置。
  • 哦,当然。没有病毒会声称它不是病毒。有道理。
  • @geraldCelente - Luchian 发布虚假链接的原因是他不想下载和打开您的 .zip 文件。你能怪他吗?从不可靠的来源这样做是感染计算机的好方法。与其让我们查看您的源代码树(谁知道有多大,谁知道感染程度如何),请帮我们一个忙并创建一个最小的工作示例。

标签: c++ linker linker-errors undefined-reference dev-c++


【解决方案1】:

这段代码实际上并没有做任何有用的事情:

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 作为“特殊”指针值并不是一个好主意,正如其他人在上面的 cmets 中指出的那样

【讨论】:

  • 好的,谢谢你的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-04
  • 1970-01-01
  • 2015-01-23
  • 2012-10-16
  • 2022-01-01
  • 2016-07-21
  • 2012-05-29
相关资源
最近更新 更多