ok here issue, have global array declared in class. use function in class assign values array, values passed argument function. (this works) have function in same class array , first function, called class; when called other class global array zero'd out.
this picture actual issue in function not working
here function saves data array, can see works
here code example:
// // player.cpp // battleship // // created cody flies on 5/25/16. // copyright © 2016 cody flies. rights reserved. // #include "player.hpp" #include <string.h> #include "utility.hpp" std::string player::getname(){ return name; } void player::setname(std::string name){ this->name = name; } int player::getnumbershipsleft(){ return numbershipsleft; } void player::setnumberofshipsleft(int numberofshipslost){ this->numbershipsleft -= numberofshipslost; } /// set location of ships , store them in players variables void player::setcarrierlocation(int x, int y, int direction){ carrierlocation[0] = x;// <5 x values carrierlocation[5] = y;// >= 5 y values if(direction == 1){ for(int = 0; i<5; i++){ carrierlocation[i] = i+x; } for(int = 5; i<10; i++){ carrierlocation[i] = y; } } if(direction == 0){ for(int = 5; i<10; i++){ carrierlocation[i] = i+y-5; } for(int = 0; i<5; i++){ carrierlocation[i] = x; } } } void player::printcontents(){ (auto : carrierlocation) std::cout<<i<<std::endl; } /* void setcruiserlocation(int pos[10][10]); void setdestroyerlocation(int pos[10][10]); void setpatrolboatlocation(int pos[10][10]);*/ /// location of ships bool player::iscarrierhit(int x, int y){ for(int = 0; i<10; i++){ for(int j = 0; j<10; j++){ if(x==carrierlocation[i]&&y==carrierlocation[j]) return true; /// carrierlocatio /// not holding info. } } return false; } /*int* getcruiserlocation(){ } int* getdestroyerlocation(); int* getpatrolboatlocation();*/
and here player.hpp--->
class player { /// variables name, shipsleft private: std::string name; int numbershipsleft; //int cruiserlocation[1]; int carrierlocation[10]; /*int destroyerlocation[10][10]; int patrolboatlocation[10][10]; */ /// getters , setters name , shipsleft /// public: std::string getname(); void setname(std::string); int getnumbershipsleft(); void setnumberofshipsleft(int); /// set location of ships , store them in players variables void setcarrierlocation(int, int, int);/* void setcruiserlocation(int pos[10][10]); void setdestroyerlocation(int pos[10][10]); void setpatrolboatlocation(int pos[10][10]);*/ /// location of ships bool iscarrierhit(int, int);/* int* getcruiserlocation(); int* getdestroyerlocation(); int* getpatrolboatlocation();*/ void printcontents(); }; #endif /* player_hpp */
and here object called.
// // board.cpp // battleship // // created cody flies on 5/25/16. // copyright © 2016 cody flies. rights reserved. // #include "board.hpp" #include <string.h> #include <iostream> #include "player.hpp" using namespace std; void board::createboard(){ int shipplacement, startcol, startrow, shipsize; char matrix[10][10]; //call method user information , set ship locations userinput(); startcol = 2; startrow = 3; shipsize = 5; string value; for(int row = 0; row<10; row++){ for(int column = 0; column<10; column++){ matrix[row][column] = 126; if(player1.iscarrierhit(row, column)){ matrix[row][column] = 'a'; } } } string space = " "; cout << " 1 2 3 4 5 6 7 8 9 10" << endl; for(int = 0; i<10; i++){ if(i == 9){ space = " "; } cout << i+1 << space; for(int j = 0; j<10; j++){ cout << matrix[i][j] << " "; } cout << endl; } player1.printcontents(); } void board::userinput(){ string playername; cout << "how many players? " << endl; int players; cin >> players; if(players == 1){ player2.setname("computer"); cout << "what name" << endl; cin >> playername; player1.setname(playername); placeships(player1); } else if(players == 2){ cout << "what name player 1" << endl; cin >> playername; player1.setname(playername); cout << "what name player 2" << endl; cin >> playername; player2.setname(playername); placeships(player1); placeships(player2); } cout << "your name is: " << player1.getname() << " , opponents name is: " << player2.getname() << endl; } void board::placeships(player player){ int coord[2]; int direction; cout << "what coordinates carrier start"<<endl; cin >> coord[0] >> coord[1]; cout << "what direction ship 0 vertical 1 horizontal" << endl; cin >> direction; player.setcarrierlocation(coord[0], coord[1], direction); } board::board(){ }
and board.hpp
// // board.hpp // battleship // // created cody flies on 5/25/16. // copyright © 2016 cody flies. rights reserved. // #ifndef board_hpp #define board_hpp #include <stdio.h> #include "utility.hpp" #include "player.hpp" class board { private: player player2, player1; public: // method create board void createboard(); bool isship(char, char, char, char); void placeships(player); int attachship(int, int, int, int); void userinput(); board(); }; #endif /* board_hpp */
and main file
// // main.cpp // battleship // // created cody flies on 5/25/16. // copyright © 2016 cody flies. rights reserved. // #include <iostream> #include "board.hpp" #include "player.hpp" using namespace std; int main(int argc, const char * argv[]) { //run battlespace class board board; board.createboard(); //carrier 5 //battleship 4 //crusier 3 //sub 3 //destroyer 2 return 0; }
this code code have used in xcode.
aside other issues code, problem stems term "global array" misleading.
class { int array[10]; };
array
here member variable, every instance of class has it's own discrete copy of array
. not global, not shared. result, here problem:
void board::placeships(player player){
placeships
takes player value meaning makes copy. changes made player
affect copy of player until end of function @ point instance called player
destroyed, leaving player
passed in unaffected.
what need here pass reference, because want change instance being called with:
void board::placeships(player& player){
practical demonstration:
#include <iostream> int fvalue(int i) { // ia local variable = 10; } // destroyed int fref(int& i) { = 42; } int main() { int = 1; std::cout << "main:i=" << << "\n"; fvalue(i); std::cout << "after fvalue i=" << << "\n"; // unchanged fref(i); std::cout << "after fref i=" << << "\n"; // prints 42 }