Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access static members of a class?

Tags:

c++

static

I am starting to learn C++ and Qt, but sometimes the simplest code that I paste from a book results in errors.

I'm using g++4.4.2 on Ubuntu 10.04 with QtCreator IDE. Is there a difference between the g++ compiler syntax and other compilers? For example when I try to access static members something always goes wrong.

#include <iostream> using namespace std; class A {    public:       static int x;       static int getX() {return x;} }; int main() {    int A::x = 100; // error: invalid use of qualified-name 'A::x'    cout<<A::getX(); // error: : undefined reference to 'A::x'    return 0; } 

I think it's exactly the same as declared here and here (isn't it?). So what's wrong with the above code?

like image 814
sorush-r Avatar asked Nov 05 '10 09:11

sorush-r


People also ask

How do you access the static member of a class?

A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator ::. A static member function can only access static data member, other static member functions and any other functions from outside the class.

How do I access static data members?

We can access the static member function using the class name or class' objects. If the static member function accesses any non-static data member or non-static member function, it throws an error. Here, the class_name is the name of the class. function_name: The function name is the name of the static member function.

How do we access static members in Java?

Static variables can be accessed by calling with the class name ClassName. VariableName. When declaring class variables as public static final, then variable names (constants) are all in upper case. If the static variables are not public and final, the naming syntax is the same as instance and local variables.

Can you access static members from an instance of a class?

The static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created.


2 Answers

You've declared the static members fine, but not defined them anywhere.

Basically what you've said "there exists some static member", but never set aside some memory for it, you need:

int A::x = 100; 

Somewhere outside the class and not inside main.

like image 184
Flexo Avatar answered Sep 26 '22 02:09

Flexo


Section [9.4.2]

Static Data Members

The declaration of a static data member in its class definition is not a definition and may be of an incomplete type other than cv-qualified void. The definition for a static data member shall appear in a namespace scope enclosing the member’s class definition. In the definition at namespace scope, the name of the static data member shall be qualified by its class name using the :: operator

like image 42
Prasoon Saurav Avatar answered Sep 24 '22 02:09

Prasoon Saurav