在重载<<运算符是否正确格式化?

时间:2017-11-30 00:46:09

标签: c++ class operators overloading

我一直在获取输出,有时甚至获得完整的输出,但是我不太明白为什么我会得到输出。我想相信它与我的运算符“<<”的重载有关。当我执行输出“OpenSea”对象中的信息的操作时。任何线索都会非常有帮助,感谢您的时间!

注意:我只包含了必要的文件,但如果您认为需要更多内容来了解​​我的情况,我会更新。此外,没有必要向我提供有关如何格式化代码的提示,因为我知道很多可以重新进行优化。

的main.cpp

#include "aquaClasses.h"
#include "aquaFish.h"
#include "aquaKillerWhale.h"
#include "aquaPenguin.h"
#include "aquaSea.h"

using namespace std;

int main ()
{
  srand (time (NULL));
  // Simulation
  Sea OpenSea (17, 17);
  cout << OpenSea;
}

aquaClasses.h &lt; - “main”标题文件

#ifndef AQUACLASSES_H
#define AQUACLASSES_H

#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iomanip>

// Constants
const short int AMT_FISH = 200;
const short int AMT_KWHALES = 2;
const short int AMT_PENGS = 50;
const short int MAX_SIM_INTERATIONS = 10000;
const short int MAX_SEA_GRID = 25;
const short int MAX_SEA_SIZE = 625;

// Functions
template <typename TYPE> int calculate_distance (int obj1_x, int obj1_y,
                                                 TYPE obj2);
template <typename TYPE> int calculate_direction (int obj1_x, int obj1_y,
                                                  TYPE obj2);

#endif

aquaSea.h

#ifndef AQUASEA_H
#define AQUASEA_H

// Forward Declarations to circumvent circular dependency
class Fish;
class Penguin;
class Killer_Whale;

class Sea
{
  private:
    char m_grid [MAX_SEA_GRID][MAX_SEA_GRID];
    int m_fish;
    int m_kwhales;
    int m_pengs;
    int m_size;
    int m_height;
    int m_width;
    void clear ();
    void populate ();
  public:
    Sea (const int grid_size_height, const int grid_size_width);
    friend std::ostream &operator<<(std::ostream &os, Sea obj);
    int getHeight () const {return m_height;};
    int getWidth () const {return m_width;};
    void setCell (int obj_x, int obj_y, char cell_symbol);
    // Appropriate accessor/mutator functions
};

#endif

aquaSea.cpp

#include "aquaClasses.h"
#include "aquaFish.h"
#include "aquaPenguin.h"
#include "aquaKillerWhale.h"
#include "aquaSea.h"

using namespace std;

//  - [row][column]
Sea::Sea (const int grid_size_height, const int grid_size_width)
{
  m_fish = 200;
  m_kwhales = 2;
  m_pengs = 50;

  if ((grid_size_height || grid_size_width) <= 0)
  {
    if (grid_size_height <= 0 && grid_size_width > 0)
    {
      m_size = MAX_SEA_GRID * grid_size_width;
      m_width = grid_size_width;
      m_height = MAX_SEA_GRID;
    }
    else if (grid_size_width <= 0 && grid_size_height > 0)
    {
      m_size = MAX_SEA_GRID * grid_size_height;
      m_width = MAX_SEA_GRID;
      m_height = grid_size_height;
    }
    else if (grid_size_height <= 0 && grid_size_width <= 0)
    {
      m_size = MAX_SEA_GRID * MAX_SEA_GRID;
      m_height = MAX_SEA_GRID;
      m_width = MAX_SEA_GRID;
    }
  }
  else
    {
      m_size = grid_size_height * grid_size_width;
      m_height = grid_size_height;
      m_width = grid_size_width;
    }
  clear ();
  populate ();
}


ostream &operator<<(ostream &os, Sea obj)
{
  os << "Sea Height: " << obj.m_width << endl << "Sea Wdith: "
     << obj.m_width << endl << "Sea Size: " << obj.m_size
     << endl;
  os << "Grid View: " << endl;
  for (int i = 0; i < obj.m_height; i++)
  {
    for (int j = 0; j < obj.m_width; j++)
    {
      os << obj.m_grid [i][j];
    }
    os << endl;
  }
  return os;
}

void Sea::clear ()
{
  for (int i = 0; i < m_height; i++)
  {
    for (int j = 0; j < m_width; j++)
    {
      m_grid[i][j] = 'O';
    }
  }
  return;
}

