c类中的static const:未定义引用

c类中的static const:未定义引用,第1张

概述我只有一个本地使用的课程(即它的上课只是在它定义的c文件) 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 我只有一个本地使用的课程(即它的上课只是在它定义的c文件)
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:未定义引用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1245915.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存