类功能定义但不称为(C )
Class function is defined but not to be called(C++)
我想从" Hello"中调用公共功能,但是此错误不会让我。请告诉我我在做什么错?
这是代码:
#include <iostream>
using std::cout;
using std::cin;
using std::string;
class hello {
public:
void sayit(){
cout << "Hello, World!";
}
};
int main(){
string str;
cout << "Type in "start":";
cin >> str;
if (str == "start"){
//this is where the error happens.
hello.sayit();
}
}
这是错误:
[错误]预期'。令牌
您需要制作sayIt()
static
或声明hello
对象,然后调用method
。静态方法看起来像static void sayIt()
,您将其称为hello.sayIt()
,因为静态方法不需要类的实例才能工作。如果使用hello
对象,则将像此hello x; x.sayIt()
一样完成。
void sayit()
定义了类成员函数。
要调用类成员函数,您需要类的实例。
hello hello_instance;
hello_instance.sayit();
作为替代方案,您可以将sayit
更改为静态功能。静态功能是类的成员,但不需要类的实例。结果,this
变量在static
方法中不存在,而非静态成员变量无法使用。
static void sayit(){
cout << "Hello, World!";
}
不使用任何成员变量,可以使用
来调用hello::sayit();
相关文章:
- 类模板的成员功能的定义在单独的TU中完全专业化
- C++ - 没有自定义交换功能的移动分配运算符?
- 如何定义可变参数类模板的成员模板功能
- '_HAS_CXX17'宏是否可用于自定义项目标头以启用C++17 语言集功能?
- C++模板功能并定义特定情况
- 这种错误的原因是什么:将"功能"重新定义为不同类型的符号
- 功能原型,没有定义
- 如何在cmake工具链文件中设置编译功能,以便已知的自定义编译器使用target_compile_features
- 如何将标准::矢量插入具有自定义排序功能的 std::set 中
- C++ 添加编译器警告,以错误使用自定义打印/日志功能
- 为什么此C 功能定义不需要卷曲括号
- 使用clang获取功能定义
- 使用正则匹配功能定义
- 类功能定义但不称为(C )
- 选择性地将功能定义添加到Python名称空间
- C 使用功能定义中的if/其他语句以返回最小的数字
- C 功能定义和可变声明不匹配
- 全局功能定义
- Intellisense中缺少的功能定义C 2005-2013
- 头文件和源文件的功能定义