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 3242978
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:22:44+00:00 2026-05-17T18:22:44+00:00

I am sure this problem is asked a lot but I can’t seem to

  • 0

I am sure this problem is asked a lot but I can’t seem to find anything relevant. I have multiple source files in a C++ project. In typical fashion, there are multiple header files with class and function declarations and associated source files with their definitions. The problem is that when I try to use one of my classes defined in another file as a member for a class in a different file, I get compile errors even when uses the #include directive. What fixes the problem is by prototyping (is that the right word?) the class first before declaring it a member. So if ClassA is in one file and ClassB is in another and I want to use a ClassA member in ClassB I must write:


// ClassA.h
class ClassA {
    public: ClassA (void); };
// ClassB.h class ClassA; // prototype class ClassB { public: ClassA* ca; };

Is this normal? It doesn't matter if I use pointers or instances, I still must prototype them. I have found that I also must prototype structs and enums if they are in separate files. I can't seem to use constants declared using #define, or const across multiple files I get errors that they are undefined so I am not sure how to give them more than file scope. The same goes for typedefs. I am sure there is some easy fix to this that I am not remembering... any help is appreciated!

  • 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-17T18:22:44+00:00Added an answer on May 17, 2026 at 6:22 pm

    The word you’re looking for is “declare”, not “prototype”.

    In any case, that’s normal. Usually you’d just include the relevant header files:

    // ClassB.h
    
    #include "ClassA.h"
    
    class ClassB {
    public: ClassA* ca; };
    

    This will cause problems with circular references, though (but in all other cases it’s fine).

    Another thing: Don’t forget your include guards, to protect against multiple inclusion of a file. That is, always write header files in the following way:

    #ifndef UNIQUE_IDENTIFIER_HERE
    #define UNIQUE_IDENTIFIER_HERE
    
    // Rest of header file here.
    
    #endif // ndef UNIQUE_IDENTIFIER_HERE
    

    The UNIQUE_IDENTIFIER_HERE is usually an all-uppercase variant of the header file name, e.g. ‹PROJECTNAME›_‹PATH_TO_HEADER›_‹HEADERNAME›_H. For example, I’m currently working in a project (called “SeqAn”) that has a header file in the path parallel/taskdata.h. The unique identifier I use is thus SEQAN_PARALLEL_TASKDATA_H.

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

Sidebar

Related Questions

i'm pretty sure that this question has allready been asked (but i didn't find
I'm not sure if this a settings problem or an HTML problem, but on
I am sure this is a very simple problem, but I am new to
I'm sure there are different approaches to this problem, and I can think of
This is a problem I'm sure is easy to fix, but I've been banging
I am not sure what the problem is but I keep receiving this error
I'm sure this problem has been solved before and I'm curious how its done.
I'm sure this must be a common problem. I've got a class that in
I'm not sure how to describe this problem, so I think an example is
Uhm I'm not sure if anyone has encountered this problem a brief description is

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.