c++ - 从类方法 C++ 调用非类函数

标签 c++ compiler-errors

关闭。这个问题是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/

相关文章:

c++ - 不是类或 namespace 名称-除外

c++ - 为什么全局 merge() 函数与 std::merge() 冲突?

c++ - "error: sizeof ' x' 的原因在 C++ 中未知或为零?

c++ - 如何知道导致异常的确切代码行?

Swift:比较泛型类中的泛型类型

c++ - 带有提示的 std::map::insert_or_assign 的复杂性

c++ - 模板类中没有匹配的函数调用

visual-studio-2013 - 在另一个项目中找不到 C# 类型或命名空间名称

c++ - 为什么在 "const"中忽略 "const typename iterator_traits<RandomIterator>::reference"?

c++ - 使用链接查找数组中的最大值