C++程序编译报错C3867:非标准语法问题求助
解决C++编译错误C3867:成员函数调用语法问题
错误信息
Error C3867 'budget::check_mort': non-standard syntax; use '&' to create a pointer to member Error C3867 'budget::check_car': non-standard syntax; use '&' to create a pointer to member Error C3867 'budget::check_groc': non-standard syntax; use '&' to create a pointer to member Error C3867 'budget::check_util': non-standard syntax; use '&' to create a pointer to member
问题根源
错误出在budget类的display()成员函数中。你直接使用check_mort、check_car等标识符,这在C++中会被解释为成员函数指针,而非调用该成员函数。编译器提示你用&取指针,但这不是你想要的——你需要的是执行这些函数并获取返回的布尔值。
修复方案
在display()函数中,所有调用check_*系列函数的地方,都需要加上括号()来明确是函数调用,而不是取指针。
修改前的错误代码片段:
void budget::display() const{ float x = mortgage + utilities + groceries + car_payment + misc; cout << "The sum of all of your expenses is " << x << endl; cout << "Based on your inputs: "; if (check_mort) { cout << "Your mortgage is higher than the national average./n Consider taking steps to reduce this such as refinancing or relocating./n "; } if (check_car) { cout << "Your car payments are higher than the average. Consider finding alternate transportation and downgrading/n"; } if (check_groc) { cout << "Your grocieries are higher than the average. Consider finding cheaper alternatives and not eating out./n"; } if (check_util) { cout << "Your utilities cost is higher than the average. Consider finding ways to reduce usage, such as raising or lowering the thermometer./n"; } }
修改后的正确代码片段:
void budget::display() const{ float x = mortgage + utilities + groceries + car_payment + misc; cout << "The sum of all of your expenses is " << x << endl; cout << "Based on your inputs: "; if (check_mort()) { cout << "Your mortgage is higher than the national average.\n Consider taking steps to reduce this such as refinancing or relocating.\n "; } if (check_car()) { cout << "Your car payments are higher than the average. Consider finding alternate transportation and downgrading\n"; } if (check_groc()) { cout << "Your groceries are higher than the average. Consider finding cheaper alternatives and not eating out.\n"; } if (check_util()) { cout << "Your utilities cost is higher than the average. Consider finding ways to reduce usage, such as raising or lowering the thermometer.\n"; } }
同时顺便修正了两处小问题:
- 将错误的换行符
/n改为标准的\n - 修正了拼写错误
grocieries为groceries
额外说明
你之前推测需要将函数设为static或const是不必要的——这些check_*函数已经被声明为const(符合类成员函数访问const对象的要求),且它们需要访问类的私有成员变量,因此不能设为static。
内容的提问来源于stack exchange,提问作者Sir Davahoy
相关产品推荐
相关产品推荐

