C++ 错误:成员访问不完整的类型';蜗牛';
我在下面的截图中有这个错误。我曾尝试在hpp文件中列出构造函数和函数,并在cpp文件中定义函数,但这也不起作用。我不知道该怎么解决这个问题。我认为这个错误可能与标题和它们可能被放置的顺序有关。这两门课的所有内容都完成了。这些类都位于名为Cell.hpp和Snail.hpp的单独文件中 在Cell.hpp中,我有:C++ 错误:成员访问不完整的类型';蜗牛';,c++,C++,我在下面的截图中有这个错误。我曾尝试在hpp文件中列出构造函数和函数,并在cpp文件中定义函数,但这也不起作用。我不知道该怎么解决这个问题。我认为这个错误可能与标题和它们可能被放置的顺序有关。这两门课的所有内容都完成了。这些类都位于名为Cell.hpp和Snail.hpp的单独文件中 在Cell.hpp中,我有: #ifndef Cell_hpp #define Cell_hpp #include "TheGreatSnailRaceParams.hpp" #include
#ifndef Cell_hpp
#define Cell_hpp
#include "TheGreatSnailRaceParams.hpp"
#include <stdio.h>
#include "Cell.hpp"
#include "Snail.hpp"
#include "CellBase.hpp"
class Cell : public CellBase{
public:
Cell() = default; // default constructor
Cell(params::settings *s) { // parameterized constructor
settings_colors = &s->c; // construct cell
current = &s->c.at(0);
}
params::color* current;
std::vector<params::color>* settings_colors; // unsigned int color requirement
void nextColor() override{
this->color = current->nextColor;
current = &settings_colors->at(this->color);
}
void nextOrientation(Snail *s) override{
int change_orientation = current->deltaOrientation;
int absolute_value;
if(change_orientation < 0){
absolute_value = (-1) * (change_orientation);
while(absolute_value > 0){
s->o.go_clockwise(); // error message here: Member access into incomplete type 'Snail'
absolute_value = absolute_value - 1;
}
}
if(change_orientation > 0){
absolute_value = change_orientation;
s->o.go_counterclockwise(); // error message here: Member access into incomplete type 'Snail'
absolute_value = absolute_value - 1;
}
}
private:
};
#endif /* Cell_hpp */
//In the Snail.hpp I have:
#ifndef SNAIL_H_
#define SNAIL_H_
#include "Board.hpp"
#include "SnailBase.hpp"
#include "TheGreatSnailRaceParams.hpp"
class Snail : public SnailBase{
public:
Snail() = default; // default constructor
Snail(orientation init_orientation, SnailBase::location location, unsigned int board_size){
boardSize = board_size;
snail_obj->row = std::get <0> (location);
snail_obj->col = std::get <1> (location);
o = init_orientation;
}
orientation o;
location current_location;
Snail* snail_obj;
int row, col;
location loc;
void step(Cell* c) override{
c->nextOrientation(this); // change orientation
c->nextColor(); // changes to next color
if(o.current == 0){
this->snail_obj->col = this->snail_obj->col + 1;
}
if(o.current == 1){
this->snail_obj->row = this->snail_obj->row + 1;
}
if(o.current == 2){
this->snail_obj->col = this->snail_obj->col - 1;
}
if(o.current == 3){
this->snail_obj->row = this->snail_obj->row - 1;
}
if(this->snail_obj->row == -1){
this->snail_obj->row = this->boardSize - 1;
}
if(this->snail_obj->row == 0){
this->snail_obj->row = this->boardSize;
}
if(this->snail_obj->col == -1){
this->snail_obj->col = this->boardSize - 1;
}
if(this->snail_obj->col == 0){
this->snail_obj->col = this->boardSize;
}
}
unsigned int get_orientation() override{
return o.current;
}
virtual location get_location() override{
std::tuple<int, int> current_location (snail_obj->row, snail_obj->col);
return current_location;
}
private:
unsigned int boardSize;
};
#endif
\ifndef单元水电站
#定义单元\u水电站
#包括“TheGreatSnailRaceParams.hpp”
#包括
#包括“Cell.hpp”
#包括“Snail.hpp”
#包括“CellBase.hpp”
类别单元:公共单元库{
公众:
Cell()=default;//默认构造函数
单元格(params::settings*s){//参数化构造函数
设置\u colors=&s->c;//构造单元
当前=&s->c.at(0);
}
参数::颜色*当前;
std::vector*设置\u颜色;//无符号整数颜色要求
void nextColor()覆盖{
此->颜色=当前->下一个颜色;
当前=&settings\u colors->at(此->颜色);
}
无效连接(Snail*s)覆盖{
int change_orientation=current->deltaOrientation;
int绝对值;
如果(改变方向<0){
绝对_值=(-1)*(改变_方向);
而(绝对值>0){
s->o.go_顺时针();//此处的错误消息:成员访问不完整的类型“Snail”
绝对值=绝对值-1;
}
}
如果(更改方向>0){
绝对值=改变方向;
s->o.go_逆时针();//此处显示错误消息:成员访问不完整的类型“Snail”
绝对值=绝对值-1;
}
}
私人:
};
#endif/*单元水电站*/
//在Snail.hpp中,我有:
#ifndef蜗牛_
#定义蜗牛_
#包括“Board.hpp”
#包括“SnailBase.hpp”
#包括“TheGreatSnailRaceParams.hpp”
蜗牛类:公共蜗牛库{
公众:
Snail()=default;//默认构造函数
Snail(方向初始方向,SnailBase::位置位置,无符号int板大小){
boardSize=电路板尺寸;
snail_obj->row=std::get(位置);
snail_obj->col=std::get(位置);
o=初始方向;
}
方向o;
位置当前位置;
蜗牛*Snail_obj;
int row,col;
位置loc;
无效步骤(单元格*c)覆盖{
c->nextOrientation(this);//更改方向
c->nextColor();//更改为下一种颜色
如果(o.current==0){
此->蜗牛对象->列=此->蜗牛对象->列+1;
}
如果(o.current==1){
此->蜗牛对象->行=此->蜗牛对象->行+1;
}
如果(o.current==2){
此->蜗牛对象->列=此->蜗牛对象->列-1;
}
如果(o.current==3){
此->蜗牛对象->行=此->蜗牛对象->行-1;
}
如果(此->蜗牛对象->行==-1){
此->蜗牛对象->行=此->板尺寸-1;
}
如果(此->蜗牛对象->行==0){
此->蜗牛对象->行=此->板尺寸;
}
如果(此->蜗牛对象->列==-1){
此->蜗牛对象->列=此->板尺寸-1;
}
如果(此->蜗牛对象->列==0){
此->蜗牛对象->col=此->板尺寸;
}
}
unsigned int get_orientation()覆盖{
返回o.current;
}
虚拟位置get_location()覆盖{
std::元组当前位置(蜗牛对象->行,蜗牛对象->列);
返回当前位置;
}
私人:
无符号整型板尺寸;
};
#恩迪夫
这是否回答了您的问题?我真的解决了。我必须将这两个类相互转发,然后在cpp文件中定义。提示:您可以抛弃所有this->
chaff。这只有在有可变阴影的情况下才有必要,这里没有。还有为什么蜗牛体内有蜗牛呢?snail\u obj
在哪里初始化?