Foreward declaration von Klassen
-
Hallo zusammen,
ich habe folgendes Problem:
Ich habe zwei Klassen, die jeweils die andere verwenden.#ifndef VPREPROCXS_H_ #define VPREPROCXS_H_ #include "VPreProc.h" #include "VFileLineXs.h" #include <deque> /* Perl */ # include <EXTERN.h> # include <perl.h> # include <XSUB.h> class VFileLineXs; class VPreProcXs : public VPreProc { public: SV* m_self; // Class called from (the hash, not SV pointing to the hash) deque<VFileLineXs*> m_filelineps; VPreProcXs() : VPreProc() {}; virtual ~VPreProcXs(); // Callback methods virtual void comment(string filename) {}; // Comment for keepComments=>sub virtual void include(string filename){}; // Request a include file be processed virtual void define(string name, string value, string params){}; // `define with parameters virtual void undef(string name){}; // Remove a definition virtual void undefineall(){}; // Remove all non-command-line definitions virtual bool defExists(string name); // Return true if define exists virtual string defParams(string name); // Return parameter list if define exists virtual string defValue(string name); // Return value of given define (should exist) virtual string defSubstitute(string substitute); // Return value to substitute for given post-parameter value // void call(string* rtnStrp, int params, const char* method, ...); void unreadback(char* text); }; #endif /* VPREPROCXS_H_ */und
#ifndef VFILELINEXS_H_ #define VFILELINEXS_H_ #include "VFileLine.h" #include "VPreProcXs.h" #include <deque> /* Perl */ # include <EXTERN.h> # include <perl.h> # include <XSUB.h> class VPreProcXs; class VFileLineXs : public VFileLine { VPreProcXs* m_vPreprocp; // Parser handling the errors public: VFileLineXs(VPreProcXs* pp) : VFileLine(true), m_vPreprocp(pp) { if (pp) pushFl(); } virtual ~VFileLineXs(); virtual VFileLine* create(const string& filename, int lineno) { VFileLineXs* filelp = new VFileLineXs(m_vPreprocp); filelp->init(filename, lineno); return filelp; } virtual void error(const string& msg); // Report a error at given location void setPreproc(VPreProcXs* pp) { m_vPreprocp=pp; pushFl(); // The very first construction used pp=NULL, as pp wasn't created yet so make it now } // Record the structure so we can delete it later void pushFl() { m_vPreprocp->m_filelineps.push_back(this); } }; #endif /* VFILELINEXS_H_ */Wenn ich nun mein Programm kompilliere erhalte ich folgenden Fehler:
In file included from Parser/VPreProcXs.h:11:0, from Parser/VPreProcXs.cpp:8: Parser/VFileLineXs.h: In member function ‘void VFileLineXs::pushFl()’: Parser/VFileLineXs.h:38:32: error: invalid use of incomplete type ‘struct VPreProcXs’ Parser/VFileLineXs.h:20:7: error: forward declaration of ‘struct VPreProcXs’Wie kann ich das umgehen?
Ich verstehe zwar den Fehler, aber nicht wie ich Ihn lösen kann. Meine einzige Idee, wäre beide Klassen in eine Datei zu schreiben und zwar statt der foreward declaration.
Aber das ist ja nicht die schönste Lösung.Beste Grüße,
JRS
-
Brauchst du
#include "VFileLineXs.h"bei der ersten Klasse? Die Vorwärtsdeklaration sollte doch ausreichen.
-
Die Fehlermeldung passt nicht zum gezeigten Code. VFileLineXs.h hat keine 38 Zeilen.
-
Hallo,
danke. Ich werde mal weiter sehen. der komplette Code ist:
/* * VFileLineXs.h * * Created on: 30.04.2012 * Author: jseyler */ #ifndef VFILELINEXS_H_ #define VFILELINEXS_H_ #include "VFileLine.h" #include "VPreProcXs.h" #include <deque> /* Perl */ # include <EXTERN.h> # include <perl.h> # include <XSUB.h> class VPreProcXs; class VFileLineXs : public VFileLine { VPreProcXs* m_vPreprocp; // Parser handling the errors public: VFileLineXs(VPreProcXs* pp) : VFileLine(true), m_vPreprocp(pp) { if (pp) pushFl(); } virtual ~VFileLineXs(); virtual VFileLine* create(const string& filename, int lineno) { VFileLineXs* filelp = new VFileLineXs(m_vPreprocp); filelp->init(filename, lineno); return filelp; } virtual void error(const string& msg); // Report a error at given location void setPreproc(VPreProcXs* pp) { m_vPreprocp=pp; pushFl(); // The very first construction used pp=NULL, as pp wasn't created yet so make it now } // Record the structure so we can delete it later void pushFl() { m_vPreprocp->m_filelineps.push_back(this); } }; #endif /* VFILELINEXS_H_ */Dann kommt man auch auf die lines. Auch beim anderen Code ist nochmal ne Präambel davor.
-
Jetzt passt es zusammen. Und Bashar hat ja die Lösung schon vorgegeben.
-
Mach die zyklische Abhängigkeit der Header weg und lagere wenigstens die Implementierung der Methoden (aka Elementfunktionsdefinitionen), die die vollständigen Klassendefinitionen benötigen, in entsprechende cpp-Dateien aus, in der du dann jeweils beide Header inkludierst.
Aber ... mal ehrlich gesagt ... so richtig dolle wirkt das Design jetzt nicht.
-
Hallo zusammen,
ich habe natürlich auch noch zwei entsprechende CPP-Klassen mit den Definitionen. Die wollte ich nicht auch noch hier posten.
Und das Design ist tatsächlich nicht sehr hübsch. Habe es so komplett übernommen und versuche es nur erstmal zum Laufen zu bringen

Vielen Dank!
Beste Grüße
-
Wobei ich natürlich für alle Verbesserungsvorschläge und Anregungen immer sehr dankbar bin
