问题描述
我正在尝试编译我的代码以测试读取和打印数据文件的功能,但是我收到了一个我不理解的编译错误-错误:'之前的预期构造函数,析构函数或类型转换; '令牌。
I'm trying to compile my code to test a function to read and print a data file, but I get a compiling error that I don't understand - "error: expected constructor, destructor, or type conversion before ';' token". Wall of relevant code-text is below.
struct Day
{
int DayNum;
int TempMax;
int TempMin;
double Precip;
int TempRange;
};
struct Month
{
Day Days[31];
int MonthMaxTemp;
int MonthMinTemp;
double TotalPrecip;
int MonthMaxTempRange;
int MonthMinTempRange;
double AverageMaxTemp;
double AverageMinTemp;
int RainyDays;
double AveragePrecip;
}theMonth;
double GetMonth();
double GetMonth()
{
for (int Today = 1; Today < 31; Today++)
{
cout << theMonth.Days[Today].TempMax << theMonth.Days[Today].TempMin;
cout << theMonth.Days[Today].Precip;
}
return 0;
}
GetMonth(); // compile error reported here
推荐答案
出现错误的行看起来您正在尝试调用GetMonth,但是在全局级别,一个C ++程序由一系列声明组成。由于函数调用不是声明,因此不能在全局级别孤立存在。您可以有一个声明,该声明也是一个定义,在这种情况下,它可以调用函数作为初始化的一部分。
The line with the error looks like you're trying to call GetMonth -- but at the global level, a C++ program consists of a series of declarations. Since a function call isn't a declaration, it can't exist in isolation at the global level. You can have a declaration that's also a definition, in which case it can invoke a function as part of initialization.
一个函数调用本身必须包含在其他函数中:
A function call by itself, however, has to be contained within some other function:
#ifdef TEST
int main() {
GetMonth();
}
#endif
这篇关于错误:“;”之前的预期构造函数,析构函数或类型转换令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!