对函数的未定义引用 (C++)

undefined reference to functions (c++)

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

>我在询问之前搜索了这个问题,我发现每个人都在使用.h文件来实现他们的功能,而我的老师教我们将main和函数放在一个文件中。

我只是想弄清楚为什么它是未定义的。我知道这段代码中可能潜伏着其他问题,但我真的无法弄清楚我做错了什么。

这是我的代码:

#include <iostream>
#include <iomanip>
using namespace std;
void start();
void process();
void check();
void deposit();
const float dserv = 0.10;
const float cserv = 0.15;
const float fiveserv = 5.0;
const float oserv = 10.0;
int main()
{
float balance;
float amount;
bool fivehun;
bool endcheck;
float servcharge;
float servchargetotal;
char type;
start();
while(endcheck != true)
{process();}
cout<<"Current balance: $"<<balance<<endl;
cout<<"Total service charges: $"<<servchargetotal<<endl;
cout<<"Final balance: $"<<
system("pause");
return 0;
}
void start(float balance)
{
    balance = 0;
    cout<<fixed<<showpoint<<setprecision(2);
    cout<<"Transactions will take the form of a letter followed by a dollar ";
    cout<<"amount. Valid letters are “C” for a check, “D” for a deposit, and";
    cout<<"“E” for the ending transaction (use zero on this transaction).";
    cout<<"Press <Enter> after each line of input";
    cout<<"Enter the beginning balance:"<<endl;
    cin>>balance;
}
void process(float balance, float amount, char type, bool endcheck)
{
    cout<<"Enter a transaction:"<<endl;
    cin>>type>>amount;
    if (type = "C"||"c")
    {check();
    endcheck = false;}
    else if(type = "D"||"d")
    {deposit();
    endcheck = false;}
    else if(type= "E"||"e")
    {endcheck = true;}
}
void check(float balance, float amount, bool fivehun, float servcharge, float& servchargetotal)
{
    balance = balance - amount;
    servcharge = cserv;
    if (balance<500.00)
    {fivehun = true;}
    else
    {fivehun = false;}
    cout<<"Transaction: Check in amount of $"<<amount<<endl;
    cout<<"Current balance: $"<<balance<<endl;
    cout<<"Service charge: Check - $"<<cserv<<endl;
    if (fivehun == true)
    {cout<<"Service charge: Below $500 - $"<<fiveserv<<endl;
     servcharge = (fiveserv+servcharge);}
    cout<<"Total service charges: $"<<servcharge<<endl;
    servchargetotal = servchargetotal + servcharge;
}
void deposit(float balance, float amount, bool fivehun, float servcharge, float& servchargetotal)
{
    balance = balance + amount;
    servcharge = dserv;
    if (balance<500.00)
    {fivehun = true;}
    else
    {fivehun = false;}
    cout<<"Transaction: Deposit in amount of $"<<amount<<endl;
    cout<<"Current balance: $"<<balance<<endl;
    cout<<"Service charge: Check - $"<<cserv<<endl;
    if (fivehun == true)
    {cout<<"Service charge: Below $500 - $"<<fiveserv<<endl;
     servcharge = (fiveserv+cserv);}
    cout<<"Total service charges: $"<<servcharge<<endl;
    servchargetotal = servchargetotal + servcharge;
}

如果有什么地方可以详细说明,我会尝试编辑/评论任何我能做的事情。

检查你的start函数,如M.M.所述,你应该得到这样的东西:

float start() {
    // ...
    float balance;
    cin >> balance;
    return balance;
}

int main() {
    float n;
    n = start();
    // ...
    return 0;
}

并逐步检查所有函数,因为您在下一个函数中遇到问题process:in if 语句必须是比较运算符if (type == "C" || type == "c")而不是赋值type = "C"||"c"

你声明一个函数void start(); .你用start();来称呼它.

但是,您从未提供此功能的正文。 定义为void start(float balance)的函数是不同的函数。在C++中,可能有几个名称相同但参数列表不同的函数;这些是不同的功能。

您在process()等方面也有类似的问题。


您的函数应该传递和返回它们使用的变量,但您实际上并没有编写函数来执行此操作。 void start(float balance) 中的balancemain() 内部的float balance不同;您在此功能中所做的更改不会影响float balance; main

