朋友班不工作

Run*_*ith 7 c++ private class friend

我得到典型的'...在此上下文中是私有'错误.你能告诉我我做错了什么吗?代码缩短了可读性.

在类SceneEditorWidgetController中:( settingsdialog和此处使用的变量在头文件中定义)

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

我的类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;
    };
}
Run Code Online (Sandbox Code Playgroud)

我无法修改IceProxyFinderBase类,但它之前使用的确如此(我可能是盲目的?)

有人可以解释一下我做错了什么吗?

Mik*_*our 18

对于非限定类名,friend声明声明在周围的命名空间中该名称的类是朋友,如果存在这样的类.所以这相当于

friend class GuiController::SceneEditorPluginWidgetController;
Run Code Online (Sandbox Code Playgroud)

但是,你的评论说这个类实际上是在全局命名空间中,而不是GuiController,所以这不会使它成为朋友.您需要正确限定它:

friend class ::SceneEditorPluginWidgetController;
Run Code Online (Sandbox Code Playgroud)

  • 如果它对其他人有用,我还必须在适当的命名空间中转发声明该类。 (3认同)