I'm trying to make the Memory game Concentration (pick two cards from a board, if they are a match good, if not, turn them back over). I'm getting an error (bottom of post) and I'm not sure why or how to fix it.
Card.h
#pragma once
#include "stdafx.h"
usingnamespace System;
class Card
{
public:
Card(); //default constructor
Card(Card &inCard); //copy constructor
Card operator=(Card &inCard); //overload assigment operator
Card operator==(Card &inCard); //overload == operator
Card makeCard(int inputNum, char inputColor); //makes a card with the specified color and number
void turnover(); //turns a card over, either face down to up or face up to down
bool isFaceUp(); //returns true if card is face up, false if card is face down
int myNumber(); //returns the number on the card
char myColor(); //returns the color of the number on the card
void display(); //displays the number on the card's face and it's color
void setFace(char inputFace);
void setNum(int inputNum);
void setColor(char color);
private:
char face; //card is face up (U) or face down (D)
int num; //number on the card, 1-9
char color; //color of the number on the card either Red (R) or Blue (B) or Default (D)
};
#pragma once
#include "stdafx.h"
#include "Card.h"
usingnamespace System;
class Board
{
public:
Board(); //default constructor, 6x6 grid of cards with 0
Board(Board &inBoard); //copy constructor
~Board(); //destructor
void Deal(); //places cards randomly on the grid in all spaces
int getFaceDown(); //gets the number of cards that are face down
void turnOver(int x,int y); //turns over a card at a position x y
void printBoard(); //prints the board showing what cards are faceup and facedown
void printAll(); //prints all cards as with values showing for debugging
Board operator=(Board &inBoard); //overload assignment operator
private:
int faceDownNum; //number of face down cards
Card board[6][6]; //array of cards that make up the board
};
while(true)
{
int x,y,x1,y1;
Board gameBoard;
gameBoard.Deal();
gameBoard.printBoard();
cout<<"Pick location 1 in the form x y"<<endl;
cin>>y>>x;
cout<<"Pick location 2 in the form x y"<<endl;
cin>>y1>>x1;
cout<<"Location 1 is a"<<gameBoard[x][y].display();
cout<<"Location 2 is a"<<gameBoard[x1][y1].display();
if((gameBoard[x][y].myNumber()==gameBoard[x][y].myNumber()) &&(gameBoard[x][y].myColor()==gameBoard[x][y].myColor()))
{
cout<<"Those two cards are a match."<<endl;
gameBoard[x][y].turnover();
gameBoard[x1][y1].turnover();
gameBoard.printBoard();
}
else
{
cout<<"Those two are not a match"<<endl;
}
if(getFaceDown==0)
{
goto gameFinish;
}
}
The error I can't figure out is in the main, everytime I use gameBoard[][] I get these errors (line 39 is the first time I use gameBoard[][].display() )
1>Concentration.cpp(39): error C2676: binary '[' : 'Board' does not define this operator or a conversion to a type acceptable to the predefined operator
1>Concentration.cpp(39): error C2228: left of '.display' must have class/struct/union
If anyone can help fix this or explain where the error is coming from I would appreciate it, thanks.
It should be gameBoard.board[x][y].display(); instead. That's still going to cause errors though since the board member of class Board is private, so it's not directly accessible in the main().
Also, you probably meant to use x1 and y1 in a couple of places on line 13 in that last chunk of code.
Protected isn't good enough for access in main(). You could make it public or provide a Card getCard(int x, int y)const{ return board[x][y];} function in the public section of the Board class.
EDIT: In this case the call would be: gameBoard.getCard(x,y).display();