C++循环声明

Lyn*_*ite 1 c++

我的主要(全局)标题中的类delaractions中有几个循环声明的例子.

#include <cstdlib>
#include <iostream>

using namespace std;


enum piece_t {BLACK, WHITE, EMPTY, WALL}; //wall is area out side of board (board array is 21x21 but only 19x19 is playable)
enum dir_t {ABOVE,BELOW,LEFT, RIGHT}; //shall i overload ! or - operatior? !LEFT==RIGHT?


struct nextPoint_t  //should be implimented with references, but need to practice pointer
{ 
  point_t* above;
  point_t* below;
  point_t* left;
  point_t* right;

};




class point_t
{
 private:
  piece_t mType; //what sort of point this is
  int mLiberties;
  nextPoint_t  mAdjacent; // points to adjacent points

  bool mLibertiesCounted; // keeps track of if liberties have been counted, for mCountLiberites() (sets), is reset by mUpdateLiberites(); 

  int mCountLiberties(); //counts this point's liberites, by calling count on mAdjacent points etc.
  void mSetPos(int xPos, int yPos, board_t theBoard); //sets up mAdjacent to point to adjacent points,
  void mSetStructureLiberties(int numLibs); // Sets this squares liberites then calls this on all adjacent squares 


 public:
  point_t ();//  parameterless constructor, for arrays
  void mSetUp(int xPos, int yPos, board_t theBoard);// sets up mType then calles setPos iFF not WALL type
  point_t (int xPos, int yPos, board_t theBoard); //constructor, takes it's position in the grid as a parameter

  void mUpdateLiberties(); // calles countLiberties then, updates liberites on whole of connected structure, by operating pon all conencted points



};

class board_t 
{
 private:
  point_t mArray [21][21];


 public:
  board_t(); //constructor, sets up board by operating on the point_t's

};
Run Code Online (Sandbox Code Playgroud)

不要担心我的阅读评论,我知道我的意思.

我以为我可以通过前向声明修复它,但它们似乎不起作用,它只是认为我正在重新定义类

Jod*_*odi 9

好吧,在考虑了评论并自己进行测试之后,真正的答案是:你必须使用前瞻声明,不再需要.:)

#include <cstdlib>
#include <iostream>

class point_t;
class board_t;

/* Rest of the code stay the same */
Run Code Online (Sandbox Code Playgroud)

  • 在非定义函数声明中,允许参数类型不完整.所以这应该没有指针或引用. (4认同)