使用索引指示符初始化_C ++ _对象数组

时间:2012-04-03 03:26:19

标签: c++

我正在尝试将一些代码移植到GCC,用IAR编译器编译好。代码初始化一个C ++对象数组(一个带有字符数组的结构)。我可以在C中使用GCC,但不能使用C ++。这是一个简单的例子。

#include  <stdio.h>

typedef struct
{
 int lineID[10];
} TMenu;

static const TMenu t1[8] =
{
    {{3}},
    {{4}},
    [6] = {{33, 22}},
    [8] = {{33, 22}},
    {{}},
    {{9,8,7,6,5,4,3,2,1}},
};

注意:我还必须在初始化程序周围添加额外的大括号,IAR没有抱怨。

它与GCC编译很好,但是当用G ++编译时,我得到以下错误。

x.c:12:6: error: expected identifier before numeric constant
x.c: In lambda function:
x.c:12:9: error: expected '{' before '=' token
x.c: At global scope:
x.c:12:20: error: no match for 'operator=' in '._2 = {{33, 22}}'
x.c:13:6: error: expected identifier before numeric constant
x.c: In lambda function:
x.c:13:9: error: expected '{' before '=' token
x.c: At global scope:
x.c:13:20: error: no match for 'operator=' in '._3 = {{33, 22}}'

2 个答案:

答案 0 :(得分:2)

看起来GCC 4.7越来越接近支持这种结构。以下是编译相同示例时GCC 4.5,4.6和4.7的输出。

GCC 4.5.3

$ /opt/local/bin/g++-mp-4.5 -Wall -std=c++0x -o y.exe x.c
x.c:12:6: error: expected identifier before numeric constant
x.c: In lambda function:
x.c:12:9: error: expected '{' before '=' token
x.c: At global scope:
x.c:12:20: error: no match for 'operator=' in '._2 = {{33, 22}}'
x.c:13:6: error: expected identifier before numeric constant
x.c: In lambda function:
x.c:13:9: error: expected '{' before '=' token
x.c: At global scope:
x.c:13:20: error: no match for 'operator=' in '._3 = {{33, 22}}'

GCC 4.6.3

$ /opt/local/bin/g++-mp-4.6 -Wall -std=c++0x -o y.exe x.c
x.c:12:6: error: expected identifier before numeric constant
x.c: In lambda function:
x.c:12:9: error: expected '{' before '=' token
x.c: At global scope:
x.c:12:20: error: no match for 'operator=' in '{} = {{33, 22}}'
x.c:12:20: note: candidate is:
x.c:12:7: note: <lambda()>&<lambda()>::operator=(const<lambda()>&) <deleted>
x.c:12:7: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const<lambda()>&'
x.c:13:6: error: expected identifier before numeric constant
x.c: In lambda function:
x.c:13:9: error: expected '{' before '=' token
x.c: At global scope:
x.c:13:20: error: no match for 'operator=' in '{} = {{33, 22}}'
x.c:13:20: note: candidate is:
x.c:13:7: note: <lambda()>&<lambda()>::operator=(const<lambda()>&) <deleted>
x.c:13:7: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const<lambda()>&'

GCC 4.7.0

$ /opt/local/bin/g++-mp-4.7 -Wall -std=c++0x -o y.exe x.c
x.c:16:1: sorry, unimplemented: non-trivial designated initializers not supported
x.c:16:1: sorry, unimplemented: non-trivial designated initializers not supported

答案 1 :(得分:0)

我从未见过类似

的语法
[6] = {33, 22}},

在C ++中。您可以尝试使用空的初始化程序来填补空白。以下适用于ideone

#include <iostream>

struct TMenu
{
  int lineID[10];
};

int main(int, char*[])
{
  const TMenu t1[8] =
  {
    {{3}},
    {{4}},
    {{33, 22}},
    {{33, 22}},
    {{}},
    {{9, 8, 7, 6, 5, 4, 3, 2, 1}},
  };
  for (int i = 0; i < 8; ++i)
  {
    for (int j = 0; j < 10; ++j)
    {
      std::cout << t1[i].lineID[j] << "\t";
    }
    std::cout << std::endl;
  }
}