简体   繁体   English

“语法错误:缺少';' 在带有预编译标头的c ++项目中,在“ {””之前

[英]“syntax error : missing ';' before '{'” in c++ project with precompiled header

When trying to compile simple c++ project, I get the following error: 尝试编译简单的c ++项目时,出现以下错误:

syntax error : missing ';' before '{'

I did my research. 我做了研究。 Compiler doesn't allow declaration of variables inside of a for loop, which is very inconvenient for me. 编译器不允许在for循环内声明变量,这对我来说非常不便。 If I declare loop variables before the for loop, the error disappears. 如果我在for循环之前声明循环变量,则错误消失。

This is a c++ project with "stdafx.h" precompiled header. 这是一个带有“ stdafx.h”预编译头的c ++项目。

Is this compiler behavior (C89) enforced by the fact that I'm using precompiled header or did I scr**ed something up with my config in the past ? 是因为我使用的是预编译头文件,还是由于过去使用我的配置对某些内容进行了编排而导致了这种编译器行为(C89)? Is there any way of circumventing this befavior ? 有什么办法可以规避这种行为?

PS I'm using visual studio 2012 on windows 7 64-bit; PS我正在Windows 7 64位上使用Visual Studio 2012。

Code samples: 代码示例:

  1. error is on the first line 错误在第一行

     for (int idx = 0, int i = 100; idx < (sizeof(anTestScores) / sizeof(int)); i++, idx++) { anTestScores[idx] = i; } 
  2. this compiles 这编译

     int idx; int i; for (idx = 0, i = 100; idx < (sizeof(anTestScores) / sizeof(int)); i++, idx++) { anTestScores[idx] = i; } 

Go with 一起去

for (int idx = 0, i = 100; idx < (sizeof(anTestScores) / sizeof(int)); i++, idx++) { anTestScores[idx] = i; }

This declares two variables of type int in the first statement of the for loop. 这在for循环的第一条语句中声明了两个 int类型的变量。

The reason this works and your other attempt doesn't is as follows: 起作用的原因而您的其他尝试不起作用的原因如下:

Each declaration statement has to be separated from each other by a ; 每个声明语句必须用分隔; , but the same ; ,但是一样; is used to separate parts of the for(;;) loop's header so you tried to get around it by using the ',' operator. 用于分隔for(;;)循环标题的各个部分,因此您尝试通过使用','运算符来解决它。 Which didn't work due to syntax error 由于语法错误而无法使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM