I have two classes and want to have a reference from class Kunde to class Konto and backward, but my compiler shows many errors. I don’t know what the problem is. Please help me.
Class Konto:
#pragma once
#include "Kunde.h"
class Konto {
private:
Kunde* kunde;
protected:
int kontonummer;
double stand;
public:
int getKontonummer();
Kunde* getKunde();
double getKontostand();
bool einzahlen(double betrag);
virtual bool auszahlen(double betrag);
};
Class Kunde:
#pragma once
#include "Konto.h"
#include <string>
class Kunde {
private:
string vorname;
string nachname;
Konto* konto;
public:
Kunde(string vorname, string nachname);
void setKonto(Konto* konto);
Konto* getKonto();
};
I get following compiler errrors:
konto.h(6): error C2143: syntax error: missing ‘;’ before ‘*’
konto.h(6): error C4430: missing typespecifier – int assumed. Note: C++ does not support “default-int”
konto.h(6): error C4430: missing typespecifier – int assumed. Note: C++ does not support “default-int”
and some more.
You have a circular inclusion problem. You see the
#pragma oncestatement in the first line of the header file? This prevents an inclusion of the header if it has already been included. Since your header files include each other, at the declaration of eitherKundeorKontothe other one has not yet been defined.You can circumvent the problem if you make a simple forward declaration of either class in the other header file. Specifically:
(Konto.h)
The only thing is that you now should include
Kunde.hin theKonto.cpp, or else this would lead to a linker error.EDIT: see comments 🙂 thanks