c++ - 函数 "was not declared in this scope"

标签 c++

我是 C++ 新手,犯了初学者的错误:

myclass.cpp: In function ‘int main()’:
myclass.cpp: 14:16: error: ‘func’ was not declared in this scope

这是代码:

#include <iostream>
using namespace std;

class MyClass{
public:
    int func(int);
};

int MyClass::func(int a){
    return a*2;
}

int main(){
    cout << func(3);
}

我希望你能帮助我。

最佳答案

int main(){
    cout << func(3);
}

func 不是全局函数;它是类的成员函数。您需要类的一个实例才能访问它。

例如:

int main()
{
   MyClass obj;
   std::cout<< obj.func(3);
}

关于c++ - 函数 "was not declared in this scope",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16136551/

相关文章:

c++ - C++如何计算复数的绝对值,防止溢出?

python - libclang:添加编译器系统包含路径(Windows 中的 Python)

c++ - 用 setw 对齐文本

c++ - 找不到为 R 构建 C++ 代码所需的工具

c++ 如何通过套接字发送 const std::vector<std::uint8_t>?

使用模板定义多个函数的 C++ 通用方法

c++ - 复制构造函数调用无限循环,尽管通过引用调用

c++ - 模板化类时模板不能是虚拟错误

c++ - 您可以在 boost asio 中设置 SO_RCVTIMEO 和 SO_SNDTIMEO 套接字选项吗?

c++ - 许多具有相同构造函数的类