C++ Class inheritance "expected class-name" error - c++

My previous question was asked wrong, so I'll post it fixed.
I have this example throwing
expected class-name before ‘{’ token
error while compiling. I am understanding why is it fails, but I don't know how to fix it. Thank you.
BaseClass.h
#ifndef INHERITTEST_BASECLASS_H
#define INHERITTEST_BASECLASS_H
#include "ElementClass.h"
class ElementClass;
class BaseClass
{
private:
ElementClass *m_someField;
};
#endif
ElementClass.h
#ifndef INHERITTEST_ELEMENTCLASS_H
#define INHERITTEST_ELEMENTCLASS_H
#include "ChildClass.h"
class ChildClass;
class ElementClass
{
private:
ChildClass *m_class;
};
#endif
ChildClass.h
#ifndef INHERITTEST_CHILDCLASS_H
#define INHERITTEST_CHILDCLASS_H
#include "BaseClass.h"
class ChildClass : public BaseClass
{
};
#endif

You have circulary dependent .h files.
In BaseClass.h:
#ifndef INHERITTEST_BASECLASS_H
#define INHERITTEST_BASECLASS_H
#include "ElementClass.h" // Includes ElementClass.h
In ElementClass.h:
#ifndef INHERITTEST_ELEMENTCLASS_H
#define INHERITTEST_ELEMENTCLASS_H
#include "ChildClass.h" // Which included BaseClass.h
You can remove those #include lines since you are using those classes by pointers only and a forward declaration is sufficient for that purpose.

When you're working with inheritance the following
#include "ChildClass.h"
class ChildClass;
is unnecessary, if you're going to break these into sepperate source files (which it looks like you are) you can say
#include "ElementClass.h"
in the source file of your derived class

Related

Error "Unterminated conditional directive" in cross-referencing headers

There are two classes that are related to each other in their headers:
PlotMarker
#ifndef PLOTMARKER_H
#define PLOTMARKER_H
#include <QObject>
#include "plotter.h"
class Plotter;
class PlotMarker : public QObject
{
// ...
Plotter* m_attachedPlot;
// ...
};
#endif // PLOTMARKER_H
Plotter
#ifndef PLOTTER_H
#define PLOTTER_H
// ...
#include "plotmarker.h"
// ...
class PlotMarker;
class Plotter : public QQuickPaintedItem
{
// ...
QLinkedList<PlotMarker*> m_markerList;
// ...
};
#endif // PLOTTER_H
The program is compiled well, but it's got a error error: unterminated conditional directive in #ifndef and the code of classes in the IDE isn't highlighted because of it.
If I remove #include "plotter.h" in PlotMarker's header or #include "plotmarker.h" in Plotter's header, Qt Creator highlights the code as usual, but the compilating fails because of errors about invalid use of incomplete type.
Could you please tell me what's wrong? I think it's because of wrong headers cross-referencing, but I ran into this and it didn't help me.
The problem is solved.
I just moved one of #include from the header to the source file, and it has worked.
plotmarker.h
#ifndef PLOTMARKER_H
#define PLOTMARKER_H
#include <QObject>
class Plotter;
class PlotMarker : public QObject
{
// ...
Plotter* m_attachedPlot;
// ...
};
#endif // PLOTMARKER_H
// ...
plotmarker.cpp
#include "plotmarker.h"
#include "plotter.h"
// ...
There is a fundamental design flaw.
For example
#include "b.h"
class A
{
B b; // B is an object, can't be forward declared
};
a.h header file above
#include "a.h"
class B {
A* a // A is an object, can't be forward declared
};
b.h header file above
This is a Circular Dependencies
The compiler will do the following:
#include "a.h"
// start compiling a.h
#include "b.h"
// start compiling b.h
#include "a.h"
// compilation of a.h skipped because it's guarded
// resume compiling b.h
class B { A* a }; // <--- ERROR, A is undeclared
I've just gotten "unterminated conditional directive" in #ifndef and "invalid preprocessing directive" in #end.
I've just added "if" after #end(edit "#end" to "#endif"), that error is fixed.

Including header file in header file

I have this code in C++:
#ifndef MYCLASS_H
#define MYCLASS_H
#include "gspace.h"
class myclass {
public:
void update(gspace **);
}
gspace is another class defined in gspace.h. The compiler however is telling me:
include/myclass.h error: ‘gspace’ has not been declared|
Is there anything wrong in what I'm doing?
EDIT:
#ifndef GSPACE_H
#define GSPACE_H
#include <vector>
#include <iostream>
#include <math.h>
using namespace std;
class gspace
{
public:
gspace();
Assuming you have even declared a class named "gspace" in gspace.h I could think of two possible errors.
1) class gspace would not be in global scope i.e could be in some namespace.
2) Please check if header gaurds used in gspace not "MYCLASS_H".

