member function not recognizing member variables

I keep getting errors saying kSize and answerArray are undeclared identifiers. Am I missing something to get the member function to recognize member variables?

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
  using namespace std;

class Colors: public Key{
public:
   Colors(){};
   Colors(int numColors, bool duplicates){
      kSize = numColors;
      cDuplicates = duplicates;
      answerArray = new char[kSize];
   };
   ~Colors(){
      delete  answerArray;
   };
   
   //virtual void getCode();
   
   void randomChar();
   
   int getNumColors(){return cNumColors;};
   bool getDuplicates(){return cDuplicates;};

protected:
   int cNumColors;
   bool cDuplicates;
   char *answerArray;
};

//member function

void randomChar(){
   char letter = 'a';
   
   srand(unsigned(time(NULL)));
   
	int randomNum = (rand()%5)+1;
   
	for(int i = 0; i < kSize; i++){
      randomNum = (rand()%5)+1;

      switch(randomNum){
         case 1:
            letter = 'R';
            break;
         case 2:
            letter = 'Y';
            break;
         case 3:
            letter = 'G';
            break;
         case 4:
            letter = 'B';
            break;
         case 5:
            letter = 'P';
            break;
      }
      answerArray[i] = letter;
   }
   
   for(int i = 0; i < 4; i++){
      cout << answerArray[i];
   }
}
Last edited on
You haven't declared randomChar() as a member function, rather as a seperate, freestanding function by itself. Try implementing it as the following:
1
2
3
void Colors::randomChar() {
    // ...
}


The Colors:: bit says that this is the randomChar function that is part of class/namespace Colors.
Last edited on
Ahhh thanks, completely forgot that!
Topic archived. No new replies allowed.