cannot appear in a constant expression?

This code is really incomplete but I'm trying to work out what's going on to fix errors before I move on. Sorry it's so long.
My teacher gave us two chunks of code, the first class and main(). We have to inherit from the class in order to complete the project. We have to drop a random number of "cookies" on a grid of dots and the user has to move their cursor around to get them.
My question has to do with my inherited class (I named it Cookies). This is just when I'm trying to get the random number of cookies. I'm getting a message that says "int rand() cannot appear in a constant expression" along with a few other messages (line 87) which I'm not sure how to fix because I've used rand() in this way before. Also, it says that "Direction" is undeclared but it's in the base class, so I thought I could use it? Or do I have inheritance wrong? (line 106)
And line 127 says I can't declare new types in a return type. and that there is an extraneous int and that main must return int.

I'm sorry I have so many questions! I'm really new at c++ and this code is nowhere near done I just need to fix the problems I have now before I can move on.

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
  //You can add or delete includes
#include <iostream>
#include <stdlib.h> //For system()
#include <conio.h> //For getche()
#include <time.h>
using namespace std;

//You can modify these numbers but don't delete these constants or this starting code will not work
const int MAX_HEIGHT = 20; //The height of the grid
const int MAX_WIDTH = 40; //The width of the grid

// DO NOT ALTER OR DELETE THIS CODE (START)!!!!!!!!!!!!!!!!!!!!!!!!!!!
/********************************************************************
 * Class: PickUpGame
 * Purpose: To store the grid and the current x and y position of the
 * user. Also has memeber functions to intialize the grid and print it.
 * Allows the user to move around the grid but provides no out of
 * bounds checking.
 ********************************************************************/
class PickUpGame
{
protected:
      char Screen[MAX_HEIGHT][MAX_WIDTH]; //The grid to print to the screen
      int xPos, yPos; //The current x and y position of the users cursor on the grid

public:
      //Constructor that will intialize the screen and x and y positions
      PickUpGame() : xPos(0), yPos(MAX_WIDTH - 1)
      {
           SetupScreen(); //Initalize the grid
      }

      //Initialize the screen with all '.' characters and set the intial user cursor position on the grid
      void SetupScreen()
      {
           for(int height = 0; height < MAX_HEIGHT; height++) {
                for(int width = 0; width < MAX_WIDTH; width++) {
                     Screen[height][width] = '.'; //Initialize each grid position
                }
           }
           Screen[xPos][yPos] = '<'; //Set the users initial cursor position
      }

      //Print the grid to the screen
      void Print()
      {
           for(int height = 0; height < MAX_HEIGHT; height++) {
                for(int width = 0; width < MAX_WIDTH; width++) {
                     cout << Screen[height][width]; //Print the character at this location in the grid
                }
                cout << endl; //After each row is printed, print a newline character
           }
      }

      //Take in user input to move around the grid
      void Move(char Direction)
      {
           switch(static_cast<int>(Direction)) //Don't know the ASCII characters for the arrow keys so use the ASCII numbers
           {
                case 72: //Up arrow
                     Screen[xPos][yPos] = ' '; //Wipe out the users current cursor
                     xPos--; //Move the users x position on the grid
                     Screen[xPos][yPos] = '^'; //Move the users cursor
                     break;
                case 80: //Down arrow
                     Screen[xPos][yPos] = ' ';
                     xPos++;
                     Screen[xPos][yPos] = 'V';
                     break;
                case 75: //Left arrow
                     Screen[xPos][yPos] = ' ';
                     yPos--;
                     Screen[xPos][yPos] = '<';
                     break;
                case 77: //Right arrow
                     Screen[xPos][yPos] = ' ';
                     yPos++;
                     Screen[xPos][yPos] = '>';
                     break;
           }
      }
};
// DO NOT ALTER OR DELETE THIS CODE (END)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

class Cookies : public PickUpGame {
      public:
      int CookieNumber = rand() %10 + 1;
      int xPos2, yPos2;
      
      void SetCookie()
      {
           for(int j=0; j<CookieNumber; j++) {
                   xPos2 = rand() %MAX_HEIGHT + 1;
                   yPos2 = rand() %MAX_WIDTH + 1;
                   }
                   Screen[xPos2][yPos2] = '0';
                   
      }
      void PrintCookie()
      {
           cout << Screen[xPos2][yPos2];
      }
      
      void CheckCursor()
      {
           if   (Direction == 72 && xPos > 0)
           {
               Move(Direction);
           }
           if   (Direction == 80 && xPos < MAX_HEIGHT)
           {
               Move(Direction);
           }
           if   (Direction == 75 && xPos > 0)
           {
               Move(Direction);
           }
           if   (Direction == 77 && xPos < MAX_WIDTH)
           {
               Move(Direction);
           }
           }
}

//You can modify and change main()
int main()
{
      PickUpGame* Game = new PickUpGame; //Create a new game object and store it in a object pointer

      char UserMove = ' '; //This is used to store the users input
      do {
           system("cls"); //Clear the screen before printing anything
           cout << "Welcome to cookie pickup. You will move to the cookies by using the arrow keys." << endl; //Program intro
           Game->Print(); //Print the grid out
           cout << "What direction would you like to move in? \n(Move using the arrow keys or type q to quit.) "; //Instructions to the user
           //UserMove = getche(); //Get one character from the user (some compilers have "getche()")
           UserMove = _getche(); //Get one character from the user (Visual Studio 2010 "_getche()" is the new version of "getche()")
           Game->Move(UserMove); //Process the users input
      } while(UserMove != 'Q' && UserMove != 'q'); //Keep running the program until the user types in a Q or q

      system("cls"); //Clear the screen
      cout << endl;
      Game->Print(); //Print the final grid out to the user
      cout << endl;

      system("PAUSE");
      return 0;
}
Last edited on
"int rand() cannot appear in a constant expression" along with a few other messages (line 87)
Move this in class constructor. You cannot initialize members with non-const expressions.

Also, it says that "Direction" is undeclared but it's in the base class,
There is no member named "Direction" in base class

line 127
You forgot to put semicolon after class declaration
I'm getting a message that says "int rand() cannot appear in a constant expression" along with a few other messages (line 87) which I'm not sure how to fix because I've used rand() in this way before.

It's a member of a class, if you want to give it a new value whenever you construct an object of that class, use a constructor, e.g.
1
2
3
4
class Cookies : public PickUpGame {
      public:
      int CookieNumber;
      Cookies() : CookieNumber(rand() %10 + 1) {}


Also, it says that "Direction" is undeclared but it's in the base class, so I thought I could use it? Or do I have inheritance wrong? (line 106)

It isn't: the base class has only three data members: Screen, xPos, and yPos. There is a function whose parameter is called "Direction", but that name is only visible within that function.

And line 127 says I can't declare new types in a return type

That's just because you forgot a semicolon after class definition, so it thinks you're defining a function with class Cookies {...} int main() { ...
Sorry-- some of c++ jargon is new to me. I understand the concepts just not the words for them.

when you say "Move this in class constructor" could you tell me what you mean? I am not sure I understand that correctly. I thought I could declare new ints in the second class.
@ Cubbi thank you, I think I understand now. And now I see what you mean about Direction. I will have to redeclare that in the derived class?
Topic archived. No new replies allowed.