简体   繁体   中英

Static variables and initialization

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

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

}

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.)

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

Refer here for source

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM