C++ Struct function body calling another structs function, same module -


i attempting make chess module using chesspiece struct , chessgame struct. using xcode 6.1.1. here header file , function in question in chess.cpp. error, "use of undeclared identifier 'initchesspiece' did mean 'chesspiece::initchesspiece'?. if make change says error, 'call non-stack member function without object argument.' finally, if make line,

game.pieces[i].initchesspiece(game.pieces[i], color, piece, x, y); 

linker throws error:

undefined symbols 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 architecture x86_64 clang: error: linker command failed exit code 1 (use -v 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; (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 cs final practice question , function headers set instructions need work way setup

you need object of type chesspiece able call initchesspiece().

something this:

#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;     (int i=0;i<32;i++) {         indata >> color >> piece >> x >> y;         game.pieces[i].initchesspiece(game.pieces[i], color, piece, x, y);     } } 

you should remove piece parameter since have object available in function through this pointer. if decide keep need make reference or pointer able change value of pieces in array.

 game.pieces[i].initchesspiece(color, piece, x, y); 

edit2: void chesspiece::initchesspiece(const string& colour, const string& name, unsigned int row, unsigned int column) { this->name = name; this->colour = colour; this->row = row; this->column = column; }

i think should consider making constructor.

edit: if want keep calling style make initchesspiece() static. should callable without object.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -