
class A {public: static const int MY_CONST = 5;};voID fun( int b ) { int j = A::MY_CONST; // no problem int k = std::min<int>( A::MY_CONST,b ); // link error: // undefined reference to `A::MY_CONST` } 所有的代码都驻留在同一个c文件中.当在windows上编译VS时,根本没有问题.
但是,在linux上编译时,只能在第二个语句中获取未定义的引用错误.
有什么建议么?
解决方法std::min<int>的参数都是const int&(而不是int),即引用int.并且您不能传递对A :: MY_CONST的引用,因为它未定义(仅声明). 在类之外的.cpp文件中提供一个定义:
class A {public: static const int MY_CONST = 5; // declaration};const int A::MY_CONST; // deFinition (no value needed) 总结 以上是内存溢出为你收集整理的c类中的static const:未定义引用全部内容,希望文章能够帮你解决c类中的static const:未定义引用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)