为什么这不能正确初始化?
我正在尝试在另一个构造函数中初始化类构造函数。 GCC 引发错误,“类型‘foo’没有调用运算符。”这个伪代码应该解释我的意图。
class foo {
type arg1, arg2;
foo (type _arg1, type _arg2) {
_arg1=arg1;
_arg2=arg2;
}
}
class foo2 {
foo member;
foo2(type _arg1, type _arg2) {
member(_arg1, _arg2);
}
}
I'm trying to initialize a class constructor within another constructor. The GCC raises the error, 'Type 'foo' does not have a call operator.'This psuedo-code should explain my intentions.
class foo {
type arg1, arg2;
foo (type _arg1, type _arg2) {
_arg1=arg1;
_arg2=arg2;
}
}
class foo2 {
foo member;
foo2(type _arg1, type _arg2) {
member(_arg1, _arg2);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
两个问题:
首先,您的 foo 构造函数应该是公共的,如 Mark 的回答中所述。
其次,要使用其构造函数初始化成员,应使用以下语法:
Two issues:
First, your foo constructor should be public, as stated in Mark's answer.
Second, to initialize the member with its constructor, you should use the following syntax:
你的构造函数不是公开的;默认情况下,类中的所有内容都是私有的,除非您另外指定。我不确定这是否能解释您的确切错误消息。
Your constructors are not public; by default, everything in a class is private unless you specify otherwise. I'm not sure this explains your exact error message though.
您需要初始值设定项列表:
You want the initializer list:
您正在尝试使用
member
的构造函数。您应该在初始化列表中而不是在构造函数主体中执行此操作,即:You're trying to use
member
's constructor. You should do that in the initializer list and not in the constructor body, i.e.:伪代码可以解释您的意图,但它不能解释您的错误,因为它不会按照您描述的方式出错:
尽管它确实产生了有用的诊断:
这表明您不应该在此处发布“类似”的代码,并且期待有用的答案。
The pseudo-code may explain your intention but it doesn't explain your error as it doesn't error in the way you describe:
Although it does yield helpful diagnostics:
Which shows that you shouldn't post "sorta like" code here and expect useful answer.