错误:C++ 中不允许使用类型名称 [英] Error: type name is not allowed in C++

查看:44
本文介绍了错误:C++ 中不允许使用类型名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我编译我的代码时,VC++ 返回一个错误,如上所述.受影响的行是 (brushes){5.6, 214.0 , 13.0}

When I compiled my code, VC++ returns an error, as stated above. The affected line is (brushes){5.6, 214.0 , 13.0}

更具体地说,这里是受影响的代码块

More specifically, here is the affected code block

const  brushes palette[] = {
    (brushes){5.6, 214.0 , 13.0},
    (brushes){200.0, 211.0, 12.0}
};

这段代码在 Linux 中编译得很好,那么为什么 VC++ 会发生这种情况?

This code compiles fine in Linux, so why is this happening for VC++?

画笔的定义:

typedef union {
    struct {
        double c;
        double m;
        double y;
    } t;
double v[3];
} brushes;

推荐答案

您使用的 C99 结构(第 6.5.2.5 节复合文字)不受 MS VC 支持,但 GCC 支持.

You are using a C99 construct (§6.5.2.5 Compound Literals) which is not supported by MS VC, but which is supported by GCC.

您应该能够通过删除 (brushes) 符号来编译代码:

You should be able to get the code to compile on both by dropping the (brushes) notation:

const  brushes palette[] = {
    { {   5.6, 214.0, 13.0 } },
    { { 200.0, 211.0, 12.0 } },
};

这将初始化 union 的第一个成员,即 brushes.这适用于 GCC;我相信它也应该与 MSVC 一起使用.

This will initialize the first member of the union that is brushes. This works with GCC; it should work with MSVC too, I believe.

这篇关于错误:C++ 中不允许使用类型名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