Error: Expected Class-Name Before '{' Token

error: expected class-name before ‘{’ token

Replace

#include "Landing.h"

with

class Landing;

If you still get errors, also post Item.h, Flight.h and common.h

EDIT: In response to comment.

You will need to e.g. #include "Landing.h" from Event.cpp in order to actually use the class. You just cannot include it from Event.h

C++ error expected class-name before '{' token

If you follow the error message, it says:
OptimizedSurface.h|11|error: expected class-name before ‘{’ token

Take a look at OptimizedSurface.h, line 11. You have:
class OptimizedSurface : public Game{

There's nothing wrong with that, but the error is saying that the identifier right before the '{' is not recognized. That identifier is Game. So for some reason, Game is not declared before you get to line 11.

Looking at the headers included by OptimizedSurface.h, you probably expect it to be declared in "Game.h". So now look at Game.h. It does indeed have a declaration for the Game class. But before that, there is an #include "MenuState.h". You should guess that this is interfering.

In MenuState.h, you have #include "OptimizedSurface.h"...

Does it make sense yet? It's a circular #include pattern. Your OptimizedSurface needs to know about Game, but Game.h is trying to declare OptimizedSurface (via MenuState.h) first! OptimizedSurface.h depends on Game.h, Game.h depends on MenuState.h, and MenuState.h depends on OptimizedSurface.h.

The best solution is to remove the #include "OptimizedSurface.h" from MenuState.h. MenuState does not actually depend on OptimizedSurface, so there's no need for it. It just contributes to a mess of header dependencies.

If you do have an implementation dependency, then you should #include the appropriate headers in the cpp files. You mentioned that MenuState.cpp depends on OptimizedSurface, so add #include "OptimizedSurface.h" to MenuState.cpp instead of in MenuState.h.

Besides fixing it by removing unnecessary dependencies, you can in some cases solve a circular dependency pattern by adding a "forward declaration" of dependent classes (see a good C++ reference for more). This is important for complicated dependencies, but should not be the solution you use this time.

expected class-name before '{' token, class inheritance

Just to mark this as answered. Paraphrasing the discussion: Check your include chain to see if you are including EntityPlayer.h from Entity.h.

expected class-name before '{' token - with header files and cpp files

The error says that the name of a class was expected between : public and { here:

class Dollar : public Currency {
^^^^^^^^

Currency is not a name of a class, because you haven't defined such class. Yes, you have defined such class in files Currency.cpp and Currency.h, but not in the file Dollar.h where that error occurs.

Solution: The class Currency has to be defined first before it can be used as a base class. Like so:

// class is defined first
class Currency {
public:
virtual void printStatement();
};

// now Currency is a class and it can be used as a base
class Dollar : public Currency {
public:
void printStatement();
};

Since a class must be defined in all source files where it is used, and the definition must be identical across all source files, it is often useful to define the class in a separate "header" file, such as you have done. In such case you can simply include that header in stead of writing the definition repeatedly in each source file:

#include "Currency.h"

Currency.cpp contains two definitions for the class Currency. Once in the header that is included, and then second time after that. You may not have multiple definitions for the same class in a single source file.

Solution: Remove the class definition from Currency.cpp. Instead only define the member function:

void Currency::printStatement() {
//...
}

Finally, you haven't defined Dollar::printStatement. You've defined printStatement, which is not the same thing.

C++ expected class-name before { token, an inheritance error

KSetBase is a template class, if you inherit from it you must specify a template argument:

template<typename T>
class KSet : public KSetBase<T>

In addition, it seems you have a typo in the base class destructor.

src/walletdb.h:96:1: error: expected class-name before ‘{’ token

Seems like base class CDB is missing (header not available or not included). Check if it's an external package, and install the development files if necessary. If not there may be a #define somewhere that makes the class unavailable on that specific system, e. g. because prerequisites are missing.

error: expected class-name before '{' token { ^

My guess is you have a circular include issue, meaning controller.h includes, either directly or indirectly, form1.h.

EDIT: Change the include I was talking about to a forward declaration - you don't need the full definition of form1:

#ifndef CONTROLLER_H
#define CONTROLLER_H

#include <QWidget>
class form1;
class form2;
class form3;

namespace Ui {
class controller;
}

class controller : public QWidget
{
//.................
//.................


Related Topics



Leave a reply



Submit