Multiple Definition Of In C

closed account (zb0S216C)
This problem is really doing my box in. First, here's my code:
1
2
3
4
5
6
7
8
9
struct Vertex
{
    struct Location
    {
        float *XRes, *YRes, *ZRes;
    } Location;

    bool UsedRes; // Error: expected specified-qualifier-list before 'bool'
};


No matter what I change in the code, it always gives me this error( comment ). I'm struggling with this one.

Any help is appreciated.
Try this:
1
2
3
4
5
6
7
8
9
struct Vertex
{
    struct Location
    {
        float *XRes, *YRes, *ZRes;
    };

    bool UsedRes; // Error: expected specified-qualifier-list before 'bool'
};

EDIT: just noticed you said it was C (C99, I presume). Assuming you don't want to say "struct Location" every time you refer to it, you can instead say:

1
2
3
4
5
6
7
8
9
struct Vertex
{
    typedef struct
    {
        float *XRes, *YRes, *ZRes;
    } Location;

    bool UsedRes; // Error: expected specified-qualifier-list before 'bool'
};

Last edited on
closed account (zb0S216C)
That didn't work. :(
Did you #include <stdbool.h> ?
closed account (zb0S216C)
I never knew that I had to include a header to use bool. Thanks, filipe.
C doesn't have an intrinsic bool type, C++ does.
closed account (zb0S216C)
1
2
3
4
5
6
7
8
9
struct Vertex
{
    typedef struct
    {
        float *XRes, *YRes, *ZRes;
    } Location;

    bool UsedRes; // Error: expected specified-qualifier-list before 'bool'
};


I tried that and it gave me: expected specifier-qualifier-list before 'typedef'. I have two headers in total and in total, I have 2 vertex structures.

Here's the first vertex( VertexOne.h ):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef VERTEX_ONE_H
#define VERTEX_ONE_H

struct VertexOne
{
     struct Location
     {
          float *XRes, *YRes, *ZRes;
     } Location;

     struct Colour
     {
          float *RRes, *GRes, *BRes;
     } Colour;
};

#endif 


Here's the second vertex( VertexTwo.h ):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef VERTEX_TWO_H
#define VERTEX_TWO_H

struct VertexTwo
{
    struct Location
    {
         float *XRes, *YRes, *ZRes;
    } Location;

    struct Colour
    {
         float *RRes, *GRes, *BRes;
    } Colour;

    struct Normal
    {
         float *XRes, *YRes, *ZRes;
    } Normal; 
};

#endif 


I don't know how the compiler can see a multiple definition of the two structures. Both Location and Colour structures are members of different structures.
Here's how to declare a struct in C++: http://www.cplusplus.com/doc/tutorial/structures
closed account (zb0S216C)
Here's how to declare a struct in C++

My code is pure C.
closed account (zb0S216C)
OK, I figured it out. It turns out that the GNU GCC compiler doesn't like the nested structures of Location and Colour in VertexOne and VertexTwo to be named the same. So I simply swapped the names with new ones and now GCC is happy.

Thanks to those who contributed :)
It's C99 (most "pure C" out there is C89, so it's nice to qualify when talking about C99).

Anyway, your problem is that those nested structs are not confined in the outer struct's "namespace", because there are no namespaces in C. So you're redefining. If I may, it doesn't even make sense to define them inside a struct because they're not specific to that struct (at least Location and Colour).

On a side note, this:

1
2
3
struct Colour {
     float *RRes, *GRes, *BRes;
} Colour;

Defines a struct named Colour (which must be referred to as "struct Colour") and then declares an object named Colour, of type struct Colour. Now, this:

1
2
3
typedef struct {
    float *RRes, *GRes, *BRes;
} Colour;

Simply defines a nameless struct and provides it with the alias Colour.
Last edited on
Topic archived. No new replies allowed.