1

I am trying to reach a static variable declared in MyClass.h from MyClass.cpp. But I get following errors. I made a research but still have no clue why my code does not compile. I use visual studio 2013.

MyClass.h

#ifndef __MyClass_h_
#define __MyClass_h_
class MyClass {
static int x;
public:
static int y;   
};
#endif

MyClass.cpp

#include "MyClass.h"
void MyClass::sor() (const string& var1, const unsigned count) const {
    // What goes here? See below for what I have tried
}

So, if I use:

int MyClass::x=8;

This says int MyClass::x redefinition and MyClass::x 'MyClass::x' : definition or redeclaration illegal in current scope

If I use:

    MyClass::x=8;

This gives the error 1 unresolved external.

If I use:

    MyClass::y=8;

This also gives the error 1 unresolved external.

If I use:

    int MyClass::y=8;

This says int MyClass::y redefinition and 'MyClass::y' : definition or redeclaration illegal in current scope

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • 2
    Indenting your code makes it more readable. – aportr Apr 20 '15 at 02:47
  • 1
    Welcome to Stack Overflow! I have heavily edited your code to make it more readable and make it easier for people to see what you have tried; you have made a good attempt at diagnosing the issue, so with a clearer layout hopefully people can address your issues more easily. – Ken Y-N Apr 20 '15 at 03:22
  • 2
    It sounds like you are writing `int MyClass::x=8;` inside a function, when in fact it should go outside a function – M.M Apr 20 '15 at 06:26

4 Answers4

1

You need to understand you don't have a static variable in a header, how other answers suggest. You have a static member of a class, which is perfectly fine.

In order to access it you write: MyClass::x. You need to initialize it also.

Unrelated to the static member, you need to declare the method also:

header:

#ifndef __MyClass_h_
#define __MyClass_h_
class MyClass {
  static int x;
public:
  static int y;   

  void sor() (const string& var1, const unsigned count) const;
};
#endif

source file:

#include "MyClass.h"
int MyClass::x = 0; // intialization

void MyClass::sor() (const string& var1, const unsigned count) const {
    MyClaxx::x = 11; // access it

}
bolov
  • 72,283
  • 15
  • 145
  • 224
0

You have a declaration for the static variable but don't have the definition for it. In order to use static class members you should define them in the corresponding translation unit. You can't do it in the header file because it will violate ODR(one definition rule) so you should define it in the .cpp file to confrom the aforementioned rule.

So you have to put int MyClass::x = 0; in your cpp file in global scope(or under the namespace if you have one) to get it working. Note, that you could use whatever value insted of 0 or even didn't provide any(in this case it will be 0 anyway, due to special treatment of global(static) variables.)

ixSci
  • 13,100
  • 5
  • 45
  • 79
-1

When static variable is declared in a header file is its scope limited to .h file or across all units.

Refer here for source

Community
  • 1
  • 1
Mark
  • 564
  • 4
  • 12
-1

This is simple. When you declare a static variable in a header file, it's scope is limited to header file. When you going to use that static variable in a .cpp file you getting an error like this. It is because you didn't give the definition of the static variable. So in any of the .cpp file you need to give the definition of static variable. ex :

.h file

class MyClass {
static int x;
.....
}

Top of the .cpp file you should define the static variable like this.

#include "MyClass.h"
int MyClass::x = 0 ;

void MyClass::sor() (const string& var1, const unsigned count) const {
    MyClass::x = 8 ;  // you can access without any issue
}
ANjaNA
  • 1,404
  • 2
  • 16
  • 29
  • Is there a difference between int MyClass::x = 0 ; int MyClass::x = 8; as =8 is giving him error – Vinay Shukla Apr 20 '15 at 04:25
  • 1
    oh, you misunderstand my point. You need to add this line at the top of the .cpp file. I'll correct it in my answer. – ANjaNA Apr 20 '15 at 06:14
  • 1
    "it's scope is limited to header file" that's not true. Try to use this variable in the header file and you will get the same error. Header file is just a part of the translation unit. – ixSci Apr 20 '15 at 06:23
  • I mean static variables can be access without any issue if the function has implementation on the same file. The issue is happen when implementation goes to some other .cpp file and then compiler don't know any definition of this variable. So we can give a definition on a .cpp file. – ANjaNA Apr 20 '15 at 06:35