类中静态变量未定义导致undefined reference to static class member问题的解决方法
来源:互联网 发布:淘宝店保证金怎么退回 编辑:程序博客网 时间:2024/05/20 06:07
undefined reference to ***这个链接错误的花样总是层出不穷(more),这一次是找不到类中的成员。
例子1:undefined reference to VS. 类静态成员变量
在文件A.h中声明了类A与类B:
class A
public:
void funB();//funB()用到 A::pa;
}
问题:
gcc返回链接错误:undefined reference to 'A::pa'。然而,把类B中的pa声明为非static变量,则可以通过编译。
WHY:
先复习一下static data members in class。
[1] a single piece of storage for a static data member, regardless of how many objects of that class you create.
[2] the static data belongs to the class. Its name is scoped inside the class and all objects share that data member.
[3] NOTE: The linker will report an error if a static data member is declared but not defined.
所以,问题就出在:在class中,无法对static data member进行复制,即便是在构造函数中对static data member进行赋值,linker还是会报错。
因为static data member不属于任何一个对象,所以即便是在创建对象的时候进行赋值,也只能说明,这个对象对这个data member重新赋值而已。
因此,这里的undefined reference to ***,是找不到类静态成员变量的定义。
解决方法:
The definition must occur outside the class (no inlining is allowed), and only one definition is allowed.
It is common to put it in the implementation file for the class.
其实,也就是在全局的地方对静态成员变量赋值。
在上面的例子中,可在文件A.cpp中在其他函数定义之外,包括在构造函数之外,加上pa的定义,此时不需要加static前缀,但要加类名限定,也可以不赋值。
例子1:undefined reference to VS. 类静态成员变量
在文件A.h中声明了类A与类B:
class A
{
friend class B;
staticint pa; // 注:这里把成员变量声明为static
}
class B
{public:
void funB();//funB()用到 A::pa;
}
问题:
gcc返回链接错误:undefined reference to 'A::pa'。然而,把类B中的pa声明为非static变量,则可以通过编译。
WHY:
先复习一下static data members in class。
[1] a single piece of storage for a static data member, regardless of how many objects of that class you create.
[2] the static data belongs to the class. Its name is scoped inside the class and all objects share that data member.
[3] NOTE: The linker will report an error if a static data member is declared but not defined.
所以,问题就出在:在class中,无法对static data member进行复制,即便是在构造函数中对static data member进行赋值,linker还是会报错。
因为static data member不属于任何一个对象,所以即便是在创建对象的时候进行赋值,也只能说明,这个对象对这个data member重新赋值而已。
因此,这里的undefined reference to ***,是找不到类静态成员变量的定义。
解决方法:
The definition must occur outside the class (no inlining is allowed), and only one definition is allowed.
It is common to put it in the implementation file for the class.
其实,也就是在全局的地方对静态成员变量赋值。
在上面的例子中,可在文件A.cpp中在其他函数定义之外,包括在构造函数之外,加上pa的定义,此时不需要加static前缀,但要加类名限定,也可以不赋值。
A* B::pa;
0 0
- 类中静态变量未定义导致undefined reference to static class member问题的解决方法
- C++静态方法调用静态变量报错:undefined reference to 'namespace::class::field'
- linux c/c++ undefined reference to 'pthread_create' 未定义pthread_create问题
- g++编译时对'xxxx'未定义的引用问题(undefined reference to)
- gcc/g++ undefined reference to function() 问题的解决方法
- undefined reference to `vtable for’”问题的原因及解决方法
- 单例模式中 undefined reference to `XXX' 的解决方法
- "undefined reference to" 问题汇总及解决方法
- C++类模板中静态成员变量的隐式实例化问题(Implicit initialization of static member variables for template classes)
- undefined reference to错误的解决方法
- undefined reference to `img_convert’的解决方法
- undefined reference to `ceilf'的解决方法
- undefined reference to错误的解决方法
- 关于将undefined reference to `*'的解决方法
- undefined reference to 'inflate' 错误的解决方法
- undefined reference to错误的解决方法
- undefined reference to错误的解决方法
- undefined reference to 错误的解决方法总结
- Android-数据格式解析对象JSON用法
- 高斯滤波及canny边缘检测代码实现
- 关于Java内存泄漏
- 第二周项目三——时间类(2)
- 报到
- 类中静态变量未定义导致undefined reference to static class member问题的解决方法
- Jave程序和C语言的区别
- C++之多态性与虚函数
- Android检查用户是否有网络工具类
- 第二周项目二——长方柱
- javascript 闭包
- 后台多任务多线程断点下载
- 字符串排序输出
- 对Java与C语言的简单对比