title: Using a class within a class
body: <p>I have a very specific question on a program I am writing which makes a battleship game.  I have the header file provided to me and I must implement the function definitions within the defined header file.  I will post my entire code, however, I am only having trouble implementing the function <code>void ship::printShip() const</code>. I need it to access information for a class within a class and I have a function provided that I am using under location <code>void print() const</code>, however, it wants me to create a pointer in order to access the information under print. I will post the function definitions and the header file along with the error message.</p>
  
  <p><strong>Header file:</strong></p>
  
  <pre><code>#ifndef BATTLESHIP_H_
  #define BATTLESHIP_H_
  
  // coordinates (location) of the ship and shots
  class location{
  public:
      location(); // void constructor, assigns -1 to X 
      void pick(); // picks a random location
      void fire(); // asks the user to input coordinates of the next shot
      void print() const; // prints location in format "a1"
  
      // returns true if the two locations match
      friend bool compare(location, location);
  
  private:
      static const int fieldSize = 5; // the field (ocean) is fieldSize X fieldSize
      int x;  // 1 through fieldSize
      char y; // 'a' through fieldSize
  };
  
  // contains ship's coordinates (location) and whether is was sunk
  class ship{
  public:
      ship(); // void constructor, sets sunk=false
      bool match(location&amp;) const; // returns true if this location matches
      // the ship's location
      bool isSunk() const { return sunk; } // checks to see if the ship is sunk
      void sink();       // sets "sunk" member variable of the ship to true
      void setLocation(const location&amp;); // deploys the ship at the specified location
      void printShip() const; // prints location and status of the ship
  
  private:
      location loc;
      bool sunk;
  };
  
  // contains the fleet of the deployed ships
  class fleet{
  public:
      void deployFleet(); // deploys the ships in random locations
      // of the ocean
      bool operational() const; // returns true if at least
      // one ship in the fleet is not sunk
      bool isHitNSink(const location &amp;); // returns true if there was a deployed
      // ship at this location (hit) and sinks it
      // otherwise returns false (miss)
      void printFleet() const; // prints out locations of ships in fleet
  
  private:
      static const int fleetSize = 5; // number of battleships
      int check(const location &amp;);          // returns index of the ship 
      // that matches location
      // -1 if none match
      ship ships[fleetSize];        // battleships of the fleet
  };
  
  #endif /* BATTLESHIP_H_ */
  </code></pre>
  
  <p><strong>Here are my function definitions:</strong></p>
  
  <pre><code>#include "battleship.h"
  #include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;ctime&gt;
  
  using namespace std;
  const int FLEET_SIZE = 5;
  const int FIELD_SIZE = 5;
  
  
  void location::pick(){
  
  
  
          x = rand() % FIELD_SIZE;
          if (x == 0){
              x = 1;
          }
          y = rand() % FIELD_SIZE;
          switch (y)
          {
  
          case 0:
              y = 'A';
              break;
          case 1:
              y = 'B';
              break;
          case 2:
              y = 'C';
              break;
          case 3:
              y = 'D';
              break;
          case 4:
              y = 'E';
              break;
          }
  
  
  
  }
  
  void location::fire(void){
  
      cout &lt;&lt; "Enter the number of the grid to fire on ";
      cin &gt;&gt;y;
      cout &lt;&lt; "Enter the letter of the grid to fire on ";
      cin &gt;&gt; x;
  }
  
  void location::print() const{
      cout &lt;&lt; y &lt;&lt; x;
  }
  
  bool compare(location one,location two){
      if ((one.x == two.x)&amp;&amp;(one.y==two.y)){
          return true;
      }
      return false;
  }
  
  location::location(){
      x = -1;
  }
  
  void ship::setLocation(const location&amp; location){
      loc= location;
  }
  
  bool ship::match(location&amp; location)const{
  
      if (compare(loc, location)){
          return true;
      }
      return false;
  }
  
  void ship::printShip()const{
      cout &lt;&lt; loc.print &lt;&lt; endl;
      cout &lt;&lt; sunk &lt;&lt; endl;
  
  }
  
  void ship::sink(){
      sunk = true;
  }
  </code></pre>
  
  <p><strong>Here is the .cpp</strong></p>
  
  <pre><code>#include "battleship.h"
  #include &lt;iostream&gt;
  #include &lt;ctime&gt;
  #include &lt;cstdlib&gt;
  
  using std::cout; using std::cin; using std::endl;
  
  
  /// this is main function
  
  const int FLEET_SIZE = 5;
  const int FIELD_SIZE = 5;
  int main(){
      srand(time(NULL));
  
      //
      // checking location object
      // 
  
      location mySpot, userShot;
      mySpot.pick(); // selecting a new random location
  
      cout &lt;&lt; "Randomly selected location is: "; mySpot.print();
  
      cout &lt;&lt; "Input location: ";
      userShot.fire(); // having user input a location
  
      if (compare(mySpot, userShot))
          cout &lt;&lt; "Random location matches user input.\n";
      else
          cout &lt;&lt; "Random location does not match user input.\n";
  
  
  
      //
      // checking ship object
      //
  
  
  
      ship myShip;
  
      myShip.setLocation(mySpot); // placing ship at mySpot location
  
      if(myShip.match(userShot))
      cout &lt;&lt; "myShip\'s location matches user input.\n";
      else
      cout &lt;&lt; "myShip's location does not match user input.\n";
  
      if(!myShip.isSunk()){
      cout &lt;&lt; "myship is not sunk yet, sinking it.\n";
      myShip.sink();
      }
  
      cout &lt;&lt; "myShip\'s status is: "; myShip.printShip();
  
  
  
      //
      // checking fleet object
      // 
  
      /* // uncomment this part once you are done debugging above code
      fleet myFleet;
  
      myFleet.deployFleet(); // fleet is deployed at random locations
  
      if(myFleet.operational())
      cout &lt;&lt; "Some ships of myFleet are still up.\n";
  
      if(myFleet.isHitNSink(userShot))
      cout &lt;&lt; "there was a ship at userShot location, no it is sunk.\n";
      else
      cout &lt;&lt; "there was no ship at userShot location.\n";
  
      cout &lt;&lt; "myFleet\'s status is: "; myFleet.printFleet();
      */
  }
  </code></pre>
  
  <p>Here is the error for <code>printShip() const</code></p>
  
  <pre><code>Error   1   error C3867: 'location::print': function call missing argument list; use '&amp;location::print' to create a pointer to member
  </code></pre>
  
  <p>If someone could just explain what I need to edit in order to make  <code>printShip() const</code> print the location of the ship as intended that is all need.</p>