c ++“不完全类型不允许”错误访问类引用信息(具有前向声明的循环依赖) [英] c++ "Incomplete type not allowed" error accessing class reference information (Circular dependency with forward declaration)

查看:181
本文介绍了c ++“不完全类型不允许”错误访问类引用信息(具有前向声明的循环依赖)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中有一些问题,最近围绕着我现在知道的一个循环依赖。简而言之,有两个类,Player和Ball,它们都需要使用来自另一个的信息。在代码中的某一点,将传递另一个引用(从另一个类,将包括两个.h文件)。



阅读之后,我从每个文件中删除#include.h文件,并与向前声明一起。这解决了能够声明类在彼此的问题,但我现在留下一个不完全类型错误当尝试访问对象的传递引用。似乎有几个类似的例子,虽然经常混合更复杂的代码,很难缩小到基本。



我重写了代码在它的最简单

 类播放器; 

class Ball {
public:
Player& PlayerB;
float ballPosX = 800;
private:

};

Player.h:

  class Ball; 

class Player {
public:
void doSomething(Ball& ball);
private:
};

Player.cpp:

  #includePlayer.h

void Player :: doSomething(Ball& ball){
ball.ballPosX + = 10; //不完全类型错误出现在这里。非常感谢:
}



任何帮助理解为什么会这样) p>

Natalie

解决方案

如果您按照此顺序放置定义,代码将被编译

  class Ball; 

class Player {
public:
void doSomething(Ball& ball);
private:
};

class Ball {
public:
Player& PlayerB;
float ballPosX = 800;
private:

};

void Player :: doSomething(Ball& ball){
ball.ballPosX + = 10; //不完全类型错误出现在这里。
}

int main()
{
}


b $ b

函数doSomething的定义需要Ball类的完整定义,因为它访问它的数据成员。



在你的代码示例模块中Player.cpp没有访问类Ball的定义,所以编译器发出错误。


Had some issues in my code recently surrounding what I now know of as a Circular dependency. In short there are two classes, Player and Ball, which both need to use information from the other. Both at some point in the code will be passed a reference of the other (from another class that will include both .h files).

After reading up on it, I removed the #include.h files from each one and went with forward declaration. This solved the issue of being able to declare the classes in eachother, but I'm now left with an "Incomplete type error" when trying to access a passed reference to the object. There seem to be a few similar examples around, though often mixed with more complex code and hard to narrow down to the basics.

I've rewritten the code in it's simplest form (a skeleton essentially).

Ball.h:

class Player;

class Ball {
public:
    Player& PlayerB;
    float ballPosX = 800;
private:

};

Player.h:

class Ball;

class Player {
public:
    void doSomething(Ball& ball);
private:
};

Player.cpp:

#include "Player.h"

void Player::doSomething(Ball& ball) {
    ball.ballPosX += 10;                   // incomplete type error occurs here.
}

Any help understanding why this is the case would be greatly appreciated :)

Natalie

解决方案

If you will place your definitions in this order then the code will be compiled

class Ball;

class Player {
public:
    void doSomething(Ball& ball);
private:
};

class Ball {
public:
    Player& PlayerB;
    float ballPosX = 800;
private:

};

void Player::doSomething(Ball& ball) {
    ball.ballPosX += 10;                   // incomplete type error occurs here.
}

int main()
{
}

The definition of function doSomething requires the complete definition of class Ball because it access its data member.

In your code example module Player.cpp has no access to the definition of class Ball so the compiler issues an error.

这篇关于c ++“不完全类型不允许”错误访问类引用信息(具有前向声明的循环依赖)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