1

Item.h:

class Item { private: static const int price = 5; public: static const int& getPrice(); }; 

Item.cpp:

const int& Item::getPrice(){ return price; } 

main.cpp:

 int main() { Item item; cout << item.getPrice() << endl; } 

Error: undefined reference to 'Item::price'

Why is this?

3
  • 1
    Static members do not belong to objects. Item::getPrice() should work. Commented Apr 5, 2021 at 14:23
  • Why not static const int getPrice();? Commented Apr 5, 2021 at 14:23
  • You can also use just const int price = 5; it doesn't need to be a static class variable as far I can see. Commented Apr 5, 2021 at 14:25

0