非标准语法问题

-1 c++ syntax static pointers constants

我正在尝试学习 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
Run Code Online (Sandbox Code Playgroud)

我不太确定从这里该去哪里。搜索问题似乎表明需要将某些东西设为静态或常量,但我不太确定。任何帮助将非常感激。

使用 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
Run Code Online (Sandbox Code Playgroud)

.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;
    }

}

Run Code Online (Sandbox Code Playgroud)

主文件

#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 information\n";


        cin >> I;

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

    }




    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Rem*_*eau 6

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

您缺少调用方法的括号,例如:

if (check_mort()) {
      ...
}
if (check_car()) {
      ...
}
if (check_groc()) {
      ...
}
if (check_util()) {
      ...
}
Run Code Online (Sandbox Code Playgroud)