“未定义引用”错误。 (从成员函数到静态成员变量)

时间:2011-07-13 19:03:48

标签: c++ g++

我在链接时遇到以下错误。

无法从函数ClassBB :: bound()访问成员变量ClassBB :: THR。

奇怪的是,从ClassBB :: setThreshold(T v)函数来看,它与ClassBB :: bound()的条件相同,两者都是模板成员函数,成功访问了ClassBB :: THR(我是当然只讨论编译时间。)

我还是C ++的新手,特别是在模板架构方面。谢谢!

环境:通过Eclipse Indigo CDT的Ubuntu 10.10,G ++ 4.4.5

错误消息(仅链接器部分):

Building target: SampleEclipsePrj
Invoking: GCC C++ Linker
g++ -L/usr/local/lib -L/usr/lib -o "SampleEclipsePrj"  ./src/SampleEclipsePrjFinal/IntersectAngle.o ./src/SampleEclipsePrjFinal/ReadRealTime.o ./src/SampleEclipsePrjFinal/SampleEclipsePrj.o ./src/SampleEclipsePrjFinal/SampleEclipsePrjThread.o ./src/SampleEclipsePrjFinal/Slope.o ./src/SampleEclipsePrjFinal/Transform.o ./src/SampleEclipsePrjFinal/Utility.o ./src/SampleEclipsePrjFinal/main3_linux.o ./src/SampleEclipsePrjFinal/reader.o   -lcv -lcxcore -lhighgui
./src/SampleEclipsePrjFinal/SampleEclipsePrj.o: In function `ClassBB<double>::bound()':
/home/user/Documents/workspace_eclipse/SampleEclipsePrj/Includes/ClassBB.hpp:203: undefined reference to `ClassBB<double>::THR'
/home/user/Documents/workspace_eclipse/SampleEclipsePrj/Includes/ClassBB.hpp:204: undefined reference to `ClassBB<double>::THR'
/home/user/Documents/workspace_eclipse/SampleEclipsePrj/Includes/ClassBB.hpp:206: undefined reference to `ClassBB<double>::THR'
/home/user/Documents/workspace_eclipse/SampleEclipsePrj/Includes/ClassBB.hpp:207: undefined reference to `ClassBB<double>::THR'
collect2: ld returned 1 exit status
make: *** [SampleEclipsePrj] Error 1

**** Build Finished ****

注意:在Eclipse和终端上都得到了相同的结果。

代码B(剪切仅显示相关部分):

#ifndef _BBOUND_H
#define _BBOUND_H

template<class T>
class ClassBB
{
        T *pMap;
        float u1, v1, u2, v2;
        int w, h;

        float converge(float sp, float ep, float steps, float fixedPt,
                float thr);
        static T THR;

    public:
        ClassBB() :
                pMap(NULL), u1(0), v1(0), u2(0), v2(0), w(0), h(0)
        {
        }
        ClassBB(float U1, float V1, float U2, float V2);
        static void setThreshold(T v);
    int bound();
        ~ClassBB();
};

template<class T>
ClassBB<T>::ClassBB(float U1, float V1, float U2, float V2)
{// do something
}

template<class T>
void ClassBB<T>::setThreshold(T v)
{
    ClassBB::THR = v;
}

//Converge
template<class T>
float ClassBB<T>::converge(float sp, float ep, float step, float fixedPt,
        float thr)
{// do something
    return point;
}
//The bound algorithm
template<class T>
int ClassBB<T>::bound()
{
    u2 = converge(0.75, 0.5, 0.03125, v2, ClassBB::THR);
    v2 = converge(0.25, 0.0, 0.03125, u2, THR); // Both don't work.
    return 1;
}

template<class T>
ClassBB<T>::~ClassBB()
{// do something
}

#endif

Code-C(实例化类):

#ifndef _SCENE_CLASSIFIER_H
#define _SCENE_CLASSIFIER_H
#include "ClassBB.hpp"

class ClassCC
{
        ClassBB<double> bb; //Branch and Bound

    public:
        ClassCC(int W = 0, int H = 0); 
        ~ClassCC(); 
};
#endif

2 个答案:

答案 0 :(得分:7)

您必须为静态变量创建存储空间。只需添加:

template<class T> T ClassBB<T>::THR;

请注意,对于普通成员变量,只要创建模板类的新实例,就会生成存储。但是对于静态成员,应该为它们定义一个“静态”的位置,因此是明确的定义。

答案 1 :(得分:4)

您需要定义静态变量。

template<class T>
class ClassBB {
    // ...
    static T THR;
    // ...
};

template<class T>
T ClassBB<T>::THR;