关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。
想改进这个问题?将问题更新为 on-topic对于堆栈溢出。
1年前关闭。
Improve this question
我想调用函数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;
}
最佳答案
在您的第一行代码中,编译器不知道 Entity
是什么。是。因此,将以下行添加到代码的顶部:
class Entity;
关于c++ - 从类方法 C++ 调用非类函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64415970/