开发者

C++ "multiple types in one declaration" error

Why do I get the "multiple types in one declaration" error when I compile my C++ p开发者_高级运维rogram?


You probably have code that's the equivalent of

int float x;

probably

class Foo { } float x;

or in it's more common form (note the missing semicolon after closing curly bracket)

class Foo {
  //
}

float x;


Don't forget to check for ; after enum declarations, too.


I had the same problem. Sometimes the error line does not show the correct place. Go through all new-created/modified classes and see if you forget ";" in the end of class defifnition.


You must have declared twice the same variable in some class or two classes with the same name. See this on Stack Overflow, for example.

You could be also missing a ; or you could have a class definition with broken syntax ...

If you can show us some code, that would be better!


My guess is you're missing a closing brace somewhere in a class definition, or a semicolon after it.


Also, you may have forgotten a semicolon in a forward declaration:

class Foo // <-- forgot semicolon

class Bar {
  ...
};


Here is a yet another scenario that can pop up the same error

struct Field
{   // <------ Forget this curly brace
    enum FieldEnum
    {
        FIRSTNAME,
        MIDDLENAME,
        LASTNAME,
        UNKNOWN
    };
};


C or C++ error: "multiple types in one declaration": Further explanation for the case where you simply forgot the semicolon (;) at the end of a class, enum, or struct definition

Imagine you have the following code:

enum class ErrorType {
    MY_ERROR_1 = 0,
    MY_ERROR_2,
    MY_ERROR_3,
    /// Not a valid value; this is the number of enums
    _COUNT,
}  // <====== MISSING SEMICOLON (;)!

class MyClass {
public:
    // some stuff
private:
    // some stuff
};

Since I forgot the semicolon (;) at the end of the enum class definition, after the curly brace, it looks like I am defining the entire class MyClass inside of the enum class ErrorType, so I get the error!:

../my_header.h:43:1: error: multiple types in one declaration
   43 | };
      | ^

...where line 43 in my case is at the end of the class MyClass definition.

SOLUTION: add the missing semicolon (;) at the end of the enum definition, as stated by @eguaio here and @MSalters here.


Agree with the above. Also, if you see this, preprocess the app and look at the .i Search for the "offending" name. Then look back up. You'll often see the "}" w/o ";" on a class in the first non-with space above. Finding the problem is often harder than knowing what it is.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