Global Array in a C++ Object
<p>Ok so here is my issue, I have a global array declared in a class. I use a function in that class to assign values to the array, values which are passed as an argument to that function. (this works) I then have another function which is in the same class as the array and the first function, but is called from another class; but when it is called from the other class the global array is all zero'd out. </p>

<p><a href="http://i.stack.imgur.com/pRgs7.png" rel="nofollow">This is a picture to the actual issue in the function that is not working</a></p>

<p><a href="http://i.stack.imgur.com/w8VNZ.png" rel="nofollow">Here is the function that saves the data to the array, as you can see it works</a></p>

<p>Here is the code as an example:</p>

<pre><code>    //
    //  Player.cpp
    //  Battleship
    //
    //  Created by Cody Flies on 5/25/16.
    //  Copyright © 2016 Cody Flies. All rights reserved.
    //

    #include "Player.hpp"
    #include &lt;string.h&gt;
    #include "Utility.hpp"


    std::string Player::getName(){
        return name;
    }
    void Player::setName(std::string name){
        this-&gt;name = name;
    }
    int Player::getNumberShipsLeft(){
        return numbershipsleft;
    }
    void Player::setNumberOfShipsLeft(int numberOfShipsLost){
        this-&gt;numbershipsleft -= numberOfShipsLost;
    }
    /// set the location of the ships and store them in the players        variables
    void Player::setCarrierLocation(int x, int y, int direction){
        carrierLocation[0] = x;// &lt;5 is x values
        carrierLocation[5] = y;// &gt;= 5 is y values
        if(direction == 1){
            for(int i = 0; i&lt;5; i++){
                carrierLocation[i] = i+x;
            }
            for(int i = 5; i&lt;10; i++){
                carrierLocation[i] = y;
            }
        }
        if(direction == 0){
            for(int i = 5; i&lt;10; i++){
                carrierLocation[i] = i+y-5;
            }
            for(int i = 0; i&lt;5; i++){
                carrierLocation[i] = x;
            }
        }
    }

    void Player::printContents(){
        for (auto i : carrierLocation) std::cout&lt;&lt;i&lt;&lt;std::endl; }

    /*
    void setCruiserLocation(int pos[10][10]);
    void setDestroyerLocation(int pos[10][10]);
    void setPatrolBoatLocation(int pos[10][10]);*/
    /// get the location of the ships
    bool Player::isCarrierHit(int x, int y){
        for(int i = 0; i&lt;10; i++){
            for(int j = 0; j&lt;10; j++){
                if(x==carrierLocation[i]&amp;&amp;y==carrierLocation[j])
                    return true; /// this is where the carrierLocatio
                                 /// is not holding the info.
            }
        }
        return false;
    }

    /*int* getCruiserLocation(){
    }
    int* getDestroyerLocation();
    int* getPatrolBoatLocation();*/
</code></pre>

<p>and here is the Player.hpp---></p>

<pre><code>    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 and setters for name and shipsleft
    ///
    public:

        std::string getName();
        void setName(std::string);
        int getNumberShipsLeft();
        void setNumberOfShipsLeft(int);
        /// set the location of the ships and store them in the 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]);*/
        /// get the location of the ships
        bool isCarrierHit(int, int);/*
        int* getCruiserLocation();
        int* getDestroyerLocation();
        int* getPatrolBoatLocation();*/
        void printContents();




    };

    #endif /* Player_hpp */
</code></pre>

<p>and here is where the object is called.</p>

<pre><code>    //
    //  Board.cpp
    //  Battleship
    //
    //  Created by Cody Flies on 5/25/16.
    //  Copyright © 2016 Cody Flies. All rights reserved.
    //

    #include "Board.hpp"
    #include &lt;string.h&gt;
    #include &lt;iostream&gt;
    #include "Player.hpp"
    using namespace std;


    void Board::createBoard(){
        int shipPlacement, startCol, startRow, shipSize;
        char matrix[10][10];
        //call method to get user information and set ship locations
        userInput();


        startCol = 2;
        startRow = 3;
        shipSize = 5;
        string value;

        for(int row = 0; row&lt;10; row++){
            for(int column = 0; column&lt;10; column++){
                matrix[row][column] = 126;
                if(player1.isCarrierHit(row, column)){
                    matrix[row][column] = 'A';
                }
            }
        }
        string space = "  ";
        cout &lt;&lt; "   1 2 3 4 5 6 7 8 9 10" &lt;&lt; endl;
        for(int i = 0; i&lt;10; i++){
            if(i == 9){
                space = " ";
            }
            cout &lt;&lt; i+1 &lt;&lt; space;
            for(int j = 0; j&lt;10; j++){
                cout &lt;&lt; matrix[i][j] &lt;&lt; " ";
            }
            cout &lt;&lt; endl;
        }

        player1.printContents();
    }

    void Board::userInput(){
        string playername;
        cout &lt;&lt; "how many players? " &lt;&lt; endl;
        int players;
        cin &gt;&gt; players;
        if(players == 1){
            player2.setName("Computer");
            cout &lt;&lt; "what is your name" &lt;&lt; endl;
            cin &gt;&gt; playername;
            player1.setName(playername);
            placeShips(player1);

        }
        else if(players == 2){
            cout &lt;&lt; "what is your name player 1" &lt;&lt; endl;
            cin &gt;&gt; playername;
            player1.setName(playername);
            cout &lt;&lt; "what is your name player 2" &lt;&lt; endl;
            cin &gt;&gt; playername;
            player2.setName(playername);
            placeShips(player1);
            placeShips(player2);
        }

        cout &lt;&lt; "your name is: " &lt;&lt; player1.getName() &lt;&lt; " and your opponents name is: " &lt;&lt; player2.getName() &lt;&lt; endl;

    }

    void Board::placeShips(Player player){
        int coord[2];
        int direction;
        cout &lt;&lt; "what coordinates would you like your Carrier to start"&lt;&lt;endl;
        cin &gt;&gt; coord[0] &gt;&gt; coord[1];
        cout &lt;&lt; "what direction would you like your ship 0 for vertical 1 for horizontal" &lt;&lt; endl;
        cin &gt;&gt; direction;
        player.setCarrierLocation(coord[0], coord[1], direction);


    }

    Board::Board(){
    }
</code></pre>

<p>and Board.hpp</p>

<pre><code>    //
    //  Board.hpp
    //  Battleship
    //
    //  Created by Cody Flies on 5/25/16.
    //  Copyright © 2016 Cody Flies. All rights reserved.
    //

    #ifndef Board_hpp
    #define Board_hpp

    #include &lt;stdio.h&gt;
    #include "Utility.hpp"
    #include "Player.hpp"

    class Board
    {
    private:
         Player player2, player1;
    public:
    // a method to create the board
        void createBoard();
        bool isShip(char, char, char, char);
        void placeShips(Player);
        int attachShip(int, int, int, int);
        void userInput();
        Board();


    };

    #endif /* Board_hpp */
</code></pre>

<p>and the main file</p>

<pre><code>    //
    //  main.cpp
    //  Battleship
    //
    //  Created by Cody Flies on 5/25/16.
    //  Copyright © 2016 Cody Flies. All rights reserved.
    //

    #include &lt;iostream&gt;
    #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;
    }
</code></pre>

<p>This code is the code that I have used in Xcode.</p>