Undefined link to why

g ++: undefined reference to `A :: sc ', why? But the statement a = sc is ok. because of the pattern?

#include <iostream>

template<typename T>
inline const T &min(const T &left, const T &right)
{
     return (left < right ? left : right);
}

class A 
{
public:
   static const size_t sc = 0;
   A() 
    {   
      size_t tmp = 0;
      size_t a = sc; 
      size_t b = min(sc, tmp);
    }   
};

int main()
{
  A a;
  return 0;
}                                                                                    

      

+3


source to share


1 answer


If you have

static const size_t sc = 0;

      

as a member of a class, it is still a declaration. If you only use your value in your program, you do not need to define it. However, if you are using it by reference, you must define it using:

const size_t A::sc;

      

Line



  size_t a = sc; 

      

uses sc

by value, but the line

  size_t b = min(sc, tmp);

      

uses sc

the link. For this reason, it is necessary to define sc

.

+8


source







All Articles