试图编写函数的未定义标识符

Undefined identifier trying to write a function

本文关键字:未定义 标识符 函数      更新时间:2023-10-16

我是一名学生,我的任务是创建一个程序来确定一个人是否富有。我的代码在下面,我一直得到一个错误,identifier "isRich" is undefined。有人能告诉我哪里出错了吗?

#include "stdafx.h"
#include <iostream> //for input and output
#include "Cusick Project 5.h"
using namespace std;

void calcWealth(int age, long cash, int dependants, long debt)
{
cout << "Please enter your age: ";
cin >> age;
cout << "Please enter the amount of cash on hand: ";
cin >> cash;
cout << "Please enter the amount of dependents you have: ";
cin >> dependants;
cout << "Please enter the amount of money you owe";
cin >> debt;
bool isRich(int *age, long *cash, int *dependants, long *debt);
{
    long trueCash;
    bool status = false;
    trueCash = cash - debt;
    if (dependants == 0)
    {
        if (trueCash >= 1000000)
        {
            status = true;
        }
        else
            status = false;
    }
    else if (age < 40)
    {
        trueCash = trueCash - (dependants * 150000);
        if (trueCash >= 1000000)
        {
            status = true;
        }
        else
            status = false;
    }
    else if (age > 39 && age < 51)
    {
        trueCash = trueCash - (dependants * 75000);
        if (trueCash >= 1000000)
        {
            status = true;
        }
        else
            status = false;
    }
    else
    {
        trueCash = trueCash - (dependants * 25000);
        if (trueCash >= 1000000)
        {
            status = true;
        }
        else
            status = false;
    }
  }
}
int main()
{
    int age;
    long cash;
    int dependants;
    long debt;
    bool status; 
    cout << "Welcome to the wealth indicator..." << endl;
    calcWealth(age, cash, dependants, debt);
    if (isRich(status) = true)
    {
        cout << "Congratulations!  We consider you as being "rich."" <<     endl;
    }
    else
    {
        cout << "I am sorry!  You are not yet "rich."" << endl;
    }
}

isRich()的声明后面有一个额外的分号。

你的代码应该看起来像:

...    
bool isRich(int *age, long *cash, int *dependants, long *debt)
{
...

此外,在函数calcWealth()的末尾,就在isRich()的声明之前,您缺少一个关闭的}

减去cin语句,您的前几行看起来像

void calcWealth(int age, long cash, int dependants, long debt)
{
// ...
bool isRich(int *age, long *cash, int *dependants, long *debt);
{

但是你不能在 calcWealth中开始声明isRich 。您需要告诉编译器第一个函数已经完成,并使用}

顺便说一句,这个函数无论如何都不会做你想要的——它读取的值会丢失——但这是另一个问题。

作为参考,如果你有一个编译错误,它会附带一个行号。如果不显示它在哪一行出错,你就要求每个人猜测编译器已经告诉你的事情。