[英]Error: variable or field 'PrintEntity' declared void void PrintEntity(Entity e);
The code is really simple (but I am a newbie so I have no idea what I am doing wrong):代码非常简单(但我是新手,所以我不知道我做错了什么):
#include<iostream>
#include<string>
void PrintEntity(Entity* e);
class Entity
{
public:
int x,y;
Entity(int x, int y)
{
Entity* e= this;
e-> x=x;
this->y=y;
PrintEntity(this);
}
};
void PrintEntity(Entity* e)
{
// *Do stuff*
}
int main()
{
return 0;
}
My understanding of the error is that I cannot declare the function PrintEntity before of the class Entity.我对错误的理解是我不能在 class 实体之前声明 function PrintEntity。 But even if I would declare the function below the class it would be a problem since in the Constructor I am calling the function PrintEntity.
但是,即使我将 function 声明在 class 之下,这也会是一个问题,因为在构造函数中我调用的是 function PrintEntity。
So I am quite stuck.所以我很困惑。 Can anybody explain to me what I am doing wrong please?
有人可以向我解释我做错了什么吗?
Declare the function before the class definition like在 class 定义之前声明 function
void PrintEntity( class Entity* e);
using the elaborated type specifier.使用详细的类型说明符。
Otherwise the compiler does not know what is Entity
.否则编译器不知道什么是
Entity
。
The compiler reads your file from the top down.编译器从上到下读取您的文件。
When it encounters void PrintEntity(Entity * e);
当它遇到
void PrintEntity(Entity * e);
, it must determine whether Entity * e
is a formal parameter (making this a function declaration) or a multiplication (making this a variable declaration with initialiser). ,它必须确定
Entity * e
是形式参数(使其成为 function 声明)还是乘法(使其成为带有初始化程序的变量声明)。
Since the compiler is completely unaware of a type called "Entity", it decides that this must be a variable declaration, and a variable cannot have type void
.由于编译器完全不知道名为“Entity”的类型,它决定这必须是变量声明,并且变量不能具有
void
类型。
The solution is to declare the class before the function prototype, either separately:解决方案是在 function 原型之前声明 class,或者单独声明:
class Entity;
void PrintEntity(Entity* e);
or directly in the function declaration:或直接在 function 声明中:
void PrintEntity(class Entity* e);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.