void Sea::populate ()
{
  // Special simluation variables
  bool applyFish = true;
  bool applyKWhales = false;
  bool applyPengs = false;
  int amtFishPop = 35;
  int amtKWhalesPop = 2;
  int amtPengPop = 20;
  int index = 0;
  int randGridRow = 0;
  int randGridCol = 0;
  int totalPop = amtFishPop + amtKWhalesPop + amtPengPop;
  Fish arr_fish [AMT_FISH];
  Killer_Whale arr_kwhales [AMT_KWHALES];
  Penguin arr_pengs [AMT_PENGS];

  for (int i = 0; i < totalPop; i++)
  {
    // Grab random place on grid to apply and check if grid plot is open
    randGridRow = rand () % 16;
    randGridCol = rand () % 16;

    while (m_grid [randGridRow][randGridCol] != 'O')
    {
      randGridRow = rand () % 16;
      randGridCol = rand () % 16;
    }

    // Populate and Update Fish
    if (amtFishPop > 0 && applyFish == true)
    {
      arr_fish[index].setX (randGridCol);
      arr_fish[index].setY (randGridRow);
      setCell (arr_fish[index].getX (), arr_fish[index].getY (), 'F');
      amtFishPop--;
      index++;
    }
    else if (amtFishPop == 0)
    {
      applyFish = false;
      applyKWhales = true;
      index = 0;
    }

    // Populate and Update Killer Whales
    if (amtKWhalesPop > 0 && applyKWhales == true)
    {
      arr_kwhales[index].setX (randGridCol);
      arr_kwhales[index].setY (randGridRow);
      setCell (arr_kwhales[index].getX (), arr_kwhales[index].getY (), 'K');
      amtKWhalesPop--;
      index++;
    }
    else if (amtKWhalesPop == 0)
    {
      applyKWhales = false;
      applyPengs = true;
      index = 0;
    }

    // Populate and Update Penguins
    if (amtPengPop > 0 && applyPengs == true)
    {
      arr_pengs[index].setX (randGridCol);
      arr_pengs[index].setY (randGridRow);
      setCell (arr_pengs[index].getX (), arr_pengs[index].getY (), 'P');
      amtPengPop--;
      index++;
    }
    else if (amtPengPop == 0)
    {
      applyPengs = false;
      index = 0;
    }
  }

  return;
}

void Sea::setCell (int obj_x, int obj_y, char cell_symbol)
{ 
  m_grid [obj_x][obj_y] = cell_symbol;

  return;
}

输出类型
通缉:
  - Image
不需要的:
  - Image
STOP!此点之后是可选代码以进一步了解情况

其他参考代码如果您需要,但我认为不需要

aquaFish.h

#ifndef AQUAFISH_H
#define AQUAFISH_H

class Fish
{
  private:
    int m_fish_amt_food;
    int m_fish_x;
    int m_fish_y;
    bool m_fish_alive;
  public:
    Fish ();
    int getX () const {return m_fish_x;};
    int getY () const {return m_fish_y;};
    void setX (int new_x) {m_fish_x = new_x;};
    void setY (int new_y) {m_fish_y = new_y;};
    int getFishAmtFood () const {return m_fish_amt_food;};
    void move ();
};

#endif

aquaFish.cpp

#include "aquaClasses.h"
#include "aquaFish.h"

using namespace std;

Fish::Fish ()
{
  int randNum = rand () % 10 + 1;
  m_fish_amt_food = randNum;
  m_fish_x = -1;
  m_fish_y = -1;
}

void Fish::move ()
{
  int randDir = rand () % 8 + 1;
  if (randDir == 1)
  {
    m_fish_y++;
  }
  else if (randDir == 2)
  {
    m_fish_x++;
    m_fish_y++;
  }
  else if (randDir == 3)
  {
    m_fish_x++;
  }
  else if (randDir == 4)
  {
    m_fish_x++;
    m_fish_y--;
  }
  else if (randDir == 5)
  {
    m_fish_y--;
  }
  else if (randDir == 6)
  {
    m_fish_x--;
    m_fish_y--;
  }
  else if (randDir == 7)
  {
    m_fish_x--;
  }
  else if (randDir == 8)
  {
    m_fish_x--;
    m_fish_y++;
  }

  return;
}

aquaPenguin.h

#ifndef AQUAPENGUIN_H
#define AQUAPENGUIN_H

// Forward Declarations to circumvent circular dependancy
class Sea;
class Fish;
class Killer_Whale;

class Penguin
{
  private:
    int m_peng_health;   // 0-100
    int m_peng_x;
    int m_peng_y;
    bool m_peng_alive;
  public:
    Penguin ();
    int getX () const {return m_peng_x;};
    int getY () const {return m_peng_y;};
    void setX (int new_x) {m_peng_x = new_x;};
    void setY (int new_y) {m_peng_y = new_y;};
    void move (Sea obj_sea, Fish arr_fish [], int arr_fish_size,
               Killer_Whale arr_kwhale [], int arr_kwhale_size);
};

