gpt4 book ai didi

c++ - "syntax error : missing ' ; ' before ' { '"in c++ project with precompiled header

转载 作者:太空宇宙 更新时间:2023-11-04 15:18:47 25 4
gpt4 key购买 nike

尝试编译简单的 C++ 项目时,出现以下错误:

syntax error : missing ';' before '{'

我做了我的研究。编译器不允许在 for 循环内声明变量,这对我来说很不方便。如果我在 for 循环之前声明循环变量,错误就会消失。

这是一个带有“stdafx.h”预编译 header 的 C++ 项目。

这种编译器行为 (C89) 是由于我使用的是预编译 header 还是我过去使用我的配置编写了一些东西而强制执行的?有什么办法可以避免这种行为?

附言我在 Windows 7 64 位上使用 visual studio 2012;

代码示例:

  1. 错误在第一行

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

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

最佳答案

一起去

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

这在 for 循环的第一条语句中声明了两个 int 类型的变量。

此方法有效而您的其他尝试无效的原因如下:

每个声明语句都必须用;彼此分隔,但是相同的;用于分隔for(;;) 循环的 header ,因此您尝试使用“,”运算符绕过它。由于语法错误而无法正常工作

关于c++ - "syntax error : missing ' ; ' before ' { '"in c++ project with precompiled header,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24462797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com