1

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.

0

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@wormwood87 You are welcome! If one of the answers on the page looks right, consider accepting it by clicking the check mark next to it, to tell others that you are no longer actively looking for an improved answer.
My problem is not solved yet but now I understand why I have this problem, so it's good enough for me.
1

First of all, you do not seem to need pointer to pointer types for _bc. Simply define an array in class OtherClass (or an std::vector is actually better) and make Manager a friend of OtherClass. And of course you can remove the friend declaration from the BaseClass and the Manager classes:

class OtherClass
{
  public:
    OtherClass(){}
    void print(){
      cout << _bc[0].getNum() << " " << _bc[1].getNum() << endl;
    }   
  private:
    BaseClass * _bc;
  friend class Manager;
};

class Manager
{
  public:
  Manager(){}

  void run(){
    _oc._bc = new BaseClass[10];
    _oc._bc[0].setNum(20);
    _oc._bc[1].setNum(30);

    _oc.print();
  }

  private:
  OtherClass _oc;
};

1 Comment

The problem of scope makes the friend statement impossible to use when you have each class in its own header file. So for this example, friend class Manager will always give a compiling error because the Manager header file has declared the OtherClass header file but not the other way around, meaning OtherClass has no knowledge of the Manager class!
0

Your code would not function even if it compiled, as you did not correctly allocate the BaseClass instances. In addition, your use of raw new[] is extremely concerning. Consider using a std::vector<BaseClass> or std::vector<std::unique_ptr<BaseClass>> instead.

In your OtherClass, you will have to take a Manager pointer or reference and delay the definition of it's print() member. The compiler is entirely correct in telling you that OtherClass::print has nothing called _bc in scope. You must explicitly pass the Manager instance.

1 Comment

I know it doesn't compile that why I said "I am getting the error "was not declared on this scope"". I'll try that tomorrow, now i'm almost sleeping in my laptop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.