#endif

aquaPenguin.cpp

#include "aquaClasses.h"
#include "aquaFish.h"
#include "aquaKillerWhale.h"
#include "aquaPenguin.h"
#include "aquaSea.h"
#include "aquaFunctions.cpp"

using namespace std;

Penguin::Penguin ()
{
  m_peng_health = rand () % (81 - 60) + 60;
  m_peng_x = -1;
  m_peng_y = -1;
  m_peng_alive = false;
}

void Penguin::move (Sea obj_sea, Fish arr_fish [], int arr_fish_size,
                    Killer_Whale arr_kwhale [], int arr_kwhale_size)
{
  int actuDistToFish = 8;
  int currDistToFish = 0;
  int tempMoveX = 0;
  int tempMoveY = 0;
  int amtMove = 0;
  int direction = 0;
  int fishIndex = 0;
  bool moveAwayKillerWhale = false;
  bool fishInRange = false;

  // Determine amount of cells to move in sea
  if (m_peng_health >= 81 && m_peng_health <= 100)
  {
    amtMove = 5;
  }
  else if (m_peng_health >= 61 && m_peng_health <= 80)
  {
    amtMove = 4;
  }
  else if (m_peng_health >= 41 && m_peng_health <= 60)
  {
    amtMove = 3;
  }
  else if (m_peng_health >= 21 && m_peng_health <= 40)
  {
    amtMove = 2;
  }
  else if (m_peng_health >= 1 && m_peng_health <= 20)
  {
    amtMove = 1;
  }
  else
  {
    cout << "Chicken of the sea at: " << m_peng_x << " " << m_peng_y
         << endl;

    return;
  }

  // ADD: Find if any killer whales are near first and if so then penguin just moves away

  // Find if any fish are near <-- THIS IS WRONG, YOU NEED TO FIND THE CLOSEST FISH
  for (int i = 0; i < arr_fish_size; i++)
  {
    currDistToFish = calculate_distance (m_peng_x, m_peng_y,
                                         arr_fish[i]);
    if (currDistToFish <= 8)
    {
      if (currDistToFish < actuDistToFish)
      {
        actuDistToFish = currDistToFish;
        fishIndex = i;
        fishInRange = true;
      }
    }
  }

  // ADD: If fish and whale are found do something, we decide. Otherwise move randomly Peng See Dist 8.0

  // ADD Move 1 tick then gauge situation again
  for (int k = 0; k < amtMove; k++)
  {
    if (fishInRange == true && moveAwayKillerWhale == false)
    {
      tempMoveX = m_peng_x;    // temp used for storing before changing
      tempMoveY = m_peng_y;    // temp used for storing before changing
      direction = calculate_direction (m_peng_x, m_peng_y,
                                       arr_fish[fishIndex]);
      cout << "Penguin pos before moving: " << m_peng_x << ","
           << m_peng_y << endl;
      cout << "Closest Fish pos: " << arr_fish[fishIndex].getX () << ","
           << arr_fish[fishIndex].getY () << endl;

      if (m_peng_health == 0)
      {
        cout << "Chicken of the sea at: " << m_peng_x << " " << m_peng_y
             << endl;

        return;
      }
      if (direction == 1)
      {
        actuDistToFish--;
        m_peng_health--;
        tempMoveY++;
      }
      else if (direction == 2)
      {
        actuDistToFish--;
        m_peng_health--;
        tempMoveX++;
        tempMoveY++;
      }
      else if (direction == 3)
      {
        actuDistToFish--;
        m_peng_health--;
        tempMoveX++;
      }
      else if (direction == 4)
      {
        actuDistToFish--;
        m_peng_health--;
        tempMoveX++;
        tempMoveY--;
      }
      else if (direction == 5)
      {
        actuDistToFish--;
        m_peng_health--;
        tempMoveY--;
      }
      else if (direction == 6)
      {
        actuDistToFish--;
        m_peng_health--;
        tempMoveX--;
        tempMoveY--;
      }
      else if (direction == 7)
      {
        actuDistToFish--;
        m_peng_health--;
        tempMoveX--;
      }
      else if (direction == 8)
      {
        actuDistToFish--;
        m_peng_health--;
        tempMoveX--;
        tempMoveY++;
      }
      else
      {
        cout << "[ERROR]: Penguin direction messed up." << endl;
      }

      // MODIFY: Lastly check if out of bounds and then move peng
      if (tempMoveX > obj_sea.getWidth ()
          || tempMoveX < -(obj_sea.getWidth ()))
      {
        m_peng_x = m_peng_x;    // AKA dont move
        m_peng_y = m_peng_y;
      }
      else if (tempMoveY > obj_sea.getHeight ()
               || tempMoveY < -(obj_sea.getHeight ()))
      {
        m_peng_x = m_peng_x;    // AKA dont move
        m_peng_y = m_peng_y;
      }
      else
      {
        obj_sea.setCell (m_peng_x, m_peng_y, 'O'); // Delete old cell
        m_peng_x = tempMoveX;
        m_peng_y = tempMoveY;
        obj_sea.setCell (m_peng_x, m_peng_y, 'P'); // Set new cell
      }

      // Check if peng eats after moving
      if (actuDistToFish == 0)
      {
        // Stop moving
        amtMove = 0;
        // Eat fish
        m_peng_health += arr_fish[fishIndex].getFishAmtFood ();
        // ADD: Remove fish from grid
      }
      cout << "Penguin pos after moving: " << m_peng_x << ","
           << m_peng_y << endl;
    }
    else if (fishInRange == false && moveAwayKillerWhale == true)
    {

    }
    else if (fishInRange == false && moveAwayKillerWhale == false)
    {
      // If no fish, movement is random, else it's towards fish how ever many
      //    step the penguin can go
      direction = rand () % 8 + 1;
      if (direction == 1)
      {
        m_peng_y++;
      }
      else if (direction == 2)
      {
        m_peng_x++;
        m_peng_y++;
      }
      else if (direction == 3)
      {
        m_peng_x++;
      }
      else if (direction == 4)
      {
        m_peng_x++;
        m_peng_y--;
      }
      else if (direction == 5)
      {
        m_peng_y--;
      }
      else if (direction == 6)
      {
        m_peng_x--;
        m_peng_y--;
      }
      else if (direction == 7)
      {
        m_peng_x--;
      }
      else if (direction == 8)
      {
        m_peng_x--;
        m_peng_y++;
      }
      else
      {
        cout << "[ERROR]: Penguin random direction messed up." << endl;
      }
    }
  }

  return;
}

