c++要求所有声明都有类型说明符

C++ requires a type specifier for all declarations

本文关键字:类型 说明符 声明 c++      更新时间:2023-10-16

我在我的"const MAX = 10;"行上得到错误" c++需要所有声明的类型说明符"。

下面是我的代码:
//A program that adds up the maximum of 10 numbers ( 1,2,3,4,5,6,7,8,9,10 )

#include <iostream>
#include <cmath>
using namespace std;
const MAX = 10; //the error is here!
int main()
{
int sum, num;
sum = 0;
num = 1;
do
{
    sum = sum + num;
    num++;
}
while (num <= MAX);
{
    cout << "Sum = ";
}
return 0;
}

如错误提示所示,c++需要声明类型说明符。例如,将const MAX = 10;更改为const int MAX = 10;

是否考虑过使用

#define MAX 10

?