【发布时间】:2013-09-11 23:05:10
【问题描述】:
一旦我知道它属于哪个主题,我很乐意将这个帖子的标题更改为更合适的名称。
如果我改变了导致错误的构造函数的参数,没有错误。
仅当我包含该确切的构造函数时才会发生此错误:
error: no matching function for call to 'Object::Object(Object)'
note: candidates are: Object::Object(Object&)
note: Object::Object()
代码:
#include <iostream>
using namespace std;
class Object {
public:
Object() {} /* default constructor - no problems at all */
Object( Object & toCopy ) {} /* Cause of error, but no error when commented out */
Object func() {
Object obj;
return obj;
}
};
int main() {
Object o = o.func(); /* this is the line that the error is actually on */
return 0;
}
【问题讨论】:
-
我确实包含了它。它让我在发布问题之前将其格式化为代码,因此我将其包含在代码的顶部。
-
@computer 这是我实际代码的一个非常简化的版本,但重点是,我想知道这个错误的原因是什么,以便我可以将这些知识应用到我的程序中。
-
是的,我注意到就在我发表评论之后;)我稍微编辑了你的帖子,以便(视觉上)清楚地看到消息和代码都在那里。
-
下次我会像这样格式化我的问题,谢谢。
标签: c++ constructor compiler-errors