Arrays In Class "Invalid Use of Non-Static Data Member"

Happy Reading!

I've used this code in a previous revision where I didn't use classes. Now when I try to pass it through I'm getting:

"Invalid use of non-static data member "Dungeon::numseed"

I'm trying to declare the size of an array from inside the class, any help would be greatly appreciated.

Thanks


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  #include <iostream>
#include <string>
#include <ctime>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
// Classes
#include "Character.h"
#include "Dice.h"
#include "Enemies.h"
#include "Stats.h"
#include "Dungeon.h"
#include "Chests.h"


using namespace std;


// This Version To Have Skeletons, Stats and Classes!
int main()
{
    char name[30];

    char dice;
    int mainHP;
    int mainATK;
    string mainName;
   /*
    string step;
    string ans;
    string move = "still";
    int atk = 0;
    int hp = 0;
    int pos = 0;
    int numseed = 0;
    int gamrand;
    int gam;
    int skelehp = 0;
    int skeleatk = 0;
    int bosshp = 500;
    int bossatk = 9;
    int charP = 9;
    int bossP = 9;
    float charLife = 0;
    float bossStr = 0;
    int chests [numseed];
    srand(time(NULL)); */
//Character Constructor
  Character Info;
// Character Building
 // First Convo

    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
    cout << "Welcome Young Warrior To The 20 Step Dungeon!" << endl;
    cout << "Before We Start... What's Your Name?" << endl;
    cin.getline (name,30);
    mainName.assign(name);

 // Dice Object Construction

    Dice Random;

 // HP Sats

    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
    cout << "Press R For Your Health Points!" << endl;
    cin >> dice;
     if (dice != 'r' && dice != 'R') {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
        cout << "You've Failed Already.... NEXT!!!" << endl;
        return 0;
     }
     else {
        mainHP = Random.Hundred();
        dice = 'a';
     }
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
        cout << "Your Health Points Are: " << mainHP << endl;

 // Atk Stats

    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
    cout << "Press R For Your Attack Points!" << endl;
    cin >> dice;
     if (dice != 'r' && dice != 'R') {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
        cout << "You've Failed Already.... NEXT!!!" << endl;
        return 0;
     }
     else {
        mainATK = Random.Hundred();
        dice = 'a';
     }
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
        cout << "Your Attack Points Are: " << mainATK << endl;

// Pass Name/mainHP/mainATK Into Class

     Info.Player(mainName,mainHP,mainATK);
        cout << "Overall " << Info.getName << " Your Health Points Are " << Info.getHP << " And Your Attack Points are " << Info.getATK << "!" << endl;

// Dungeon Constructor

Dungeon Dung;

// Start Of Dungeon

       while (Dung.Pos <= 30) {
        Dung.Walk();
        if (Dung.Step != 'e' && Dung.Step != 'E' && Dung.Step != 'd') {
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
             cout << "You've Failed Already... You Fell Into a Spiky AF Pit!" << endl;
             system("pause");
             return 0;
        }
    }


return 0;
}
/*

 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);

The different color codes are

0   BLACK
1   BLUE
2   GREEN
3   CYAN
4   RED
5   MAGENTA
6   BROWN
7   LIGHTGRAY
8   DARKGRAY
9   LIGHTBLUE
10  LIGHTGREEN
11  LIGHTCYAN
12  LIGHTRED
13  LIGHTMAGENTA
14  YELLOW
15  WHITE


cin.getline (name,20);

*/


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef DUNGEON_H
#define DUNGEON_H

[code]
class Dungeon
{
    public:
        Dungeon();
        Walk();
        Events();
        int Pos = 0;
        char Step;
        int numseed;
        int NoI[numseed];

    protected:

    private:
};

#endif // DUNGEON_H


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "Dungeon.h"
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

Dungeon::Dungeon()
{
    //ctor
}

Dungeon::Walk()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
    cout << "You're Currently On Level: " << Pos << endl;
    cout << "Press E To Move Forward" << endl;
     cin >> Step;
      if (Step == 'e' || Step == 'E') {
       ++Pos;
        Step = 'd';
      }
}

Dungeon::Events()
{

    numseed = rand()%30+1;
     for (int k = 0; k < numseed; k++) {
        NoI[k] = rand()%25+1;
}
Last edited on
A big part of your problem is that arrays in C++ must have sizes that are compile time constants. If you want an array of a user defined size I recommend you use a std::vector instead. The only other alternative is to use ugly and some what dangerous manual memory allocation/deallocation.

Also why are you using C-strings instead of std::string?
Last edited on
Thanks for the reply :) I'll give vectors a go.

I was using a C string because when you would input a name like "Benjamin Barnes" it would enter "Benjamin" into the string name and "Barnes" into the next field breaking the program. I found online somewhere suggesting to use a C string.
Last edited on
I was using a C string because when you would input a name like "Benjamin Barnes" it would enter "Benjamin" into the string name and "Barnes" into the next field breaking the program.

That's probably being caused because you're using the extraction operator instead of getline(). The extraction operator stops when it encounters a whitespace character by default. This behavior is the same with C-strings and C++ strings. The getline() function by default retrieves a string until it encounters a new line character. But be aware that there are two different getline() functions, one that works with a C++ string and one that works with a C-string.

I found online somewhere suggesting to use a C string.

That's a very bad "suggestion", you should stick with the much safer C++ strings.
Topic archived. No new replies allowed.