pass in member arrays and comparing them

I'm trying to compare two arrays and check if they equal each other at any point in their arrays. Each array is from a different class. I get the error: invalid operands to binary expression('PlayerColor' and 'Colors'). What can I do to fix this problem? Also near the top of the code is bool correct[answer->getSize()]; is this correct?

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
//stand alone function
  bool compareKeys(Colors answer[], PlayerColors playerGuess[]){
   int totalRight = 0;
   int grab;
   bool correct[answer->getSize()]; //Can I do this? Is it actually getting the member variable kSize?
   bool correctColor[4];
   bool inList = false;
   
   for (int i = 0; i < 4; i++)
   {
      correct[i] = false;
      correctColor[i] = false;
   }
   
   //checks to see if guess matches answerKey
   for (int i = 0; i < 4; i++)
   {
      if (playerGuess[i] == answer[i])
      {
         totalRight++;
         correct[i] = true;
      }
   }
}

//Classes

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

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

////////////

class PlayerColors: public Key{
public:
   //PlayerColors(){};
   PlayerColors(){
      pGuess = new char[kSize];
   };
   ~PlayerColors(){
      delete pGuess;
   };
   
   virtual void getCode();
   void playersGuess();
   
private:
   char *pGuess;
   
   };
I get the error: invalid operands to binary expression('PlayerColor' and 'Colors')

You're trying to compare two different types of objects.

However since they both share the same base class, you can change compareKeys as follows:
 
bool compareKeys(Keys answer[], Keys playerGuess[])

This really should be a method of the Keys class (which you didn;t show).

You have some memory issues in your program:
Line 30: Your default constructor does not allocate answerArray. However you destructor at line 37 deallocates it unconditionally. This will cause a run time error attempting to release memory that is not allocated.


Also near the top of the code is bool correct[answer->getSize()]; is this correct?

No. C++ requires that the size of an array is known at compile time. You can however allocate a variable size array via new

 
 bool *correct = new bool[answer->getSize()]; 

Don't forget to deallocate it.




Ok so in my main class I make the objects Colors answer and PlayerColors playerGuess. How do I make them into Key objects, but still am able to use Colors and PlayerColors member functions?

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
#include <iostream>
#include <string>
#include "Key.h"
#include "Colors.h"
#include "PlayerColors.h"
#include "functions.h"

using namespace std;

int main(){
   //bool duplicates;
   bool allCorrect = false;

   menu();
   
   Colors answer(false);
   
   answer.gameMode();
   answer.randomChar();
   cout << endl;
   answer.getCode();
   cout << endl;
   
   while(!allCorrect){
   PlayerColors guess;
   guess.playersGuess();
   cout << endl;
   guess.getCode();
   }
      
    return 0;
}

//base class

using namespace std;

enum Mode{
   TIMED,
   LIMIT
};

const string MODE_STRINGS[3] = {"Timed", "Limited Guesses"};

class Key{
public:
   Key(){};
   //Key(){};
   ~Key(){};
   
   virtual void getCode() = 0;
   
protected:
   Mode kMode;
   int kSize;
};

//colors cpp

#include "Colors.h"
#include "Key.h"


void Colors::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 < kSize; i++){
      cout << answerArray[i];
   }
}

void Colors::getCode(){
   for(int i = 0; i<kSize; i++){
      cout << answerArray[i];
   }
}


void Colors::gameMode(){
   int mode;
   
   cout << "What level difficulty do you what to play?\n" << "Easy(1), Normal(2), Hard(3): ";
   cin >> mode;
   
   switch(mode){
      case 1:
         cout << "easy";
         kSize = 4;
         break;
      case 2:
         cout << "normal";
         kSize = 6;
         break;
      case 3:
         cout << "hard";
         kSize = 8;
         break;
   }
}
Hmmm so I tried changing the arrays to Key objects, but I now the error: Array of abstract class type "Key"
That's because line 51 is a pure virtual function. You have no implementation for it in the Keys class.

Not sure what getCode is supposed to do. In Colors, it outputs answerArray to cout.

I'm guessing that you really want the array as part of Keys. That way both Colors and PlayerColors inherit the array. You can then move getCode to the base class (Keys) and eliminate the pure virtual function.
I have to have a pure virtual function and Colors and PlayerColors are two completely different array's. one is randomly generated and the other a user inputs. I think my entire code just crumbled. :(
Colors and PlayerColors are two completely different array's. one is randomly generated and the other a user inputs

But aren't they both arrays of colors?

If not, what is the point of their inheriting from Keys?

yes they are both arrays of colors (characters), but they are created in their respective classes not in the key class because I want to be able to use their member functions. Actually Colors array is made by one of it's member function.

So for now I've ditch the virtual function until later, but am currently having a problem with comparing the two arrays since they are from different classes and making them both Key objects makes life really difficult. Don't they inherit from Key class anyway? You'd think that would make you able to compare the two derived class with each other.
What about something like this: ?
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
#include <cstdio>
using namespace std;

class Key
{  
protected:
	int ksize;
	char * carray;
	
	virtual void Initialize () = 0;  // pure virtual

public:
	Key()	// Default constructor
	{	carray = NULL;
		ksize = 0;
	}
	Key (int size)	//	Explicit constructor
	{	carray = new char[size];
		ksize = size;
		Initialize ();  // Initialize in derived class
	}
	virtual ~Key()
	{	if (carray)
			delete carray;
	}

	bool Compare (const Key & rhs) const;
	int getSize () const
	{	return ksize;
	}
};

class Colors : public Key
{
protected:
   int cNumColors;
   bool cDuplicates;

   void Initialize ();  // Colors specific initialization

public:
   Colors()
   {}
   Colors(bool duplicates,int size) : Key(size)
   {	cDuplicates = duplicates;
   }
   virtual ~Colors()
   {}

   
   void randomChar();
   void gameMode();
   
   int getNumColors()
   {	return cNumColors;
   };
   bool getDuplicates()
   {	return cDuplicates;
   };
};

class PlayerColors : public Key
{
public:
	PlayerColors(int size) : Key(size)
	{
	}
	virtual ~PlayerColors()
	{};
   
	void Initialize ();  // PlayerColors specific initialization
	void playersGuess();
}; 

bool Key::Compare (const Key & rhs) const
{	int totalRight = 0;
	bool *correct = new bool[getSize()]; 
	bool correctColor[4];
	bool inList = false;
   
	for (int i = 0; i < 4; i++)
	{	correct[i] = false;
		correctColor[i] = false;
	}
   
	//checks to see if guess matches answerKey
	for (int i = 0; i < 4; i++)
	{	if (carray[i] == rhs.carray[i])
		{	totalRight++;
			correct[i] = true;
		}
	}
	// Not sure what you want to return here
	return true;
}


Key manages the array of colors for both Colors and PlayerColor.

Compare is moved into Key so that you can compare two objects based on Key.

Initialize is a pure virtual function allowing each derived class to have its unique initialization properties.


Topic archived. No new replies allowed.