c ++“不允许不完整的类型”访问类参考信息时出错(带有前向声明的循环依赖)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21743301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c++ "Incomplete type not allowed" error accessing class reference information (Circular dependency with forward declaration)
提问by nat1707828
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).
最近我的代码中有一些问题围绕着我现在所知的循环依赖。简而言之,有两个类,Player 和 Ball,它们都需要使用来自另一个的信息。在代码中的某个点,将传递另一个的引用(来自将包含两个 .h 文件的另一个类)。
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.
在阅读完之后,我从每个文件中删除了 #include.h 文件并进行了前向声明。这解决了能够在彼此中声明类的问题,但是当我尝试访问传递的对象引用时,我现在遇到了“不完整的类型错误”。周围似乎有一些类似的例子,尽管经常与更复杂的代码混合在一起,并且很难缩小到基础。
I've rewritten the code in it's simplest form (a skeleton essentially).
我已经以最简单的形式(本质上是一个骨架)重写了代码。
Ball.h:
球.h:
class Player;
class Ball {
public:
Player& PlayerB;
float ballPosX = 800;
private:
};
Player.h:
玩家.h:
class Ball;
class Player {
public:
void doSomething(Ball& ball);
private:
};
Player.cpp:
播放器.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 :)
任何帮助理解为什么会这样将不胜感激:)
采纳答案by Vlad from Moscow
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.
函数 doSomething 的定义需要类 Ball 的完整定义,因为它访问其数据成员。
In your code example module Player.cpp has no access to the definition of class Ball so the compiler issues an error.
在您的代码示例模块 Player.cpp 无法访问类 Ball 的定义,因此编译器会发出错误。
回答by Nima Soroush
Player.cpp
require the definition of Ball
class. So simply add #include "Ball.h"
Player.cpp
需要Ball
类的定义。所以只需添加#include "Ball.h"
Player.cpp:
播放器.cpp:
#include "Player.h"
#include "Ball.h"
void Player::doSomething(Ball& ball) {
ball.ballPosX += 10; // incomplete type error occurs here.
}
回答by Aleksandar
Here is what I had and what caused my "incomplete type error":
这是我所拥有的以及导致我的“不完整类型错误”的原因:
#include "X.h" // another already declared class
class Big {...} // full declaration of class A
class Small : Big {
Small() {}
Small(X); // line 6
}
//.... all other stuff
What I did in the file "Big.cpp", where I declared the A2's constructor with X as a parameter is..
我在文件“Big.cpp”中所做的,在那里我用 X 作为参数声明了 A2 的构造函数是..
Big.cpp
大文件
Small::Big(X my_x) { // line 9 <--- LOOK at this !
}
I wrote "Small::Big" instead of "Small::Small", what a dumb mistake.. I received the error "incomplete type is now allowed" for the class X all the time (in lines 6 and 9), which made a total confusion..
我写了 "Small::Big" 而不是 "Small::Small",这是一个多么愚蠢的错误......我一直收到类 X 的错误“现在允许不完整的类型”(在第 6 行和第 9 行),完全混乱..
Anyways, that is where a mistake can happen, and the main reason is that I was tired when I wrote it and I needed 2 hours of exploring and rewriting the code to reveal it.
无论如何,这是可能发生错误的地方,主要原因是我写它时很累,我需要2个小时的探索和重写代码才能揭示它。