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

  • SEARCH
  • Home
  • 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 3933376
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:35:37+00:00 2026-05-19T23:35:37+00:00

I have one class, called A, and it has it’s own header file. Then

  • 0

I have one class, called A, and it has it’s own header file. Then I have another class, called B, which also has it’s own header file. They each have their own .cpp file where I implement all of their functions.

I’m trying to have class B have a variable of class type A as a private variable, but I keep getting the error ‘A’ does not name a type

My code looks like this:

main.h:

#ifndef MAIN_H
#define MAIN_H

#include "A.h"
#include "B.h"

#endif

main.cpp:

#include "main.h"

int main( int argc, char* args[]) {
  B test;
}

A.h:

#ifndef A_H
#define A_H

#include "main.h"

class A {
  public:
    //public functions
  private:
    //private variables
};
#endif

B.h:

#ifndef B_H
#define B_H

#include "main.h"

class B {
  public:
    //public functions...
  private:
    A temp;
}
#endif

So all of my includes are in main.h, which includes A before B. B has a variable of type A, but it is included from being in main.h and B.h includes main.h. However, I keep getting an error saying:

error: 'A' does not name a type.

I’ve done some googling, and it seems like that means that A isn’t defined when you use it, but it should be defined there since it’s being included in main.h, right?

  • 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. Editorial Team
    Editorial Team
    2026-05-19T23:35:38+00:00Added an answer on May 19, 2026 at 11:35 pm

    The problem is that A.h includes main.h, which includes B.h, which tries to use A.

    The good way to organize your files would be this:

    main.h:

    // not needed
    

    main.cpp:

    #include "B.h" // for using class B
    
    int main( int argc, char* args[]) {
      B test;
    }
    

    A.h:

    #ifndef A_H
    #define A_H
    
    // no includes needed ATM
    
    class A {
      //...
    };
    #endif
    

    B.h:

    #ifndef B_H
    #define B_H
    
    #include "A.h" // for using class A
    
    class B {
      //public functions...
    }
    #endif
    

    That way, B.h is self-contained and can be used without having to include anything else before it. That’s very important as soon as your project grows above the toy level it is at now. Why would anyone trying to use what header x.h provides need to know to also include f.h, m.h, and u.h?

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

Sidebar

Related Questions

I have one class called dog and another class called cat. Dog has an
I have a library with a class called Recipient which has it's own fluent
I have one class called MainMenu, which has a method called switchViews, with 2
I have three files: one called sql.php witch has a class db that I
I have one class called Global and two other activities. In each activity I
I have a class User with one field called birthDate which is a java.sql.Date
I have a table called order which has many order_items , each order_items is
i have rather very basic question. I have one GUI class and another which
I have an external library(just one class file called org.vinay.dep.DependedClass).It is well within its
I have 2 questions: suppose we have one entity named class and another called

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.