【问题标题】:'initializing' error in VS2010 while initializing an array初始化数组时VS2010中的“初始化”错误
【发布时间】:2014-03-01 08:04:56
【问题描述】:

我需要分配一个包含 6 个数组的数组,它来自 set[maxSetLength] 类型

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#define maxSetLength 129

typedef short int set[maxSetLength]; 

int _tmain(int argc, _TCHAR* argv[]){
int i;
set a={0},b={0},c={0},d={0},e={0},f={0}; // Assigning 6 Sets (Arrays) initialized by zeros
set sets[6]={a,b,c,d,e,f}; //Inserting All Sets into one Array (Array Of Arrays)
}

在 CodeBlocks 中它编译没有错误,在 VS2010 中它没有,这些是错误:

6次

error C2440: 'initializing' : cannot convert from 'set' to 'short'

6 次

IntelliSense: a value of type "short *" cannot be used to initialize an entity of type "short"

总共有 12 个错误

【问题讨论】:

  • test.c:1:1: error: stdafx.h: No such file or directory。如果您要提出与 Windows 无关的问题,请在发布之前从您的代码中删除所有 Windows 主义。
  • @Zack 你是什么意思 Windowssisms ?老师是这样教我的
  • 那你的老师不称职。
  • @Zack:他的老师不称职,因为他们在使用VS?克服自己。 VS 默认为许多项目类型添加了预编译的头文件,yeesh。不要再添加无用的 cmets 来吓唬初学者了。
  • 无论如何,实际的严肃建议:下载 MSVC 2012 并尝试一下。我怀疑您正在尝试使用 CodeBlocks 实现而 MSVC 2010(较旧)没有实现的 C++11 功能。

标签: c arrays initialization


【解决方案1】:

您需要使用指针(它们在 C 中很棘手)。试试下面的代码(我已经添加了一些调试,所以把它改回 0):

#include <stdio.h>
#include <string.h>

#define maxSetLength 129

typedef short int set[maxSetLength]; 

main()
{

int i;
set a={55},b={0},c={0},d={0},e={0},f={66}; // Assigning 6 Sets (Arrays) initialized by zeros
set sets[6]={*a,*b,*c,*d,*e,*f};

printf("%d\n", sets[0][0]);   // should be 55
printf("%d\n", sets[0][5]);   // should be 66

}

【讨论】:

  • 现在按照你的方式我得到 6 个错误而不是 12 个,错误 6 次:error C2440: 'initializing' : cannot convert from 'set' to 'short'
  • 不确定谁投了反对票 - 但 C 代码是对他的代码的破解,以消除编译错误。在 3 种不同的 gcc 编译器上测试可以工作。
【解决方案2】:
set a={0},b={0},c={0},d={0},e={0},f={0}; // Assigning 6 Sets (Arrays) initialized by zeros
set *sets[6]={&a, &b, &c, &d, &e, &f}; //Inserting All Sets into one Array (Array Of Arrays)

【讨论】:

  • 它可以工作,但我必须为此更改所有项目参数,所以我仍然希望它像在 CodeBlocks 上工作一样
  • @GabourX 我认为它应该运行一次适当的功能。
猜你喜欢
  • 1970-01-01
  • 2020-10-24
  • 2014-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-17
  • 1970-01-01
相关资源
最近更新 更多