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

发布于 2024-12-12 02:36:21 字数 1047 浏览 0 评论 0原文

我正在尝试解决从 Microsoft Visual Studio 10 编译器收到的此错误。错误是: 某些类:错误 C2248:无法访问类“”中声明的受保护成员。这是重现此错误的代码。我似乎无法弄清楚如何创建一个拥有另一个具有受保护的默认构造函数的对象的对象。我有另一个构造函数,它接受一个输入参数,但无论我应用什么逻辑推理,似乎都无法调用它。显然,我错过了一些愚蠢或非常重要的东西,所以我把它放在这里,看看是否有人能发现我的错误。谢谢大家!!!

#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'm trying to get around this error I get from Microsoft Visual Studio 10 Compiler. The error is:
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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

埖埖迣鎅 2024-12-19 02:36:21

我有另一个构造函数,它接受一个输入参数,但无论我应用什么逻辑推理,似乎都无法调用它。

啊,现在我们到了重要的部分(很抱歉,其他答案似乎没有给您理解 protected 关键字的信用,但您确实似乎有点混淆了你提出问题的方式)。您确实有该构造函数,但您还有有一个默认构造函数。你写了多少个可用的构造函数并不重要;重要的是。不工作的仍然会导致编译时错误。

容器类的默认构造函数没有初始化列表,因此将尝试使用数据成员的默认构造函数。由于您无权访问成员的默认构造函数,因此编译容器的默认构造函数将失败。

可能的解决方案:使用容器的默认构造函数的初始化列表中的另一个构造函数显式初始化该成员。这意味着您必须以某种方式弥补该价值。 (这并不总是可能的。当发生这种情况时,编译器会以一种方式告诉您,拥有默认构造函数对于容器类来说没有意义。:))

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). 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. :) )

赤濁 2024-12-19 02:36:21

protected 成员只能由派生类的同一实例访问,而不能由包含此类成员的对象访问。您想做的事情无法完成,不允许 Goo 根据您要保护的默认构造函数的请求创建 Foo 的默认构造实例。

protected members are only to be accessed by the same instance of a derived class, not by objects containing such a member. 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.

酒与心事 2024-12-19 02:36:21

您不能通过以下类访问受保护成员:

a) 不是同一个类

b) 不是由具有受保护成员的类派生的类。

You can't access a protected member by a class which :

a) Is not the same class

b) Is not a class which derives by the class with the protected member.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文