未定义的函数引用 (c++)
undefined reference to functions (c++)
我问这个问题之前搜索了一下,我发现每个人都在使用 .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 0 - $"<<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 0 - $"<<fiveserv<<endl;
servcharge = (fiveserv+cserv);}
cout<<"Total service charges: $"<<servcharge<<endl;
servchargetotal = servchargetotal + servcharge;
}
如果有什么可以详细说明的地方,我会尽力edit/comment。
你声明了一个函数void start();
。你用 start();
.
来称呼它
但是您从未为此函数提供函数体。您定义为 void start(float balance)
的函数是一个 不同的函数 。在 C++ 中,可能有多个具有相同名称但参数列表不同的函数;这些是不同的功能。
您在 process()
等方面也有类似的问题。
您的函数应该传递和返回它们使用的变量,但您实际上并没有编写函数来执行此操作。 void start(float balance)
中的 balance
与 main()
中的 float balance
是不同的变量;您在此函数中所做的更改不会影响 main
.
中的 float balance;
要修复此特定功能,应该是:
float start();
在原型和函数定义中;你应该在函数内部有 float balance;
,然后以 return balance;
结束。然后你像这样调用 main
:balance = start();
您的课程材料应该涵盖将值传递给函数和从函数返回值,您需要参考它们来修复您的其他函数。
您定义了以下函数:
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)
并得出结论,这些不是关闭的足够匹配。
请使用此代码。这对你有帮助。
#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 0 - $"<<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 0 - $"<<fiveserv<<endl;
servcharge = (fiveserv+cserv);}
cout<<"Total service charges: $"<<servcharge<<endl;
servchargetotal = servchargetotal + servcharge;
}
按照 M.M 检查您的 start
函数。你应该得到这样的东西:
float start() {
// ...
float balance;
cin >> balance;
return balance;
}
int main() {
float n;
n = start();
// ...
return 0;
}
并逐步检查所有函数,因为您在下一个 process
函数中遇到问题:在 if 语句中必须是比较运算符 if (type == "C" || type == "c")
而不是赋值 type = "C"||"c"
.
我问这个问题之前搜索了一下,我发现每个人都在使用 .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 0 - $"<<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 0 - $"<<fiveserv<<endl;
servcharge = (fiveserv+cserv);}
cout<<"Total service charges: $"<<servcharge<<endl;
servchargetotal = servchargetotal + servcharge;
}
如果有什么可以详细说明的地方,我会尽力edit/comment。
你声明了一个函数void start();
。你用 start();
.
但是您从未为此函数提供函数体。您定义为 void start(float balance)
的函数是一个 不同的函数 。在 C++ 中,可能有多个具有相同名称但参数列表不同的函数;这些是不同的功能。
您在 process()
等方面也有类似的问题。
您的函数应该传递和返回它们使用的变量,但您实际上并没有编写函数来执行此操作。 void start(float balance)
中的 balance
与 main()
中的 float balance
是不同的变量;您在此函数中所做的更改不会影响 main
.
float balance;
要修复此特定功能,应该是:
float start();
在原型和函数定义中;你应该在函数内部有 float balance;
,然后以 return balance;
结束。然后你像这样调用 main
:balance = start();
您的课程材料应该涵盖将值传递给函数和从函数返回值,您需要参考它们来修复您的其他函数。
您定义了以下函数:
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)
并得出结论,这些不是关闭的足够匹配。
请使用此代码。这对你有帮助。
#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 0 - $"<<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 0 - $"<<fiveserv<<endl;
servcharge = (fiveserv+cserv);}
cout<<"Total service charges: $"<<servcharge<<endl;
servchargetotal = servchargetotal + servcharge;
}
按照 M.M 检查您的 start
函数。你应该得到这样的东西:
float start() {
// ...
float balance;
cin >> balance;
return balance;
}
int main() {
float n;
n = start();
// ...
return 0;
}
并逐步检查所有函数,因为您在下一个 process
函数中遇到问题:在 if 语句中必须是比较运算符 if (type == "C" || type == "c")
而不是赋值 type = "C"||"c"
.