大家经常犯的一个错误 函数调用缺少参数列表;请使用“&Student::Printf”创建指向成员的指针
例如代码:
#include<iostream>
using namespace std;
struct Student
{
char _name[20];
char _sex[6];
int _age;
void InitStudent(char* pname, char* psex, int age)
{
strcpy_s(_name, pname);
strcpy_s(_sex, psex);
_age = age;
}
void Printf()
{
cout << _name << " " << _sex << " " << _age << endl;
}
};
int main()
{
Student std1;
std1.InitStudent("dandna", "women", 20);
std1.Printf;
system("pause");
return 0;
}编译后提示:
这是因为在调用Printf时忘记加括号。正确的是std.Printf()
本文介绍了一个常见的编程错误——函数调用时遗漏括号,并通过一个具体的C++示例进行了解释。示例中定义了一个Student结构体,包含了初始化成员变量的方法InitStudent和打印成员变量的方法Printf。在main函数中,调用Printf方法时未添加括号导致编译错误。

被折叠的 条评论
为什么被折叠?