Inheritance with parametrized class

I have a parametrized class Queue and a subclass ClientsQueue not parametrized that inherits from Queue. I think I have a syntax error:
client.h
#ifndef CLIENT_H_
#define CLIENT_H_
class Client {
public:
Client();
~Client();
};
#endif
queue.h
#ifndef QUEUE_H_
#define QUEUE_H_
template <class T> class Queue {
public:
Queue();
~Queue();
};
#endif
clientsQueue.h
#ifndef CLIENTSQUEUE_H_
#define CLIENTSQUEUE_H_
#include "queue.h"
#include "client.h"
class ClientsQueue: public Queue<Client> {
public:
ClientsQueue();
~ClientsQueue();
};
#endif
clientsQueue.cpp
#include "clientsQueue.h"
ClientsQueue::ClientsQueue() {
};
bank.cpp
#include "clientsQueue.cpp"
int main() {
return 0;
}
So, when I try to compile and run the program, the compiler says:
clientsQueue.cpp:3:1: error: ‘ClientsQueue’ does not name a type
ClientsQueue::ClientsQueue() {
^
I can't see the error. If I quit all the code from clientsQueue.cpp, it works.
How can I fix it?
Thanks!
You should #include "clientsQueue.h" not #include "clientsQueue.cpp" in main file. When you include header you present a declarations to the compiler. You miss the declaration of the class ClientsQueue when you include just the source (cpp) file.

'C2504 base class undefined' error despite #include and forward declaration?

How can it be that the following code generates a C2504: 'GameObject': base class undefined?! error:
#ifndef INCLUDED_PLAYER
#define INCLUDED_PLAYER
#include "GameObject.h"
#include "Game.h"
#include "Bullet.h"
#include <SFML/Window/Keyboard.hpp>
class GameObject;
class Player:
public GameObject
{ <- Compiler Error
Edit: Problem found, for whatever reason I added an unnecessary include in GameObject.h. Removing that include fixed all my problems.

inheritance error: "expected class-name before '{' token"

I am writing a poker program, where two classes I have are a Deck class and a Hand class. The Hand class inherits from the Deck class, so it can use its printGroup method. However, when I compile I get an error that says:
expected class-name before '{' token
referring to the line:
class Hand : public Deck{
Here is the code for the two class headers. Could anyone help me solve this?
//Hand header
#ifndef HAND_H
#define HAND_H
#include <iostream>
#include <vector>
#include "Deck.h"
#include "Card.h"
class Card;
class Hand : public Deck{ //error occurs from this line
public:
Hand(){}
void createHand(std::vector<Card> &, std::vector<Card> &);
};
#endif /* HAND_H */
//Deck header
#ifndef DECK_H
#define DECK_H
#include <iostream>
#include <vector>
#include "Card.h"
class Card;
class Deck{
public:
Deck(){}
void createDeck(std::vector<Card> &);
void printGroup(std::vector<Card> &, int);
void sortDeck(std::vector<Card> &, int, int);
};
#endif /* DECK_H */
Assuming that #marcog's gut feeling that it is a circular dependency is correct (maybe Card.h includes Hand.h, thereby importing the file Hand.h before getting up to the declaration of the Deck class), this can be solved by forward declarations.
I see you already have a forward declaration of the Card class ("class Card;"). Therefore, do you really need to #include "Card.h"? If you remove that include, you can still refer to the class Card due to the forward declaration, but it may resolve the cyclic dependency.
I usually don't #include .h files from other .h files in C++ unless I really have to. If you are just referring to a class in another file (by pointer, reference, or putting it in a container like vector), then you can get away with just forward-declaring the class, and physically including the header file from the .cpp file only.