aquaKillerWhale.h

#ifndef AQUAKILLERWHALE_H
#define AQUAKILLERWHALE_H

class Penguin;

class Killer_Whale
{
  private:
    int m_kwhale_amt_pengs;
    int m_kwhale_x;
    int m_kwhale_y;
  public:
    Killer_Whale ();
    int getX () const {return m_kwhale_x;};
    int getY () const {return m_kwhale_y;};
    void setX (int new_x) {m_kwhale_x = new_x;};
    void setY (int new_y) {m_kwhale_y = new_y;};
    void move (Penguin arr_peng []);
};

#endif

aquaKillerWhale.cpp

#include "aquaClasses.h"
#include "aquaKillerWhale.h"
#include "aquaPenguin.h"

using namespace std;

Killer_Whale::Killer_Whale ()
{
  m_kwhale_x = -1;
  m_kwhale_y = -1;
}


void Killer_Whale::move (Penguin arr_peng [])
{

  return;
}

aquaFunctions.cpp

#include "aquaClasses.h"

using namespace std;

// Functions
template <typename TYPE>
int calculate_direction (int obj1_x, int obj1_y, TYPE obj2)
{
  int calculatedX = obj2.getX () - obj1_x;
  int calculatedY = obj2.getY () - obj1_y;
  int direction = 0;
  if (calculatedX == 0 && calculatedY > 0)
  {
    direction = 1;
  }
  else if (calculatedX > 0 && calculatedY > 0)
  {
    direction = 2;
  }
  else if (calculatedX > 0 && calculatedY == 0)
  {
    direction = 3;
  }
  else if (calculatedX > 0 && calculatedY < 0)
  {
    direction = 4;
  }
  else if (calculatedX == 0 && calculatedY < 0)
  {
    direction = 5;
  }
  else if (calculatedX < 0 && calculatedY < 0)
  {
    direction = 6;
  }
  else if (calculatedX < 0 && calculatedY == 0)
  {
    direction = 7;
  }
  else if (calculatedX < 0 && calculatedY > 0)
  {
    direction = 8;
  }
  else
  {
    cout << "[ERROR]: Direction calculation failed." << endl;
  }

  return direction;
}

template <typename TYPE>
int calculate_distance (int obj1_x, int obj1_y, TYPE obj2)
{
  int distance = sqrt ((obj1_x - obj2.getX ())
                       * (obj1_x - obj2.getX ())
                       + (obj1_y - obj2.getY ())
                       * (obj1_y - obj2.getY ()));

  return distance;
}

0 个答案:

没有答案