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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:59:03+00:00 2026-05-26T14:59:03+00:00

If I have classes A and B, how can I create a reference to

  • 0

If I have classes A and B, how can I create a reference to B inside of A? I read that you need use a pointer or a reference, but I can’t find out more than that. Here’s an example of what I’m talking about:

class B;

class A {
    public: 

    B * b_pointer;

    void setSelf(B * given_b_pointer) {
        b_pointer = given_b_pointer;
    };

    void printBName() {
        print (b_pointer.my_name);
    };
};

class B {
    public:

    string my_name;

    void setSelf(string my_given_name) {
        my_name = my_given_name;
    };
}

This gives me several errors. What am I doing wrong, and how can I fix it?

EDIT: The relevant error message:

error: request for member ‘my_name’ in ‘((B*)this)->A::b_pointer’, which
is of non-class type ‘B*’.

Error message slightly edited to replace actual class names with psuedo-class names.

  • 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-26T14:59:03+00:00Added an answer on May 26, 2026 at 2:59 pm

    [Updated] You need to forward declare class B and you can not use any of B’s members in the class until it has been declared so move the declaration of printBName to the code file.

    class B; //Forward declaration
    
    class A {
        public: 
        B * b_ref;
    
        ...
    
        void printBName(); //Need to forward declare this as well
    };
    
    class B {
        ...
    };
    
    //Now define printBName
    void A::printBName(){
        print (b_ref->my_name);// use -> to dereference b_ref
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why can NHibernate create a proxy for classes with properties that have private setters
I have a T4 template that generates classes from an xml file. How can
I have several classes (A, B, C, ...) that all use a List<AnotherClass> to
I have an interesting problem. I'd like to create a generic class that can
Can JavaScript classes/objects have constructors? How are they created?
I have two view controller classes in my application delegate. I can change from
I have classes that store data, and methods to go get data for individual
I have classes that are named exactly the same across different plug-ins that I
I have classes that are needed in both my web service and my server.
Im trying to figure out how I can have multiple pagers associated to multiple

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.