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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:04:22+00:00 2026-06-09T12:04:22+00:00

First of all this is not ‘homework’, its a problem from Thinking in C++

  • 0

First of all this is not ‘homework’, its a problem from Thinking in C++ Vol 1, chapter 5 ex 5.
I need to make 3 classes, the first granting friendship of its internals to the whole second class, and friendship to only a function from the third class.

I dont have a problem with granting friendship to the entire 2nd class, but with granting to the third class function, if i declare the third class in the same header theres no problem. But in different headers i get some undefined type / declaration. Thanks for your help and here is the code:

#ifndef FIRSTCLASS_H
#define FIRSTCLASS_H

//firstclasss header file

#include "secondclass.h"
#include "thirdclass.h"

class secondclass; //dummy declaration so it can share friendship
class thirdclass;  //it doesnt work when i want to give friendship to a function

class firstclass{
private:
    int a;
    int b;
public:
    friend secondclass; //granting friendship to the whole class
    friend void thirdclass::z(firstclass *); //error
    //use of undefined type 'thirdclass'
    //see declaration of 'thirdclass'

};

#endif FIRSTCLASS_H



#ifndef THIRDCLASS_H
#define THIRDCLASS_H

//thirdclass header file

#include "firstclass.h"

class firstclass;

class thirdclass{
public:
    void z(firstclass *);
};

#endif THIRDCLASS_H
  • 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-06-09T12:04:23+00:00Added an answer on June 9, 2026 at 12:04 pm

    You need to provide forward declarations only when you are not including the header for the corresponding class. Since you are including both secondclass.h and thirdclass.h already, you should skip the corresponding forward declarations altogether.

    In the thirdclass.h, however, you do not need firstclass.h: you are declaring a pointer to firstclass, not using its members, so you do not need an include.

    The general rule is that you should forward-declare your classes if all you need is a pointer, and include their headers when you need knowledge of the members of the class.

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

Sidebar

Related Questions

First of all this is not homework, I'm in a desperate need for a
First of all: This question is not directly programming related. However, the problem only
First of all I'm not sure this is even possible, however I need to
First of all: This is not a question about how to make a program
First of all, this is NOT homework. I'm using a book I bought for
First of all: this is not a homework assignment, it's for a hobby project
First of all this is a homework assignment so I'm looking for assistance, not
first of all: this is not the same as this . The ModelBackend has
First of all i have to clear few things 1.This is not spam group
First of all, I do not believe this belongs on Superuser. This belongs here

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.