1

The idea is to use a static member function to update the value of a static data member in the same class

template<typename K,typename U>
class Map{
private:
    static pair<K,U> default_value;
public:
    static void set_default(K& k,U& u){default_value=make_pair(k,u);}
};

int main(){
    int a{8};
    int b{9};
    Map<int,int>::set_default(a,b);
    return 0;
}

Here I get an error: undefined reference to Map<int,int>::default_value

Milo Lu
  • 3,176
  • 3
  • 35
  • 46

1 Answers1

3

You need to provide a definition for your default_value; outside the class, as it is odr-used

template<typename K,typename U>
class Map{
    static pair<K,U> default_value;
    // ...
};

template <typename K, typename U>
pair<K,U> Map<K, U>::default_value;

Inside of the class you are only declaring it, but not defining it. This causes a linker error when you actually try to assign to it.

Ryan Haining
  • 35,360
  • 15
  • 114
  • 174