0
class A
{
public:
static bool S(int);
static string str;
static int integer;

};

bool A::S(int)
{
   str+="A";

}

When I build the program, an error occurs: "str" undeclared identifier.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953

2 Answers2

2

Ignoring for a second that the type is undefined, you will still get this error even if you use ints only.

The error you are seeing is because you are missing the definition of your static variable. You have declared it only.

 string A::str = "initial value"; 

See: What is the difference between a definition and a declaration?

Community
  • 1
  • 1
Akusete
  • 10,704
  • 7
  • 57
  • 73
1

Probably you have more than one error, and the one you are centering your attention on is the least important:

  • ERROR line 5: string does not name a type
  • ERROR line 12: str undeclared.

The str is undefined because the type used to define it does not exist.

The solution would be to add

#include <string>
using namespace std;

at the beginning of the file (not that I recomment the using line anyway, but that's another story).

You should always center your attention into the very first error the compiler issues. But some popular IDEs out there are noteworthy for reordering them.

rodrigo
  • 94,151
  • 12
  • 143
  • 190