Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 187921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:54:38+00:00 2026-05-11T15:54:38+00:00

I’m getting this error when dealing with a number of classes including each other:

  • 0

I’m getting this error when dealing with a number of classes including each other:

error: expected class-name before '{' token 

I see what is going on, but I do not know how to properly correct it. Here is an abstracted version of the code:

A.h

#ifndef A_H_ #define A_H_  #include 'K.h'  class A {     public:         A();  };  #endif /*A_H_*/ 

A.cpp

#include 'A.h'  A::A() {} 

B.h

#ifndef B_H_ #define B_H_  #include 'A.h'  class B : public A { // error: expected class-name before '{' token     public:         B(); };  #endif /*B_H_*/ 

B.cpp

#include 'B.h'  B::B() : A() {} 

J.h

#ifndef J_H_ #define J_H_  #include 'B.h'  class J {     public:         J(); };  #endif /*J_H_*/ 

J.cpp

#include 'J.h'  J::J() {} 

K.h

#ifndef K_H_ #define K_H_  #include 'J.h'  class K : public J { // error: expected class-name before '{' token     public:         K(); };  #endif /*K_H_*/ 

K.cpp

#include 'K.h'  K::K() : J() {} 

main.cpp

#include 'A.h'  int main() {     return 0; } 

Starting in main.cpp, I can determine that this is what the compiler sees:

#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 

So, A‘s definition is not complete when we get to B. I’ve been told that sometimes you need to use a forward declaration and then move the #include statement into the .cpp file, but I’m not having any luck with that. If I try anything like that, I simply get the additional error:

error: forward declaration of 'struct ClassName' 

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!


Edit: I want to point out that this is just abstracted version of the real code. I realize that there are no references to K in A or B in J, 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.

Class A is an abstract node class that acts as an interface for nodes in a graph. Class B is one of what will be a number of different implementations of A. In the same manner, class J is an abstract Visitor class and K is the corresponding implementation. Here is the code with a little more context:

A.h (Abstract Node)

#ifndef A_H_ #define A_H_  #include 'K.h'  class K;  class A {     public:         A();          virtual void accept(const K&) const = 0; };  #endif /*A_H_*/ 

A.cpp

#include 'A.h'  A::A() {} 

B.h (Concrete Node)

#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&) const; };  #endif /*B_H_*/ 

B.cpp

#include 'B.h'  B::B() : A() {}  void B::accept(const K& k) const { k.visit(this); } 

J.h (Abstract Visitor)

#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_*/ 

J.cpp

#include 'J.h'  J::J() {} 

K.h (Concrete Visitor)

#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_*/ 

K.cpp

#include 'K.h'  K::K() : J() {}  void K::visit(const B*) const {}; 

main.cpp

#include 'A.h'  int main() {     return 0; } 

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T15:54:38+00:00Added an answer on May 11, 2026 at 3:54 pm

    Circular inclusions do not work.

    Try to keep inclusions to a strict minimum. If you do that, you’ll either be fine altogether, or you’ll discover problems in your design. In your case, i don’t see anything wrong with your design.

    When defining class K, you’re only using a pointer to an object of type B. That does not require B to be defined (as in ‘include the header file’), only to be declared (forward declaration is fine). So, in your case, removing inclusion to header ‘B.h’ replaced by ‘class B;’ is sufficient. (the same goes for class J)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This will give you 1 & 4: select a.values from… May 12, 2026 at 1:13 am
  • Editorial Team
    Editorial Team added an answer What you're looking for are the "binary" addition and subtraction… May 12, 2026 at 1:13 am
  • Editorial Team
    Editorial Team added an answer To check for overlapping div's you might wanna do a… May 12, 2026 at 1:13 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.