这部分代码有问题。 编译器说我不能使用指针指向未完成的类。 我已经尝试过在Figure类中使用include Board类,而在Board中使用Figure,但这会导致编译器出现严重问题,并且会出现很多错误。(#pragma一次和/或使用标题中的警卫)
//Board.h
class Figure;
class Board
{
Figure *sz[8][8];
...
public:
void showRange();
friend class Figure;
};
//-------------------
//Board.cpp
void Board::showRange()
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if(sz[j][i])
sz[j][i]->range();
}
}
this->display();
}
...
//Figure.h
class Board;
class Figure
{
protected:
Board *s;
int x, y;
public:
virtual void range() = 0;
friend class Board;
};
//range funcions are defined in member classes
[edit1] 在Figure.cpp和成员classes.files中的board.h编译器错误C2027中添加了Figure.h。
Severity Code Description Project File Line Suppression State
Error C2027 use of undefined type 'Board' ..\figure.cpp 7
Error C2027 use of undefined type 'Board' ..\figure.cpp 15
Error C2027 use of undefined type 'Board' ..\figure.cpp 17
Error C2027 use of undefined type 'Board' ..\figure.cpp 25
Error C2027 use of undefined type 'Board' ..\figure.cpp 26
Error C2027 use of undefined type 'Board' ..\bishop.cpp 7
Error C2027 use of undefined type 'Board' ..\bishop.cpp 13
Severity Code Description Project File Line Suppression State
Error (active) E0393 pointer to incomplete class type is not allowed ..\Figure.cpp 7
Error (active) E0393 pointer to incomplete class type is not allowed ..\Figure.cpp 15
Error (active) E0393 pointer to incomplete class type is not allowed ..\Figure.cpp 17
Error (active) E0393 pointer to incomplete class type is not allowed ..\Figure.cpp 26
Error (active) E0393 pointer to incomplete class type is not allowed ..\Figure.cpp 25
答案 0 :(得分:3)
Board.cpp
必须#include "Figure.h"
,否则编译器不了解range
对象的Figure
方法(在Board.cpp中调用)。
顺便说一句:为什么需要朋友声明?通常表明设计不佳。