未定义的函数引用(c ++)

时间:2015-12-08 03:37:44

标签: c++ function reference undefined

我在询问之前搜索了这个问题,我发现每个人都在使用.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;
}

如果我可以在任何地方详细说明,我会尽力编辑/评论。

4 个答案:

答案 0 :(得分:3)

您声明了一个函数void start();。你用start();称呼它。

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

您遇到与process()类似的问题,依此类推。

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

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

float start();

在原型和函数定义中;你应该在函数内部有float balance;,然后以return balance;结束。然后你就像main

那样打电话给balance = start();

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

答案 1 :(得分:3)

检查您的start功能,如上所述M.M.你应该得到这样的东西:

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"

答案 2 :(得分:0)

您可以定义以下功能:

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)的函数实现时,它找到>> import arcpy - will work but typing >> arcpy. - will instantly generate Traceback (most recent call last): File "C:\Python27\ArcGIS10.3\lib\SocketServer.py", line 295, in _handle_request_noblock Exception happened during processing of request from ('127.0.0.1', 59487) self.process_request(request, client_address) File "C:\Python27\ArcGIS10.3\lib\SocketServer.py", line 321, in process_request self.finish_request(request, client_address) File "C:\Python27\ArcGIS10.3\lib\SocketServer.py", line 334, in finish_request self.RequestHandlerClass(request, client_address, self) File "C:\Python27\ArcGIS10.3\lib\SocketServer.py", line 653, in __init__ self.finish() File "C:\Python27\ArcGIS10.3\lib\SocketServer.py", line 712, in finish self.wfile.close() File "C:\Python27\ArcGIS10.3\lib\socket.py", line 279, in close self.flush() File "C:\Python27\ArcGIS10.3\lib\socket.py", line 303, in flush self._sock.sendall(view[write_offset:write_offset+buffer_size]) error: [Errno 10053] An established connection was aborted by the software in your host machine 并得出结论这些不是足够接近的匹配

答案 3 :(得分:0)

请使用此代码。这对你有帮助。

#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;
}