简体   繁体   English

命名空间内错误c2248朋友类

[英]error c2248 friend class inside namespace

I'm trying to set up code similar to this: 我正在尝试设置与此类似的代码:

//Class1.h
namespace A {
    class Class1 {
        friend class Class2
        public:
            Class1();
            ~Class1();
        private:
            void Create() { /*do nothing for now*/ };
    };
}

//Class2.h
#include "Class1.h"
namespace A {
    class Class2 {
        void Test();
    };
} //end namespace A

//Class2.cpp
#include "Class2.h"
namespace A {
    void Class2::Test() {
        Class1 object = Class1();
        object.Create(); //error c2248
    }
}

And I'm getting the error 而且我得到了错误

"Error 101 error C2248: 'Class1::Create' : cannot access private member declared in class 'Class1'" “错误101错误C2248:'Class1 :: Create':无法访问在类'Class1'中声明的私有成员”

. What would cause this? 是什么原因造成的? Am I declaring friendship the wrong way? 我是在错误地宣告友谊吗?

I tried your example with Code:Blocks 13.12 (SDK Version 1.19.0) as well as on ideone.com . 我想你的例子与代码:块13.12(SDK版本1.19.0),以及对ideone.com

The only changes I made to get it compile successfully was 成功进行编译的唯一更改

  • adding the semicolon after the friend statement 在好友声明之后添加分号
  • adding empty blocks {} to ctr and dtr of Class1 将空块{}添加到Class1 ctr和dtr中
  • of course, I also removed the include statements (put all into one file) 当然,我也删除了include语句(全部放入一个文件中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM