2016-04-13 15 views
0

Hallo, ich versuche, eine Factory-Methode zu erstellen, die eine abgeleitete Klasse der Klasse A zurückgibt, habe ich Probleme zu verstehen, zirkuläre Erklärung, ich hoffe, Sie können mir helfen, dies zu lösen.Probleme zu verstehen, zirkuläre Erklärung

Danke.

AChildOne.cpp


#include "AChildOne.h" 

AChildOne.h


#ifndef ACHILDONE_H 
#define ACHILDONE_H 
#include "A.h" 

class A_CHILD_ONE : public A { 
}; 

#endif 

A.cpp


#include "A.h" 

void A::a(){ 
    Factory::fact(); 
}; 

A. h


#ifndef A_H 
#define A_H 

#include "Factory.h" 

class A { 
    public: 
     static void a(); 
}; 

#endif 

Factory.cpp


#include "Factory.h" 
A *Factory::fact(){ 
    return new A_CHILD_ONE; 
} 

Factory.h


#ifndef FACTORY_H 
#define FACTORY_H 

#include "A.h" 
#include "AChildOne.h" 

class Factory { 
    public: 
    static A *fact(); 
}; 

#endif 

Übersetzungsfehler

g++ A.cpp Factory.cpp AChildOne.cpp -o test 
In file included from Factory.h:5:0, 
       from A.h:4, 
       from A.cpp:1: 
AChildOne.h:5:30: error: expected class-name before ‘{’ token 
class A_CHILD_ONE : public A { 
          ^
In file included from A.h:4:0, 
       from A.cpp:1: 
Factory.h:9:10: error: ‘A’ does not name a type 
    static A *fact(); 
     ^
A.cpp: In static member function ‘static void A::a()’: 
A.cpp:4:2: error: ‘fact’ is not a member of ‘Factory’ 
    Factory::fact(); 
^
In file included from A.h:4:0, 
       from AChildOne.h:3, 
       from AChildOne.cpp:1: 
Factory.h:9:10: error: ‘A’ does not name a type 
    static A *fact(); 
     ^
+1

Was ist der Sinn, 'factory.h' in' A.h' einzubeziehen? – SergeyA

+0

Ich rufe Factory :: fact(); von A :: a() – Zindan

Antwort

0

In Factory.h Sie versuchen A.h aufzunehmen; In A.h versuchen Sie, Factory.h einzuschließen.

Einschließlich Factory.h in A.cpp und Entfernen von A.h sollte helfen.

Factory Erklärung hängt von A Schnittstelle ab. A Erklärung hängt nicht von Factory Erklärung ab, aber A Definition tut.

Auch Factory.h muss nicht über AChildOne.h wissen, aber Factory.cpp tut. So verschieben Sie #include AChildOne.h zu Factory.cpp.

+0

'Factory.cpp: In statische Member-Funktion 'statische A * Factory :: Fakt()': Factory.cpp: 3: 13: Fehler: erwartete Typ-Specifier vor 'A_CHILD_ONE' zurück neu A_CHILD_ONE; ^ Factory.cpp: 3: 13: Fehler: erwartet ';' vor 'A_CHILD_ONE' Factory.cpp: 3: 13: Fehler: 'A_CHILD_ONE' wurde in diesem Bereich nicht deklariert ' – Zindan

+0

BTW, was ist die Bedeutung wann Sie setzen Factory.h in A.cpp und nicht in Ah? – Zindan

+0

@Zindan Wie ich verstehe, haben Sie diesen Fehler, nachdem Sie "#include AChildOne.h" von "Factory.h" entfernt haben. Ich mache die Antwort präziser: '#include AChildOne.h' sollte in' Factory.cpp' stehen. –