C++ 从类方法C+;调用非类函数+;
我想调用类外的函数C++ 从类方法C+;调用非类函数+;,c++,compiler-errors,C++,Compiler Errors,我想调用类外的函数print。 在转发声明void print(Entity*e)中,我得到如下错误: 'Entity' undeclared identifier 'e' undeclared identifier 'print' illegal use of type 'void' term does not evaluate to a function taking 1 arguments (at the function call)-> `print(this);`
print
。在转发声明
void print(Entity*e)
中,我得到如下错误:
'Entity' undeclared identifier
'e' undeclared identifier
'print' illegal use of type 'void'
term does not evaluate to a function taking 1 arguments (at the function call)-> `print(this);`
'print': redefinition; previous definition was 'data variable' (at the definition of print)
我的节目
void print(Entity* e);
class Entity {
public:
int x, y;
Entity(int x, int y) {
this->x = x;
this->y = y;
print(this);
}
};
void print(Entity* e)
{
cout << e->x << " " << e->y << endl;
}
int main()
{
Entity e(1, 2);
return 0;
}
作废打印(实体*e);
类实体{
公众:
int x,y;
实体(整数x,整数y){
这个->x=x;
这->y=y;
打印(本);
}
};
作废打印(实体*e)
{
cout x在您的第一行代码中,编译器不知道什么是实体
。因此,在代码顶部添加以下行:
class Entity;
在您的前向函数声明“代码>打印”(<代码> >代码>实体< /代码>中,编译器不知道,因为前面没有前面类声明的类定义。请查看C++编译器(例如)和调试器(例如……)的文档和文档。