I am attempting to make a Chess module using a ChessPiece struct and a ChessGame struct. Using XCode 6.1.1. Here is my header file and function in question in my Chess.cpp. I get the error, "Use of undeclared identifier 'initChessPiece' did you mean 'ChessPiece::initChessPiece'?. If I make that change it says the error, 'call to non-stack member function without an object argument.' Finally, if I make the line,
game.pieces[i].initChessPiece(game.pieces[i], color, piece, x, y);
Linker throws the error:
Undefined symbols for architecture x86_64: "ChessPiece::initChessPiece(ChessPiece, std::__1::basic_string, std::__1::allocator > const&, std::__1::basic_string, std::__1::allocator > const&, unsigned int, unsigned int)", referenced from: readChessGame(ChessGame&, std::__1::basic_string, std::__1::allocator > const&) in Chess.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
#ifndef CHESS_H
#define CHESS_H
#include <stdio.h>
#include <string>
using namespace std;
const int ROWS = 8;
const int COLUMNS = 8;
struct ChessPiece {
string name;
string colour;
unsigned int row;
unsigned int column;
void initChessPiece(ChessPiece, const string& colour, const string& name, unsigned int row, unsigned int column);
string getStringColourChessPiece(const ChessPiece&) const;
string getStringNameChessPiece(const ChessPiece&) const;
friend class ChessGame;
};
struct ChessGame {
unsigned int chessBoard[ROWS][COLUMNS];
ChessPiece pieces[32];
void readChessGame(ChessGame&, const string& filename);
void printChessGame(const ChessGame&);
int scoreChessGame(const ChessGame&) const;
bool isFinished(const ChessGame&) const;
};
#endif
Chess.cpp
#include "Chess.h"
void readChessGame(ChessGame& game, const string& filename) {
ifstream inData;
inData.open(filename.c_str());
string color;
string piece;
unsigned int x;
unsigned int y;
for (int i=0;i<32;i++) {
inData >> color >> piece >> x >> y;
initChessPiece(game.pieces[i], color, piece, x, y);
}
}
void initChessPiece(ChessPiece& piece, const string& colour, const string& name, unsigned int row, unsigned int column) {
piece.row = row;
piece.column = column;
piece.name = name;
piece.colour = colour;
}
This is my CS final practice question and the all the function headers were set by the instructions so I need to work with the way they are setup
initChessPiece
? – Jonathan PotterinitChessPiece(...)
? Is there some other source file you haven't told us about? And what tool are you using to attempt to build this? – BetaChessPiece
. It will be better for you to learn the language using a tutorial or text book before some of the errors you've made will make sense. You can try a basic tutorial at tutorialspoint.com/cplusplus/cpp_classes_objects.htm. – R Sahu