对函数的未定义引用 (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 $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)
中的balance
与 main()
内部的float balance
不同;您在此功能中所做的更改不会影响float balance;
main
。
要修复此特定功能,它应该是:
float start();
在原型和函数定义中;你应该在函数内部有float balance;
,然后以return balance;
结尾。然后你像这样打电话给main
:balance = 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)
并得出结论,这些不是足够接近的匹配。
- 将对象数组的引用传递给函数
- 什么时候在C++中返回常量引用是个好主意
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 何时在引用或唯一指针上使用移动语义
- 如何在c++中使用引用实现类似python的行为
- 编译C++时未定义的引用
- Ctypes wstring通过引用传递
- c++r值引用应用于函数指针
- 理解c++中的引用
- C++取消引用指针.为什么会发生变化
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 强制转换为引用类型
- 引用一个已擦除类型(void*)的指针
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 具有默认值的引用获取函数
- 如何使用基类指针引用派生类成员
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- 如何引用基类的派生类?