要修复此特定功能,它应该是:

float start();

在原型和函数定义中;你应该在函数内部有float balance;,然后以return balance;结尾。然后你像这样打电话给mainbalance = start();

您的课程材料应涵盖将值传递给函数和从函数返回值,您需要查阅它们以修复其他函数。

请使用此代码。这将对您有所帮助。

#include <iostream>
#include <iomanip>
using namespace std;
void start(float balance);
void process(float balance, float amount, char type, bool endcheck);
void check(float balance, float amount, bool fivehun, float servcharge, float& servchargetotal);
void deposit(float balance, float amount, bool fivehun, float servcharge, float& servchargetotal);
const float dserv = 0.10;
const float cserv = 0.15;
const float fiveserv = 5.0;
const float oserv = 10.0;
int main()
{
float balance;
float amount;
bool fivehun;
bool endcheck;
float servcharge;
float servchargetotal;
char type;
start(balance);
while(endcheck != true)
{process(balance, amount, fivehun, servcharge, servchargetotal);}
cout<<"Current balance: $"<<balance<<endl;
cout<<"Total service charges: $"<<servchargetotal<<endl;
cout<<"Final balance: $"<<
system("pause");
return 0;
}
void start(float balance)
{
balance = 0;
cout<<fixed<<showpoint<<setprecision(2);
cout<<"Transactions will take the form of a letter followed by a dollar ";
cout<<"amount. Valid letters are “C” for a check, “D” for a deposit, and";
cout<<"“E” for the ending transaction (use zero on this transaction).";
cout<<"Press <Enter> after each line of input";
cout<<"Enter the beginning balance:"<<endl;
cin>>balance;
}
void process(float balance, float amount, char type, bool endcheck)
{
cout<<"Enter a transaction:"<<endl;
cin>>type>>amount;
if (type = "C"||"c")
{check(balance, amount, ...);
endcheck = false;}
else if(type = "D"||"d")
{deposit(balance, amount, ...);
endcheck = false;}
else if(type= "E"||"e")
{endcheck = true;}
}
void check(float balance, float amount, bool fivehun, float servcharge, float& servchargetotal)
{
balance = balance - amount;
servcharge = cserv;
if (balance<500.00)
{fivehun = true;}
else
{fivehun = false;}
cout<<"Transaction: Check in amount of $"<<amount<<endl;
cout<<"Current balance: $"<<balance<<endl;
cout<<"Service charge: Check - $"<<cserv<<endl;
if (fivehun == true)
{cout<<"Service charge: Below $500 - $"<<fiveserv<<endl;
 servcharge = (fiveserv+servcharge);}
cout<<"Total service charges: $"<<servcharge<<endl;
servchargetotal = servchargetotal + servcharge;
}
void deposit(float balance, float amount, bool fivehun, float servcharge, float& servchargetotal)
{
balance = balance + amount;
servcharge = dserv;
if (balance<500.00)
{fivehun = true;}
else
{fivehun = false;}
cout<<"Transaction: Deposit in amount of $"<<amount<<endl;
cout<<"Current balance: $"<<balance<<endl;
cout<<"Service charge: Check - $"<<cserv<<endl;
if (fivehun == true)
{cout<<"Service charge: Below $500 - $"<<fiveserv<<endl;
 servcharge = (fiveserv+cserv);}
cout<<"Total service charges: $"<<servcharge<<endl;
servchargetotal = servchargetotal + servcharge;
}

定义以下函数:

void start();
void process();
void check();
void deposit();

您实现:

void start(float balance)
void process(float balance, float amount, char type, bool endcheck)
void check(float balance, float amount, bool fivehun, float servcharge, float& servchargetotal)
void deposit(float balance, float amount, bool fivehun, float servcharge, float& servchargetotal)

定义必须与实现匹配。

或者,如果您将实现放在文件中首次使用(main(之前,则可以删除定义。

函数

调用与定义匹配,但是当编译器(实际上是链接器(寻找看起来像void start()的函数实现时,它找到了void start(float balance)并得出结论,这些不是足够接近的匹配。