在C ++中嵌套结构JSON样式

吉姆22150

我正在尝试显式初始化嵌套,struct但是在我的代码中找不到任何错误。我正在将Visual Studio 2013与Visual C ++编译器一起使用。当尝试编译下面的代码时,出现以下错误消息:“编译器发生内部错误。”

    // Parameter determines which menu will be displayed (0, 1, or 2).
    int menu(int n) {

        struct Menus_obj {
            struct Menu_obj {
                string query;
                string choices[5];
            };
            Menu_obj Menu;
        };

        Menus_obj Menus[3] = {
            {
                "Fruit type (1-5): ",
                {
                    "1. Apple - 1.00\n",
                    "2. Orange - 2.00\n",
                    "3. Banana - 3.00\n",
                    "4. Blueberry - 5.00\n",
                    "5. Blackberry - 8.00\n"
                }
            },
            {
                "Vegetable type (1-5): ",
                {
                    "1. Broccoli - 2.00\n",
                    "2. Spinach - 4.00\n",
                    "3. Kale - 6.00\n",
                    "4. Cauliflower - 8.00\n",
                    "5. Moringa - 10.00\n"
                }
            },
            {
                "Number of shoppers: ",
                {
                    "",
                    "",
                    "",
                    "",
                    ""
                }
            }
        };

        cout << Menus[n].Menu.query << endl;
        for (int i = 0; i < 5; i++) {
            cout << Menus[n].Menu.choices[i];
        }

        cin >> n;
        return n - 1;

    }

我的代码中是否有错误,和/或是否为逻辑错误;我是在尝试做错什么吗?提前致谢。

安迪·布朗

找到内部编译器错误总是很有趣。首先,您应该将测试代码提交给MS,以便他们解决问题。

其次,由于问题在于string数组中数组中对象的初始化,struct我们只需要将它们放入匿名struct即可实现解决方法。

更改您的Menus_obj结构,使其看起来像这样,它将可以正常编译。

struct Menus_obj {
  struct Menu_obj {
    string query;             
    struct { 
      string choices[5]; 
    };
  };
  Menu_obj Menu;
};

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章