语法不标准的问题



我正在尝试学习c++,当我尝试运行我正在创建的这个简单程序时,我得到以下错误。

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

我不太确定从这里该去哪里。搜索问题似乎指向需要做一些静态或常量,但我不太确定。如有任何帮助,我将不胜感激。

使用visual studio社区和Windows 10以下文件:

. h文件
#pragma once
#include <iostream>
#include <string>
#include <ostream>
#include <list>
#include <algorithm>
#include <numeric>
#include <windows.h>
#include <shellapi.h>
using namespace std;

#ifndef BUDGET_H
#define BUDGET_H

class budget {

private:
float mortgage, utilities, groceries, car_payment, misc, income;

public:
budget();
budget(budget& rhs);
~budget();
budget(float mortgage, float utilities, float groceries, float car_payment, float misc, float income);
void input();
void display() const;
bool check_mort() const;
bool check_util() const;
bool check_groc()const;
bool check_car()const;
void additional_resc();

};
#endif // !BUDGET_H

. cpp文件
#include "budget.h"

budget::budget() : mortgage{0}, utilities{0}, groceries{0}, car_payment{0}, misc{0}, income{0} { }
budget::budget(budget& rhs): mortgage {rhs.mortgage}, utilities{rhs.utilities}, groceries{rhs.groceries}, car_payment{rhs.car_payment}, misc{rhs.misc}, income{rhs.income}{}

budget::budget(float mort, float util, float groc, float car, float mis, float inco) : mortgage{ mort }, utilities{ util }, groceries{ groc }, car_payment{ car }, misc{ mis }, income{ inco } {
}
budget::~budget(){}
void budget::input() {
cout << "Enter your mortgage payment: ";
cin >> mortgage;

cout << "Enter your utilities payment: ";
cin >> utilities;
cout << "Enter the amount you spend on grocieries for a month: ";
cin >> groceries;
cout << "Enter the total amount that you pay in car payments each month: ";
cin >> car_payment;
cout << "Enter the total sum of any other expenses, such as phone bill, insurance, contribution to savings, etc. : ";
cin >> misc;
cout << "Enter your total income: ";
cin >> income;
}
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::additional_resc() {
ShellExecute(0, 0, L"https://www.thebalance.com/start-getting-out-of-debt-960852", 0, 0, SW_SHOW);
}
bool budget::check_mort() const {
if (mortgage > 1400){
return true;
}
else {
return false;
}
}
bool budget::check_util() const {
if (utilities > 114) {
return true;
}
else {
return false;
}
}
bool budget::check_groc() const {
if (groceries > 412) {
return true;
}
else {
return false;
}
}
bool budget::check_car() const {
if (car_payment > 640) {
return true;
}
else {
return false;
}
}

主文件

#include "budget.h"
int main()
{
int I = 0;
char choice = 'n';
budget Budget;
while (I == 0) {
cout << "Hello, thank you for using your budget butler. /n";
cout << "Please enter the following to calculate your budget: ?/n";
Budget.input();
Budget.display();
cout << "Would you like to learn more about ways to reduce your debt? y or n";
cin >> choice;
if (choice == 'y') {
Budget.additional_resc();
}
cout << "What would you like to do now? /n/n/n";
cout << "0. Quit Program?n";
cout << "1. Enter new budget informationn";

cin >> I;
if (I == 0) {
I++;
}
else if (I == 1) {
budget Budget1;
Budget1.input();
Budget1.display();
}
}


return 0;
}

budget::display()中,您的if语句没有调用类方法来评估其返回值。相反,它们以非标准的方式计算方法的内存地址,因此会出现错误。

调用方法时缺少圆括号,例如:

if (check_mort()) {
...
}
if (check_car()) {
...
}
if (check_groc()) {
...
}
if (check_util()) {
...
}

最新更新