C++ c++;尽管包含类,但声明错误
我已经读了很多类似的问题,但我还是不明白。当我试图编译这是下一个时,我在QtCreator中遇到错误:“网格尚未声明”,尽管我在显示此错误的bunny类中包含了网格类头。这是我的密码: (在函数行“void becomeVampire(Bunny*&,int&,Grid&);”中产生错误,如果您在第一个(Bunny)类中从下向上计数,这是第五个;在函数“void convertNeightorTovanpire(Bunny*const,int&,Grid&);”中也是第一个从下向上:这两个函数都使用对网格类的引用作为参数之一) 最后一节课,位置:C++ c++;尽管包含类,但声明错误,c++,class,inheritance,C++,Class,Inheritance,我已经读了很多类似的问题,但我还是不明白。当我试图编译这是下一个时,我在QtCreator中遇到错误:“网格尚未声明”,尽管我在显示此错误的bunny类中包含了网格类头。这是我的密码: (在函数行“void becomeVampire(Bunny*&,int&,Grid&);”中产生错误,如果您在第一个(Bunny)类中从下向上计数,这是第五个;在函数“void convertNeightorTovanpire(Bunny*const,int&,Grid&);”中也是第一个从下向上:这两个函数都
#define POSITION_H
#include "Grid.h"
class Position
{
private:
int row,column;
char sign;
public:
Position();
Position ( int parrentRow,int parrentColumn );
int getRow() const ;
int getColumn() const;
char getSign() const;
};
#endif // POSITION_H
Bunny.h
包括Grid.h
,Grid.h
包括Bunny.h
。这不行。#include
s是内部防护装置,因此可以工作。造成问题的原因是缺乏前瞻性声明。@JohnGaughan很难制定工作计划。这也许是可能的,但是它太混乱了以至于无法考虑。@ JuangopangZa,那么哪个头不应该包括另一个?无论我删除哪一个,我都会得到另一个编译错误(因为我在网格函数中使用Bunny对象作为参数,在Bunny函数中使用Grid对象作为参数),您可以在Bunny.h
和Grid.h
中向前声明class Grid
。删除这两个包含项,但请记住将它们添加到需要它们的.cpp
文件中。这样搜索。这个问题已经被问了无数次了。
#ifndef GRID_H
#define GRID_H
#include "Position.h"
#include "Bunny.h"
class Grid
{
private:
char GridField[80][80];
void renewGrid(); //sets all grid positions to '.',called by constructor and updateTheGrid()
public:
Grid();
void updateTheGrid(Bunny * const); //sets all grid positions to new positions based on new bunny objects linked list
void printTheGrid() const; //prints the grid to the new file
char getGridField(int,int); //returns the character on certain position
};
#endif // GRID_H
#define POSITION_H
#include "Grid.h"
class Position
{
private:
int row,column;
char sign;
public:
Position();
Position ( int parrentRow,int parrentColumn );
int getRow() const ;
int getColumn() const;
char getSign() const;
};
#endif // POSITION_H