Why do I get the "multiple types in one declaration"
error when I compile my C++ program?
9 Answers
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;
-
3
-
I explain the missing semicolon case more in my answer here. It just happened to me minutes ago. Oct 29, 2021 at 0:26
Don't forget to check for ;
after enum declarations, too.
-
2This got me while porting code from C# to C++. In C# the semicolon is optional.– RevFeb 8, 2019 at 11:07
-
1
-
@qsp And in this case the error message is not obvious, that's why I put the answer ;) Happy to hear it is still useful.– eguaioMar 18, 2020 at 19:02
-
-
Thank you. Dear god, way to make the message as unhelpful as possible. Feels like compiler writers are no longer human and lack the ability to speak human languages.– SF.Feb 1, 2022 at 15:07
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.
-
1this should be the suggested answer cos I got the same problem only to see that a list of new headers I had included had a missing ";" Oct 10, 2012 at 8:59
-
others have also mentioned ';' issue especially @Msalters with example also. why is none marked as answer?– gp.Dec 24, 2012 at 15:24
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.
-
2
;
after anenum class
declaration.