Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Friend class not working

I am getting the typical '... is private within this context' error. Can you tell me what I am doing wrong? Code is shortened for readability.

in class SceneEditorWidgetController: (settingsdialog and the variable used here is defined in the header)

SceneEditorPluginWidgetController::SceneEditorPluginWidgetController()
{
}
void SceneEditorPluginWidgetController::configured()
{
    priorKnowledge_setting = settingsDialog->priorKnowledgeProxyFinder->getSelectedProxyName().toStdString(); //This is the context
}

My class SettingsController.h

namespace Ui {
    class SettingsController;
}
namespace GuiController {
    class SettingsController : public QDialog
    {
        Q_OBJECT
        friend class SceneEditorPluginWidgetController;
    public:
        explicit SettingsController(QWidget *parent = 0);
        ~SettingsController();

    private: //it is private here
        Ui::SettingsController* ui;
        IceProxyFinderBase* priorKnowledgeProxyFinder;
    };
}

I cannot modify the IceProxyFinderBase class, but it was used exactly (I'm probably blind?) like this before.

Could somebody please explain what I am doing wrong?

like image 678
RunOrVeith Avatar asked Feb 03 '15 19:02

RunOrVeith


People also ask

What is difference between friend class and inheritance?

Inheritance and Friendship in C++ Difference between Inheritance and Friendship in C++: In C++, friendship is not inherited. If a base class has a friend function, then the function doesn't become a friend of the derived class(es).

Can a friend class be inherited?

In C++, the friendship is not inherited. It means that, if one parent class has some friend functions, then the child class will not get them as friend.

How do you declare a class as friend?

But, to declare any class as a friend class, you do it with the friend keyword. You can use the friend keyword to any class to declare it as a friend class. This keyword enables any class to access private and protected members of other classes and functions.

What do friend classes have access to?

Friend Class is a class that can access both private and protected variables of the class in which it is declared as a friend, just like a friend function. Classes declared as friends to any other class will have all the member functions as friend functions to the friend class.


1 Answers

With an unqualified class name, the friend declaration declares that a class of that name, in the surrounding namespace, is a friend, if such a class exists. So this is equivalent to

friend class GuiController::SceneEditorPluginWidgetController;

However, your comments say that the class is actually in the global namespace, not GuiController, so this doesn't make it a friend. You'll need to qualify it correctly:

friend class ::SceneEditorPluginWidgetController;
like image 94
Mike Seymour Avatar answered Sep 23 '22 04:09

Mike Seymour