C ++循环声明

时间:2009-09-25 14:25:19

标签: c++

我的主要(全局)标题中的班级延迟中有几个循环声明案例。

#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

};

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

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

5 个答案:

答案 0 :(得分:9)

好的,在考虑了评论并自己做测试后,真正的答案是: 你必须使用前向声明,不能再使用。 :)

#include <cstdlib>
#include <iostream>

class point_t;
class board_t;

/* Rest of the code stay the same */

答案 1 :(得分:2)

如果你在结构之前写

class point_t;

应该这样做。

虽然我不太清楚为什么你这样组织你的班级。您的电路板中已经有一个数组mArray,因此没有必要指向每个point_t内的相邻点。

编辑:正如另一张海报所说,在您需要使用指针之前。

答案 2 :(得分:2)

您的代码中的转发引用似乎是board_tpoint_t,这是通过向前声明它们来解决的。

由于您在board_t的成员函数声明中引用point_t,因此无法立即在point_t中定义成员函数。他们的定义必须在定义board_t后出现。因此,您必须将函数的定义移动到cpp文件中,或者您必须在board_t的定义后的标题中移动它们的定义,无论哪个更适合您。 point_t仅用作nextPoint_t中的指针类型,因此我们在此处没有相同的问题:

class point_t; // used by nextPoint_t
class board_t; // used by point_t

struct nextPoint_t  //should be implimented with references, but need to practice pointer
{ 
  point_t* above; // goes without problems - doesn't need definition of point_t
  point_t* below;
  point_t* left;
  point_t* right;
};

class point_t
{
 private:
  piece_t mType;
  int mLiberties;
  nextPoint_t  mAdjacent;
  bool mLibertiesCounted;

  int mCountLiberties();
  void mSetPos(int xPos, int yPos, board_t theBoard);
  void mSetStructureLiberties(int numLibs); 

 public:
  point_t ();
  void mSetUp(int xPos, int yPos, board_t theBoard);
  point_t (int xPos, int yPos, board_t theBoard);

  void mUpdateLiberties(); 
};

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

 public:
  board_t();     
};

标题末尾的定义类似于

// define it either inline in the header, or non-inline in a cpp file
inline void point_t::mSetPos(int xPos, int yPos, board_t theBoard) {
  /* some work... */
}

// same for mSetUp...

尽管如此,我建议您使用const引用将该板传递给point_t的成员函数,但是您的代码工作的要求。声明与不完整的参数类型一致。

答案 3 :(得分:1)

只需添加以上struct nextPoint_t

即可
enum piece_t {BLACK, WHITE, EMPTY, WALL};
enum dir_t {ABOVE,BELOW,LEFT, RIGHT}; 

class point_t;
class board_t;

struct nextPoint_t
{ 
  point_t* above;
  point_t* below;
  point_t* left;
  point_t* right;
};

并将对board_t的任何引用更改为board_t *

void mSetUp(int xPos, int yPos, board_t* theBoard);

答案 4 :(得分:0)

#include <cstdlib>
#include <iostream>

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?

class point_t;

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 board_t;

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, const 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, const board_t& theBoard);// sets up mType then calles setPos iFF not WALL type
  point_t (int xPos, int yPos, const 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

};