【发布时间】:2011-12-15 21:43:51
【问题描述】:
我正在尝试解决我从 Microsoft Visual Studio 10 编译器得到的这个错误。错误是:
Some class : error C2248: cannot access protected member declared in class ''。这是重现此错误的代码。我似乎无法弄清楚如何创建一个拥有另一个对象的对象,该对象具有受保护的默认构造函数。我有另一个构造函数,它接受一个输入参数,但无论我应用什么逻辑推理,似乎都无法调用它。显然,我错过了一些愚蠢或非常重要的东西,所以我把它放在这里看看是否有人能发现我的错误。谢谢大家!!!
#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);
}
【问题讨论】:
标签: c++ constructor protected