Reputation: 13
Hi I am attempting to create a chess board to reintroduce myself to c++. I am having trouble with adding a member function to my array that the chess board is located in. I believe I am structuring the problem incorrectly.
.cpp file:
#include "spaces.h"
#include <iostream>
char board::spaceLetter() {
return letter;
}
char board::spaceNumber() {
return number;
}
string board::getColor(board a) {
if (a.color() == true) //Also an error but not a big deal
return "black";
else
return "white";
}
void board::printBoard(board a[][8]) {
for (int i = 1; i <= 8; i++) {
for (int j = 1; j <= 8; j++) {
if (a[i][j].color() == true) { //This is where my problem is
cout << "w";
}
else
cout << "b";
}
cout << endl;
}
}
Header .h
#pragma once
#include <iostream>
using namespace std;
class board {
private:
int boardSpace[8][8];
bool color;
char number;
char letter;
public:
board(){
for (int i = 1; i <= 8; i++) {
for (int j = 1; j <= 8; j++) {
if (((i + j) % 2) == 0)
color = true; //black space
else
color = false;
}
}
}
char spaceLetter();
char spaceNumber();
string getColor(board);
void printBoard(board a[][8]);
};
Thank you!
Upvotes: 1
Views: 92
Reputation: 3186
Welcome to SO.
if (a.color() == true) //Also an error but not a big deal
color is not a function. It is a member variable. Remove the ()
from color()
.
Same mistake here:
if (a[i][j].color() == true)
Try running the code here and see if it works for you: https://rextester.com/GRG48268
Upvotes: 1