Error: type name is not allowed in C++

12,888

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.

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 } },
};

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

Share:
12,888
Skaty
Author by

Skaty

I don't really like about me pages.

Updated on June 17, 2022

Comments

  • Skaty
    Skaty almost 2 years

    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}
    };
    

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

    EDIT: Definition of brushes:

    typedef union {
        struct {
            double c;
            double m;
            double y;
        } t;
    double v[3];
    } brushes;
    
  • Skaty
    Skaty over 12 years
    Well, that worked. However for some types, it returned an error "Expected an expression". Example: return (brushes){5.6, 214.0 , 13.0};
  • Jonathan Leffler
    Jonathan Leffler over 12 years
    @Skaty: as before - you can't use C99 constructs with MSVC; it does not support C99.