1

I have a file A.hpp as such:

class A
{
private:
   static std::string s;
public:
   void modify_string();
};

I am implementing this in a file A.cpp as such:

#include "A.hpp"

void A::modify_string()
{
s = "something";  // Error here. 
}

My main class:

int main()
{
A a;
a.modify_string();
}

I understand static variables are shared by all the class instances. I also went through this SO post where it says how to access the static member. Public static member of class . Could you please let me know where my concept is missing at?

Edit: I am getting this error: error: undefined reference to A::s

Community
  • 1
  • 1
SeasonalShot
  • 2,357
  • 3
  • 31
  • 49

3 Answers3

3

When you define:

void modify_string() {
    s = "something";  // Error here. 
}

You are creating a new function, not defining the member function modify_string of the class A. You need to do:

void A::modify_string() {

To inform the compiler that you are defining the member function modify_string for class A.


You also need a ; after your class definition.


Finally, the variable s is static so it needs to be defined seperatly somewhere so the linker can find a reference to it. So add:

 std::string A::s = "default";

This was clearly described in the link you provided for your question.


Here is a working example: http://ideone.com/iQ6Kux

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
  • Yes, i intended to write A::, however i have rectified it in my question now. – SeasonalShot May 31 '16 at 19:03
  • @SeasonalShot I added the rest of the errors for you. – Fantastic Mr Fox May 31 '16 at 19:12
  • I guess i needed to know about declaration of static vs the definition of it. i.e. "declared the static members fine, but not defined them anywhere." is what is says, besides it's not clearly defined in the link i mentioned above, as accepted answer has a link on it which is dead. – SeasonalShot May 31 '16 at 19:23
  • @SeasonalShot The link in the answer is not consequential, the answer is solid without it. – Fantastic Mr Fox May 31 '16 at 20:11
  • After digging through the cache version of the dead link, here's it is http://dietmar-kuehl.de/mirror/c++-faq/ctors.html#faq-10.12 .This explains it in much detail that is understood at a fundamental level which the author originally intended to be read. The answer in the link provides code snippets, which works but not fully understood. IMHO – SeasonalShot May 31 '16 at 20:26
  • @SeasonalShot Yep, ok, i agree, you should edit the answer to fix the link. – Fantastic Mr Fox May 31 '16 at 20:39
1

You need to reserve storage for s in exactly one compilation unit.

Do that by writing

std::string A::s;

In exactly one source file.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

Your definition void modify_string() {...} in A.cpp is not defining the member function of the class, it's defining a separate global function with the same name. You probably meant

void A::modify_string()
{
    s = "something";
}
aschepler
  • 70,891
  • 9
  • 107
  • 161