简体   繁体   English

错误C2248:受保护的默认构造函数C ++在另一个类所拥有的类中

[英]Error C2248: Protected Default Constructor C++ within an class owned by another class

I'm trying to get around this error I get from Microsoft Visual Studio 10 Compiler. 我试图解决从Microsoft Visual Studio 10编译器得到的这个错误。 The error is: Some class : error C2248: cannot access protected member declared in class '' . 错误是: Some class : error C2248: cannot access protected member declared in class '' Here is code that reproduces this error. 这是重现此错误的代码。 I can't seem to figure out how to create an object that owns another object that has a protected default constructor. 我似乎无法弄清楚如何创建一个拥有另一个具有受保护的默认构造函数的对象的对象。 I have another constructor that takes an input parameter but can't seem to call it no matter what logical reasoning I apply. 我有另一个带有输入参数的构造函数,但无论我应用什么逻辑推理,似乎都无法调用它。 Obviously, I'm missing something silly or really important, so I'm putting it on here to see if anyone can catch my error. 显然,我错过了一些愚蠢或非常重要的东西,所以我把它放在这里,看看是否有人能抓住我的错误。 Thanks to all!!! 谢谢大家!!!

#ifndef FOO_H
#define FOO_H

class Foo {
    public :
        int myFooInt;

        ~Foo();

        Foo(int fooInt);

    protected :   //Uncomment to generate C2248 Error
        Foo();
};

#endif

.

#include "foo.h"

Foo::Foo() {

}
Foo::Foo(int fooInt) : myFooInt(fooInt) {

}

Foo::~Foo() {
}

.

#ifndef GOO_H
#define GOO_H

#include "foo.h"

class Goo {
    public :

        ~Goo();

        Goo();

        Goo(Foo foo);

        Foo myFoo;

};

#endif 

.

#include "Goo.h"

Goo::Goo() {

}

Goo::Goo(Foo foo) : myFoo (foo) {
}

Goo::~Goo() {
}

.

#include "foo.h"
#include "goo.h"

void main() {
    Foo foo(5);
    Goo goo(foo);
}

I have another constructor that takes an input parameter but can't seem to call it no matter what logical reasoning I apply. 我有另一个带有输入参数的构造函数,但无论我应用什么逻辑推理,似乎都无法调用它。

Ah, now we get to the important part (and sorry that the other answers don't seem to give you credit for understanding the protected keyword, but you do seem a bit confused the way you present your question). 啊,现在我们进入了重要的部分(很遗憾,其他答案似乎没有让您对理解protected关键字有所帮助 ,但您似乎对提出问题的方式感到有点困惑)。 You do have that constructor, but you also have a default constructor. 你有这个构造函数,但你有一个默认的构造函数。 It doesn't matter how many working constructors you write; 你写的工作构造函数有多少并不重要; the non-working one will still cause compile-time errors. 非工作的仍然会导致编译时错误。

Your default constructor for the container class has no initialization list, and therefore will attempt to use the default constructors for data members. 容器类的默认构造函数没有初始化列表,因此将尝试对数据成员使用默认构造函数。 Since you don't have access to the member's default constructor, compiling the container's default constructor fails. 由于您无权访问该成员的默认构造函数,因此编译容器的默认构造函数失败。

Possible solution: explicitly initialize the member using another constructor, in the container's default constructor's initialization list. 可能的解决方案:在容器的默认构造函数的初始化列表中,使用另一个构造函数显式初始化成员。 This means that you'll have to make up the value somehow. 这意味着你必须以某种方式弥补价值。 (That isn't always possible. When that happens, it's the compiler's way of telling you that having a default constructor doesn't make sense for the container class. :) ) (这并不总是可行。当发生这种情况时,编译器会告诉您具有默认构造函数对容器类没有意义。:))

protected members are only to be accessed by the same instance of a derived class, not by objects containing such a member. protected成员只能由派生类的同一实例访问,而不能由包含此类成员的对象访问。 What you want to do can't be done, Goo is not allowed to create a default constructed instance of Foo per your request of the default constructor to be protected . 您想做的事情无法完成,根据您对protected的默认构造函数的要求, Goo不允许创建Foo的默认构造实例。

You can't access a protected member by a class which : 您无法通过以下类访问受保护的成员:

a) Is not the same class a)不是同一个班级

b) Is not a class which derives by the class with the protected member. b)不是由受保护成员的类派生的类。

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

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