Static int in class C++

Asked

Viewed 392 times

0

Hello, the following code is showing error on the right:

inserir a descrição da imagem aqui

#include <iostream>

using namespace std;

class teste {
    static int x;
    public:
        teste () {
            x++;
        }
}  t1;

int main () {

    return 0;
}

declare the variable Static int, and in the constructor method I increment +1, but the following error happens.

How to solve?

Note: In the book I’m reading the code is also like this.

1 answer

4


Static data members are stored separately, as if they were not part of the object. So they must be declared outside their class.

class teste {
    static int x;
    public:
        teste () {
            x++;
        }
}  t1;

int teste::x = 0;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.