static const in c++ class: undefined reference

后端 未结 5 882
Happy的楠姐
Happy的楠姐 2021-02-18 21:17

I have a class for local use only (i.e., its cope is only the c++ file it is defined in)

class A {
public:
    static const int MY_CONST = 5;
};

void fun( int b         


        
5条回答
  •  无人及你
    2021-02-18 21:49

    std::min's arguments are both const int&(not just int), i.e. references to int. And you can't pass a reference to A::MY_CONST because it is not defined (only declared).

    Provide a definition in the .cpp file, outside the class:

    class A {
    public:
        static const int MY_CONST = 5; // declaration
    };
    
    const int A::MY_CONST; // definition (no value needed)
    

提交回复
热议问题