69

Why do I get the "multiple types in one declaration" error when I compile my C++ program?

7
  • 7
    Please show the code in question
    – Nick Meyer
    Nov 18, 2009 at 2:15
  • 19
    Because you have multiple types in one declaration. If you want good help, we need code. Nov 18, 2009 at 2:24
  • 1
    Show not only the code, but tell which compiler. Nov 18, 2009 at 9:17
  • 19
    For anyone reading this, I encountered this error when missing the semicolon ; after an enum class declaration. Feb 14, 2016 at 9:28
  • In my case I was compiling a program with g++ which was supposed to be compiled by gcc.(becase I have to use C++ class in a large C program, I'm working on it..) and there was a variable name 'class' which is a keyword in C++. I had another similar instance with variable 'new'. You know 'int class;' looks like two consecuitve types with no variable name!
    – Chan Kim
    Jul 20, 2017 at 11:47

9 Answers 9

142

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;
2
  • 3
    For me, I forgot to add a semicolon after declaring a struct.
    – saccharine
    Apr 29, 2013 at 21:05
  • I explain the missing semicolon case more in my answer here. It just happened to me minutes ago. Oct 29, 2021 at 0:26
54

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

5
  • 2
    This got me while porting code from C# to C++. In C# the semicolon is optional.
    – Rev
    Feb 8, 2019 at 11:07
  • 1
    This is the exact problem that made me search for this question.
    – sean
    Mar 18, 2020 at 16:54
  • @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.
    – eguaio
    Mar 18, 2020 at 19:02
  • I go into more detail on this one in my answer here. Oct 29, 2021 at 0:23
  • 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
31

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.

2
  • 1
    this 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
14

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!

1
  • Good guess. miss a ; after a } of a class definition.
    – lichenbo
    Jan 4, 2013 at 10:34
9

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

8

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

class Foo // <-- forgot semicolon

class Bar {
  ...
};
4

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
    };
};
3

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.

2

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.

1
  • 2
    On my screen right now, "above" your answer is the question. Suggest you edit to be more clear about which of the 7 other answers this is an addition to!
    – Mogsdad
    May 4, 2016 at 20:25

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.