需要有关在 C++ 中的函数 display() 上查找未定义引用的帮助

Need help on finding an undefined reference on function display() in C++

本文关键字:查找 帮助 引用 未定义 display 函数 C++      更新时间:2023-10-16

我的任务目标是将美元转换为欧元。我需要做的就是提示用户输入,然后使用函数获取输出 我是编码新手,对于我的生活,我找不到我在声明这个函数时出错的地方。任何建议都值得赞赏。

#include <iostream>
using namespace std;
double getmoney();
double convert();
double display();
int main()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
getmoney();
display();

return 0;
}
double getmoney()
{
double money;
cout << "Please enter the amount in US Dollars: ";
cin >> money;
return money;
}
double convert(double money)
{
double euros;
euros = money * 1.41;
return euros;
}
double display(double money)
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
double salad = convert(money);
cout << getmoney() << endl;
if (money >= 0)
cout << "Euros: " << salad;
else (money < 0);
cout << "Euros: "  << "(" << salad << ")";
return 0;
}

首先,您将 display 声明为一个不接受任何参数的函数。

double display();

但是,当您定义函数时,该函数将采用双精度类型的参数。

double display(double money)

声明和定义应匹配,以便编译器知道在代码中使用函数时要跳转哪个函数。

在您的情况下,由于声明和定义不同,当您在main()函数中使用display()时,编译器不知道在哪里跳转。因此未定义的参考错误。

名为"display"的函数在顶部声明为在函数调用中没有参数。

double display();

然后稍后你定义这个函数,就像它应该接收一个参数一样。

double display(double money);

编译器不知道如何处理这些冲突,因此会将其视为第一个没有定义的函数。

[编辑:在我看到几乎相同的第一个答案后立即发布]