由于"error C4430: missing type specifier - int assumed. Note: C++ does not support default-int",我现在无法编

I cannot write c++ arrays now because of "error C4430: missing type specifier - int assumed. Note: C++ does not support default-int"

本文关键字:default-int support not does Note type missing C4430 error specifier 由于      更新时间:2023-10-16

所有想把我从痛苦中拯救出来的英俊的人问好,

我是 c++ 的新手,在完成了 w3schools 告诉您的字符串、循环、数组、指针等通常的介绍性内容后,我决定编写一些代码。我决定从一个简单的挑战开始,要求你编写一些循环,从最高到最低或相反地对一些整数进行排序,而不使用排序函数(有人告诉我有这样的东西(。

并且代码仍然不起作用。但这对我来说很正常,只要我能理解为什么它不起作用。(请不要回答为什么不起作用,请继续阅读(

#include <iostream>

using namespace std;
int age[4] = { 26, 14, 6, 43};

int num = 4;
int a = 0;
double previous = 0;

int chosen = age[a];
void first() {

while (result[0] || result[num] == 0){
for (int i = 0; a != num; i++) {
chosen = age[a];
previous = age[i];
if (chosen < (previous + 0.01) ) {
chosen = result[a];
a++;
previous = age[0];
i = 0;
}
}
}
}
void last()
{

for (int i = 0; i < num; i++) {
cout << result[i] << "n";
}
}

int main() {
first();
last();
}

所以我试图看看我的主要"if 语句"是否正常工作,并在引入变量的地方添加了已经分配的 result[0] 值。

int a = 0;
double previous = 0;
result[0] = 20;
int chosen = age[a];

然后当我运行程序时,发生了此错误:

error C2466: cannot allocate an array of constant size 0                  
(18,14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
(18,16): error C2440: 'initializing': cannot convert from 'int' to 'int [0]'
(18,12): message : There are no conversions to array types, although there are conversions to references or pointers to arrays

(第 18 行是我分配结果[0]的地方(

当我看到这个时,我就像"我想我很愚蠢,我不知道如何分配数组",但后来我检查了是否是这种情况,我认为事实并非如此。

然后我理解了第一个错误,因为我尝试将结果 [0] 更改为结果 [4],它消失了,但我不知道第二个错误在说什么。

所以,正如一位智者可能说的那样,"如果你有谷歌无法回答的编码问题,那就去堆栈溢出吧"。

所以我做到了。但我找到的文章是代码,与我的中文知识相当。如果你还在想,我一个中文单词都不认识。而且我没有找到更好的文章来解释如何在初学者的纯编码中解决问题。我刚刚开始在大流行中编写 c++,除了英语之外,我还什么都不懂。

如果有人能告诉我这个错误是什么意思,以及他下面的其他错误朋友怎么可能消失,我将不胜感激。

你还没有声明result. C 认为使用未声明的变量会导致编译器假定该变量被声明为extern int。 一些C++编译器支持此功能以实现向后兼容性,但它不是标准C++的一部分。

在这种情况下,编译器似乎支持这一点,因此它假定extern int result;因为它没有看到此名称的声明。

这会导致两种诊断:

  • 编译器警告您有关"默认int"声明,并且已弃用:">缺少类型说明符 - 假定为 int">
  • 然后编译器告诉你,你不能下标一个int(如果result是一个intresult[0]没有意义(:"不能从'int'转换为'int [0]'">

result添加数组声明以解决此问题。