Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Tricky Inheritance Class Definition Problem
    text
    copied!<p>I'm getting this error when dealing with a number of classes including each other:</p> <pre><code>error: expected class-name before '{' token </code></pre> <p>I see what is going on, but I do not know how to properly correct it. Here is an abstracted version of the code:</p> <p><em>A.h</em></p> <pre><code>#ifndef A_H_ #define A_H_ #include "K.h" class A { public: A(); }; #endif /*A_H_*/ </code></pre> <p><em>A.cpp</em></p> <pre><code>#include "A.h" A::A() {} </code></pre> <p><em>B.h</em></p> <pre><code>#ifndef B_H_ #define B_H_ #include "A.h" class B : public A { // error: expected class-name before '{' token public: B(); }; #endif /*B_H_*/ </code></pre> <p><em>B.cpp</em></p> <pre><code>#include "B.h" B::B() : A() {} </code></pre> <p><em>J.h</em></p> <pre><code>#ifndef J_H_ #define J_H_ #include "B.h" class J { public: J(); }; #endif /*J_H_*/ </code></pre> <p><em>J.cpp</em></p> <pre><code>#include "J.h" J::J() {} </code></pre> <p><em>K.h</em></p> <pre><code>#ifndef K_H_ #define K_H_ #include "J.h" class K : public J { // error: expected class-name before '{' token public: K(); }; #endif /*K_H_*/ </code></pre> <p><em>K.cpp</em></p> <pre><code>#include "K.h" K::K() : J() {} </code></pre> <p><em>main.cpp</em></p> <pre><code>#include "A.h" int main() { return 0; } </code></pre> <hr> <p>Starting in <em>main.cpp</em>, I can determine that this is what the compiler sees:</p> <pre><code>#include "A.h" #ifndef A_H_ #define A_H_ #include "K.h" #ifndef K_H_ #define K_H_ #include "J.h" #ifndef J_H_ #define J_H_ #include "B.h" #ifndef B_H_ #define B_H_ #include "A.h" class B : public A { // error: expected class-name before '{' token </code></pre> <p>So, <em>A</em>'s definition is not complete when we get to <em>B</em>. I've been told that sometimes you need to use a forward declaration and then move the <em>#include</em> statement into the <em>.cpp</em> file, but I'm not having any luck with that. If I try anything like that, I simply get the additional error:</p> <pre><code>error: forward declaration of 'struct ClassName' </code></pre> <p>I think maybe I'm just not doing things in the right places. Can someone please show me how to get this code to compile? Thank you very much!</p> <hr> <p>Edit: I want to point out that this is just abstracted version of the real code. I realize that there are no references to <em>K</em> in <em>A</em> or <em>B</em> in <em>J</em>, but there are in the real code and I feel that they're completely necessary. Perhaps if I give a brief description of the real classes, someone can help me restructure or fix my code.</p> <p>Class <em>A</em> is an abstract node class that acts as an interface for nodes in a graph. Class <em>B</em> is one of what will be a number of different implementations of <em>A</em>. In the same manner, class <em>J</em> is an abstract Visitor class and <em>K</em> is the corresponding implementation. Here is the code with a little more context:</p> <p><em>A.h</em> (Abstract Node)</p> <pre><code>#ifndef A_H_ #define A_H_ #include "K.h" class K; class A { public: A(); virtual void accept(const K&amp;) const = 0; }; #endif /*A_H_*/ </code></pre> <p><em>A.cpp</em></p> <pre><code>#include "A.h" A::A() {} </code></pre> <p><em>B.h</em> (Concrete Node)</p> <pre><code>#ifndef B_H_ #define B_H_ #include "A.h" class K; class B : public A { // error: expected class-name before '{' token public: B(); virtual void accept(const K&amp;) const; }; #endif /*B_H_*/ </code></pre> <p><em>B.cpp</em></p> <pre><code>#include "B.h" B::B() : A() {} void B::accept(const K&amp; k) const { k.visit(this); } </code></pre> <p><em>J.h</em> (Abstract Visitor)</p> <pre><code>#ifndef J_H_ #define J_H_ #include "B.h" class B; class J { public: J(); virtual void visit(const B*) const = 0; }; #endif /*J_H_*/ </code></pre> <p><em>J.cpp</em></p> <pre><code>#include "J.h" J::J() {} </code></pre> <p><em>K.h</em> (Concrete Visitor)</p> <pre><code>#ifndef K_H_ #define K_H_ #include "J.h" class B; class K : public J { // error: expected class-name before '{' token public: K(); virtual void visit(const B*) const; }; #endif /*K_H_*/ </code></pre> <p><em>K.cpp</em></p> <pre><code>#include "K.h" K::K() : J() {} void K::visit(const B*) const {}; </code></pre> <p><em>main.cpp</em></p> <pre><code>#include "A.h" int main() { return 0; } </code></pre> <p>I had to add some forward declarations to make some additional errors that appeared (when I added detail) go away. Some of them may not be necessary or correct.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload