tags:

views:

89

answers:

2

I can't figure out what the actual problem is with this.

typedef struct _actor
{
   ...
} _actor, Actor;

class Actor
{
   ...
};

I get this weird error message actor.cpp:31: error: using typedef-name ‘Actor’ after ‘class’.

Any idea what I did wrong here? Thank you :)

+3  A: 

You are not allowed to define the symbol Actor more than once. The typedef statement already defines the symbol Actor as an alias for struct _actor before you attempt to declare a class with the same name.

I'm guessing you're using the gcc compiler. I get the following errors when I compile with gcc:

../src/main.cpp:113: error: using typedef-name ‘Actor’ after ‘class’
../src/main.cpp:111: error: ‘Actor’ has a previous declaration here

The first message (pointing the class Actor line in the program) states that you cannot declare a class with a typedef-name (Actor is already declared using typedef). The second message (pointing to the typedef struct _actor line in the program) is a little clearer and refers to the multiple declarations for Actor.

It is very common in C/C++ for a single class of error like this to result in multiple compiler errors and often the more helpful message is not the first reported.

Richard Cook
could you elaborate a little? I'm still sort of new to this.
Bocochoco
@Boco: Simply put, you are trying to create two different types with the same name.
UncleBens
Thank you :). Removing the , Actor at the end seems to have resolved that issue.
Bocochoco
Still, I don't understand why you are using that typedef.
Matteo Italia
+2  A: 

To understand what going on, we need to break the first statement up into it's parts:

struct _actor 
{ 
   ... 
};

typedef struct _actor _actor;
typedef struct _actor Actor; 

First we create a structure called _actor. Next, we create a typedef for struct _actor called _actor. This is only useful in C. It allows us to say:

  _actor myActor;

instead of

 struct _actor myActor;

But in C++, it's unnecessary, as C++ allows us to use the first form natively, without the typedef.

The third line creates a second typedef for struct _actor called Actor. When you then try to create a class named Actor the compiler complains, as that name is already being used for the alias for the struct.

Now, it seems likely that in the original C code, the author had intended struct _actor to be merely an implementation detail, and that you would always use just Actor to refer to instances of this struct. Therefore, in your C++ code, you should probably eliminate the typedefs entirely, and just rename the struct. However, that will give you:

 struct Actor {.....}
 class  Actor {.....}

So, prehaps you should look into merging those two classes.

James Curran