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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:25:04+00:00 2026-06-16T07:25:04+00:00

This is part of a larger project. Basically I have a class X to

  • 0

This is part of a larger project.

Basically I have a class “X” to manage the program and that class has an array of pointers to objects from a class “Y” and there are another class “Z” where I need to access the objects of the class “Y”, for example a print.

I am getting the error “was not declared on this scope”

I tried to make the class “Z” friend of the class “Y” but it’s not working.

I wrote code to demonstrate this problem:

#include <iostream>
using namespace std;

class BaseClass;
class OtherClass;
class Manager;

class BaseClass
{
    friend class OtherClass;

    public:
        BaseClass(){}
        void setNum(int num){_num = num;}
        int getNum(){return _num;}

    private:
        int _num;
};

class OtherClass
{
    public:
        OtherClass(){}
        void print(){
        cout << _bc[0]->getNum() << " " << _bc[1]->getNum() << endl;
        }
};

class Manager
{
    friend class OtherClass;
    public:
        Manager(){}

        void run(){
            _bc = new BaseClass*[10];

            _bc[0]->setNum(20);
            _bc[1]->setNum(30);

           _oc.print();
        }

    private:
        BaseClass ** _bc;
        OtherClass _oc;
};


int main()
{
    Manager m;
    m.run();

    return 0;
}

Maybe this is very simple but it’s late here, i’m sleepy and I want to solve this problem before go to bed.

EDITED:
In my project I have a class Manager and that class have array of pointers to clients and orders.
The class orders receive among other things a client, that’s why I have to access that array of pointers, to choose what client to insert in the order.

  • 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-16T07:25:06+00:00Added an answer on June 16, 2026 at 7:25 am

    In C++ there are two distinct concepts – accessibility and scope. From your question it appears that you’ve got these two concepts somewhat mixed up (I do not blame you, because they are very close to each other).

    Accessibility is controlled by private, public, protected and so on, and can be additionally granted through “friendship”. Scope, on the other hand, is controlled by the placement of the variables and functions being accessed.

    In order for a member of a class to access a member of another class that other member must be in scope; it also must be accessible. It appears that you are attempting to access _bc from OtherClass, a friend of the class where _bc is declared. Friendship solves the accessibility part of the problem, but it does not address the scope part. In order for OtherClass to access _bc, an instance member of the Manager class, it must have a reference of some sort to a Manager object. There are many ways to get a reference – for example, you can pass a reference to the Manager in the constructor of OtherClass, store it in a private variable, and access _bc through it, like this:

    class OtherClass {
        Manager &_m;
    public:
        OtherClass(Manager &m) : _m(m) {}
        void print(){
            cout << _m._bc[0]->getNum() << " " << _m._bc[1]->getNum() << endl;
        }
    };
    

    This is only one way to solve the problem of scope; other ways include passing Manager as a parameter of OtherClass::print, making _m a pointer, making _bc static, and so on. The exact way depends on your requirements.

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

Sidebar

Related Questions

This is only part of a larger project. I have a text box on
I have an AngularJS application that requires authentication. It's part of a larger project
(this is indirectly a part of a much larger homework assignment) I have something
This part of an app that I am working on, I have the following
I have this part of script from my GAE application which uses webapp2, which
I was creating a very simple class as part of a much larger project.
This is part of a larger project I'm working on, but for now I'm
I am getting this error as part of this larger puzzle here . var
Considering this part of a Java class, private List<Object> components = new ArrayList<Object>(); private
I basically want to use the XML parser from Qt in my existing project.

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.