How to declare a variable in a class, and be able to use it w/ every function in that class?

For simplicity sake, I am trying to declare

char x = 'X'.

in a private section of my class.

I cannot do this, because I cannot declare a variable in the header file. How do I make char x = 'X' for every function in the class?
Last edited on
Set the value of x in the constructor(s).
Last edited on
try declaring it as:
const char x='X'

idk if it will work, but if it doesn't just add it to the constructor
.nevermind, it did not work Also note the single quotes.
Last edited on
Peter, that is not working. Here is my code:

Player.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef PLAYER_H
#define PLAYER_H


class Player
{
    public:
        Player();

        void ex();

    protected:
    private:
        char x;

};

#endif // PLAYER_H


Player.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include "Player.h"
using namespace std;

Player::Player()
{
    char x = 'X';
}

void Player::ex()
{
    cout << x;
}


Main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "Player.h"
using namespace std;

int main()
{
Player bob;
bob.ex();

    return 0;
}


It is displaying nothing.
Script, sorry I meant single quoted. But if I try to declare

const int x = 'X'

in the header it says

error: ISO C++ forbids initialization of member 'x'


and if I do this:

Header
1
2
private:
const char x;


.cpp
1
2
3
4
(CONSTRUCTOR)
{
const char x = 'X';
}


it gives me

error: uninitialized member 'Player::x' with 'const' type 'const char'|
Last edited on
You declare a local variable x in the constructor that will be destroyed at once after executing of the constructor. You should initialize data member of the class x.

1
2
3
4
Player::Player()
{
    x = 'X';
}


Or you can initialize it in mem initializer list

Player::Player() : x( 'X' ) {}



The class definition

1
2
3
4
5
6
7
8
9
10
11
12
class Player
{
    public:
        Player();

        void ex();

    protected:
    private:
        char x;

};
Last edited on
Vlad, you mean make another class just for setting char x = 'X'?
I mean your class because you are changing it every time.
Last edited on
Ah I forgot about the member initializer list. Thank you!

Is this the only way to declare variables that can be used throughout the entire class?
You can declare a global variable but it is a bad practice.
Ok thank you very much!
If your variable is a constant then your can declare it as a static constant variable in a class.

1
2
3
4
5
6
7
8
9
10
11
12
class Player
{
    public:
        Player();

        void ex();

    protected:
    private:
       static const  char x = 'X';

};


In this case you need not to initialize it in the constructor.
That works too. Thank you!

Is there a way to do this with an array?
Yes, you can to do that with an array.
How? I am getting:

error: a brace-enclosed initializer is not allowed here before '{' token

1
2
3

    private:
        static const char x[1] = {'X', 'Y'};
For example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

struct A
{
   int a[3] = { 1, 2, 3 };
};

int main()
{
   A a;
   for ( auto x : a.a ) std::cout << x << ' ';
   
   return 0;
}


However not all compilers support this code.

In your code example the array shall be defined as having two elements and with specifier constexpr

static constexpr char x[2] = {'X', 'Y'};
Last edited on
Ok this is great! Thanks!
Topic archived. No new replies allowed.