errors

why i keep getting an error
error: invalid use of non-static data member 'Wasd::height'|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
class Wasd
{
       const int height =  static_cast<char>(70) - 'A';
       const int width  =  static_cast<char>(70) - 'A';
       const char path [height][width];



   public:
    void main();
};
void Wasd::main()
{
    cout<<height;
}
int main()
{
    Wasd newWasd;
    newWasd.main();
}


try remove the error and code will works
i think its the error from the array
Last edited on
width/height could conceivably have a different value for each Wads object because they are not static. However, the size of ALL Wads objects must be fixed... so you cannot use them as array sized unless they are static.

The solution, simply, is to make them static const instead of just const:

1
2
3
       static const int height =  static_cast<char>(70) - 'A';
       static const int width  =  static_cast<char>(70) - 'A';
       const char path [height][width];

by the way thank you for your fast resply i get it now
normal members are copied for each instance of an object:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class example
{
    static int s; // static
    int ns; // nonstatic
};

int main()
{
  example foo;
  example bar;

  /* foo and bar both have their own copy of 'ns' because it is nonstatic
  
  Therefore, 'foo.ns' and 'bar.ns' are two completely separate variables.
  
  However, 's' is static, therefore there is only 1 variable and both foo
  and bar share it.
} */
Topic archived. No new replies allowed.