输入名称bool不允许.C

type name bool not allowed. c++

本文关键字:不允许 bool 输入      更新时间:2023-10-16

有四个错误显示;我不了解它们。

错误1:不允许键入第11行。

错误2:预期a','第11行(Bool(。

错误3:预期a'}'第11行(在true之后(。

错误4:期望声明第12行(在此之前(。

我是新手程序员,我有点想知道自己在做什么。所以请帮忙。这是代码:

      #include "stdafx.h"
      #include <iostream>
      using namespace std;
    1 int main{ 
    2 bool alternate = true ;
    3 for (int x = 0; x < 8; x++)
    4 {
    5   for (int y = 0; y < 4; y++)
    6   {
    7       if (alternate)
    8       {
    9           cout << "X ";
   10           cout << "O ";
   11
   12       }
   13       else
   14       {
   15           cout << "O ";
   16           cout << "X ";
   17
   18       }
   19   }
   20   alternate = !alternate;
   21
   22   cout << endl;
   23 }
   24 }

main是一个函数,因此您应该将其声明为:

int main() {...

#include" stdafx.h"是Microsoft特定的,并且不会在其他平台上编译。此代码不需要STDAFX.H。

好吧,您只是错过了上述代码中的一些语法。正确的代码是:

    #include <iostream>
    using namespace std;
    int main ()
    {
       bool alternate = true ;
       for (int x = 0; x < 8; x++)
       {
         for (int y = 0; y < 4; y++)
         {
            if (alternate)
            {
               cout << "X ";
               cout << "O ";
            }
            else
            {
              cout << "O ";
              cout << "X ";
            }
         }
        alternate = !alternate;
        cout << endl;
      }
      return 0;
   }

代码中的错误是您错过了主要方法之后的"(("。另外,您忘了编写返回语句。