C++ 错误:';' 之前的预期构造函数、析构函数或类型转换 令牌?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1573168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:29:17  来源:igfitidea点击:

Error: expected constructor, destructor, or type conversion before ';' token?

c++compiler-constructiong++

提问by Owen Pierce

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

回答by Jerry Coffin

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.

带有错误的那一行看起来像是您正在尝试调用 GetMonth —— 但在全局级别,C++ 程序由一系列声明组成。由于函数调用不是声明,因此它不能在全局级别孤立存在。您可以有一个也是定义的声明,在这种情况下,它可以调用函数作为初始化的一部分。

A function call by itself, however, has to be contained within some other function:

然而,一个函数调用本身必须包含在其他一些函数中:

#ifdef TEST
int main() { 
    GetMonth();
}
#endif

回答by AnT

(In addition to other replies.) In order to excute your 'GetMonth()' function you have to either call it from another function ('main' or whatever is called from 'main') or use it in initializer expression of an object declared at namespace scope, as in

(除了其他回复。)为了执行您的“GetMonth()”函数,您必须从另一个函数(“main”或从“main”调用的任何内容)调用它,或者在对象的初始化表达式中使用它在命名空间范围内声明,如

double global_dummy = GetMonth();

However, the latter method might suffer from initialization order problems, which is why it is recommended to use the former method whenever possible.

但是,后一种方法可能会遇到初始化顺序问题,这就是为什么建议尽可能使用前一种方法的原因。

回答by JaredPar

In C/C++, you cannot simply add executable code into the body of a header or implementation (.c,.cpp,.cxx,etc...) file. Instead you must add it to a function. If you want to have the code run on startup, make sure to add it to the main method.

在 C/C++ 中,您不能简单地将可执行代码添加到头文件或实现(.c、.cpp、.cxx 等)文件的正文中。相反,您必须将其添加到函数中。如果您想让代码在启动时运行,请确保将其添加到 main 方法中。

int main(int argc, char *argv[]) {
  GetMonth();
}

回答by Andres Jaan Tack

C++ programs don't execute in a global context. This means you need to put the call to GetMonth into a function for it to run. int main() { }might be appropriate.

C++ 程序不在全局上下文中执行。这意味着您需要将对 GetMonth 的调用放入一个函数中才能运行。int main() { }可能是合适的。