我有一个仅供本地使用的类(即,它的对应只是它定义的 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 上编译时,我得到 undefined reference
仅针对第二个语句的错误。
有什么建议么?
原文由 user2379182 发布,翻译遵循 CC BY-SA 4.0 许可协议
std::min<int>
’s arguments are bothconst int&
(not justint
), ie references toint
.而且您不能传递对A::MY_CONST
的引用,因为它 _没有定义_(仅 _声明_)。在类外的
.cpp
文件中提供定义: