好的,所以我对此非常陌生,我正在尝试创建一个计算任意数量长途电话费用的程序。我还没有走得太远,我一直试图弄清楚如何让函数重复自己。现在我收到一个错误说
第18行在'{'标记和预期','或';'之前不允许使用函数定义在'{'令牌之前。
第18行是 void costCalc(int numCalls)
到目前为止,这是我的代码:
#include<iostream>
using namespace std;
int main()
{
// Declare and initialize all variables
int numCalls = 0;
int length = 0;
int hour = 0;
char day = ' ';
char dest = ' ';
double cost = 0.0;
cout<<"Enter the number of calls: ";
cin>>numCalls;
void costCalc(int numCalls)
{
if (numCalls > 0)
{
cout<<"Enter length of call in minutes: ";
cin>>length;
costCalc(numCalls-1);
}
}
// Request the number of calls from the user
// Loop for the requested number of calls:
// Request the user to give you the call length,
// call day of week and hour of call, and call
// destination
// Instantiate and initialize a Call object
// using either
// a) the 4-parameter constructor, OR
// b) the default constructor and each of the
// set member functions.
// Get the cost of the call using the calcCallCost
// function and add it to the total cost of the calls.
// Use the callMsg function to print a message about the call
// end loop
// Report the total cost of all the calls.
system("pause");
return 0;
}
答案 0 :(得分:0)
您应该在开始新功能之前关闭主功能。
您正在尝试在main中创建嵌套函数,而C或C ++不支持该函数。
您只需在main函数开头之前复制/粘贴costCalc函数的代码。
作为附注,很久以前(1994)gcc(2.95.2)支持嵌套函数作为扩展。但它从未进入C或C ++标准。
我不确定为什么嵌套函数永远不会进入标准。它接缝很简单以支持(我们可能必须使嵌套函数内联静态,但仍然)。也许是因为它会引发一些链接器和编译单元问题。
这实际上并不是什么大问题,大多数用例都可以使用命名空间来解决。
答案 1 :(得分:0)
这会让你继续参加演出。
#include <iostream>
#include <iomanip> // for setprecision(2)
using namespace std;
class Call {
private: double length;
public:
// Instantiate and initialize a Call object
// using either
// a) the 4-parameter constructor, OR
Call(double theLength)
{
this->length = theLength;
}
// b) the default constructor and each of the
// set member functions.
Call() { }
void SetLength(double someLength)
{
this->length = someLength;
}
double GetLength()
{
return this->length;
}
};
double costCalc(double costPerMinute, int length)
{
Call thisCall(length);
return thisCall.GetLength()*costPerMinute;
}
void callMsg()
{
cout << "This is a message about the call" << endl;
}
int main()
{
int numCalls = 0;
int length = 0;
double cost = 0.0;
double costPerMinute = 0.05;
// Request the number of calls from the user
cout << "Enter the number of calls: ";
cin >> numCalls;
// Loop for the requested number of calls:
for (int i = 0; i < numCalls; i++)
{
// Request the user to give you the call length,
cout << "Enter length of call " << i << " in minutes: ";
cin >> length;
// Get the cost of the call using the calcCallCost
// function and add it to the total cost of the calls.
cost += costCalc(costPerMinute, length);
// Use the callMsg function to print a message about the call
callMsg();
} // end loop
// Report the total cost of all the calls.
cout << "Total cost is $" << setprecision(2) << cost << endl;
return 0;
}
编译/运行:
g++ cost.cpp
$ ./a.out
Enter the number of calls: 2
Enter length of call 0 in minutes: 1
This is a message about the call
Enter length of call 1 in minutes: 2
This is a message about the call
Total cost is $0.15