c2512错误:没有适当的默认构造函数可用 [英] c2512 error: no appropriate default constructor available

查看:662
本文介绍了c2512错误:没有适当的默认构造函数可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使我声明了构造函数,我也遇到了c2512错误。我的代码是这样的:在我的first.h文件中,我声明它像:

I have encountered "c2512" error even if I declared the constructor. My code is like this: in my "first.h" file, I declared it like:

class myClass
{
public:
    tmpM ( cv::Mat& _model );
}

那么在我的first.cpp中:

then in my "first.cpp" I did:

#include "first.h"
myClass::tmpM ( cv::Mat& _model )
{
    ...
}

然后我把这个first.h second.h,然后在我的third.h中包含这个second.h,并在我的third.cpp中这样调用:

Then I included this "first.h" in my "second.h", then included this "second.h" in my "third.h", and called this class in my "third.cpp" like this:

cv::Mat myMat ( height, width, CV_8UC3 );
tmpM aM ( myMat );

但是这会导致c2512错误,说

But this gives out an c2512 error, saying



没有适当的默认构造函数

no appropriate default constructor available


我确实搜索过这个,发现我应该自己构建一个默认构造函数,我试图这样做在我的first.h:

I indeed searched about this, and found I should build a default constructor by myself, and I tried to do it like this in my "first.h":

class myClass
{
public:
    tmpM ( cv::Mat& _model) {};
}

然后得到一个错误:



函数myClass :: tmpM已具有正文

function "myClass::tmpM" already has a body


我尝试了其他几种方法,但仍然无法解决它。我不需要在这里构建一个默认构造函数,我想,但仍然困扰它。有人可以帮助我吗?

I tried several other methods to do it but still couldn't solve it. I don't need to build a default constructor here I think, but still bothered with it. Could someone help me?

编辑

好,建议后, :

class myClass
{
public:
myClass(cv::Mat& _model ) : tmpM (_model)
{
}
private:
cv::Mat& tmpM;
};

然后在我的first.cpp,我有一个错误说

Then in my "first.cpp", I got an error saying



声明不兼容

declaration is incompatible



b $ b

如何解决这个问题?

How could this be solved?

推荐答案

问题在这里似乎是你命名了你的类除了你命名的构造函数以外的东西。他们必须有相同的名字。此外,我假设你希望对传递的模型的引用被对象持有。

The problem here appears to be that you've named your class something other than what you've named your constructor. They have to have the same name. Also, I'm presuming you wish the reference to the model you're passing in to be held by the object. If you want to preserve it as a reference it must be initialized in the the constructors initializer list.

你想要的:

class myClass
{
public:
    myClass(cv::Mat& _model ) : tmpM (_model)
    {
    }
private:
    cv::Mat& tmpM;
};

这篇关于c2512错误:没有适当的默认构造函数可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