Cal*_*nst 1 c++ methods compiler-errors function
我正在开发一个程序,该程序需要大量的秒数,并试图将它们分解为数天、数小时、数分钟和数秒。作为 C++ 的新手,我试图将这个问题分成 3 种方法:main(从用户处获取秒数,并将结果传递给“计算”)、计算(尝试将值除以天/小时/分钟,然后通过这些方法)到“结果”),& 结果(打印结果)。
目前我遇到了编译器返回的问题:
错误 C2084:函数“void 计算(__int64)”已经有一个函数体
除此之外,我一直在环顾四周,并注意到很多人在处理这些东西时使用“头文件”;所以我的第二个问题是必要的,有没有办法避免它(我只需要提交 1 个 .cpp 文件,所以我的主文件之外的任何东西都必须是不可能的)。
以下是我到目前为止的代码,
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
//method declarations
void calculation(long long int value) {};
void results(int days, int hours, int mins, int secs) {};
int main()
{
//Declaration
long long int seconds;
//user prompt
cout << "Enter seconds" << endl;
cin >> seconds;
//check for valid input
if (seconds > 0)
{
cout << endl << "Total seconds: " << seconds;
calculation(seconds);
}
else
{
cout << "Total seconds must be greater than zero";
}
}
void calculation(long long int value)
{
//divisor values
const int secToDay = 86400;
const int secToHour = 3600;
const int secToMin = 60;
//calculated vars
int d, h, m, s;
//calculating if there are any days in this
d = value / secToDay;
if (d != 0)
value -= (secToDay * d);
//calculating if there's any hours
h = value / secToHour;
if (h != 0)
value -= (secToHour * h);
//calculating minutes
m = value / secToMin;
if (m != 0)
value -= (secToMin * m);
//whatever's left over goes to seconds
s = value;
results(d, h, m, s);
}
void results(int days, int hours, int mins, int secs)
{
cout << days << " day(s)" << endl;
cout << hours << " hours(s)" << endl;
cout << mins << " minute(s)" << endl;
cout << secs << " second(s)" << endl;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
{}
从您的声明中删除。
void results(int days, int hours, int mins, int secs) {};
Run Code Online (Sandbox Code Playgroud)
这称为函数定义。
C++ 函数定义告诉编译器函数的实际主体。函数定义也是一个声明。
认为这与
void results(int days, int hours, int mins, int secs)
{
}
Run Code Online (Sandbox Code Playgroud)
注意:;
函数定义后不需要分号
void results(int days, int hours, int mins, int secs);
Run Code Online (Sandbox Code Playgroud)
这是一个函数声明。
函数声明告诉编译器函数的名称、返回类型和参数。函数声明不是定义。
为了让您的代码正确编译,请将您的定义更改为声明。
简而言之,删除{}
.
归档时间: |
|
查看次数: |
67 次 |
最近记录: |