error : expected unqualified-id before return in c++

ID : 274466

viewed : 51

Tags : c++c++





Top 5 Answer for error : expected unqualified-id before return in c++

vote vote

91

Just for the sake of people who landed here for the same reason I did:

Don't use reserved keywords

I named a function in my class definition delete(), which is a reserved keyword and should not be used as a function name. Renaming it to deletion() (which also made sense semantically in my case) resolved the issue.

For a list of reserved keywords: http://en.cppreference.com/w/cpp/keyword

I quote: "Since they are used by the language, these keywords are not available for re-definition or overloading. "

vote vote

90

if (chapeau) { 

You forgot the ending brace to this if statement, so the subsequent else if is considered a syntax error. You need to add the brace when the if statement body is complete:

if (chapeau) {     cout << "le Professeur Violet"; } else if (moustaches) {     cout << "le Colonel Moutarde"; } // ... 
vote vote

71

Suggestions:

  • use consistent 3-4 space indenting and you will find these problems much easier
  • use a brace style that lines up {} vertically and you will see these problems quickly
  • always indent control blocks another level
  • use a syntax highlighting editor, it helps, you'll thank me later

for example,

type functionname( arguments ) {     if (something)     {         do stuff     }     else     {         do other stuff     }     switch (value)     {         case 'a':             astuff             break;         case 'b':             bstuff             //fallthrough //always comment fallthrough as intentional         case 'c':             break;         default: //always consider default, and handle it explicitly             break;     }     while ( the lights are on )     {         if ( something happened )         {             run around in circles             if ( you are scared ) //yeah, much more than 3-4 levels of indent are too many!             {                 scream and shout             }         }     }     return typevalue; //always return something, you'll thank me later } 
vote vote

66

You need to move " } " before the line of cout << endl; to the line before the first else .

vote vote

54

Top 3 video Explaining error : expected unqualified-id before return in c++







Related QUESTION?