error: using typedef-name after class

2019-05-14 07:01发布

问题:

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 :)

回答1:

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.



回答2:

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.



回答3:

for arduino hectop 3d printer:

Rename fpos_t variables in SdBaseFile.h and SdBaseFile.cpp to another name like fpos_t1 and recompile.