C++ C++;[]没有匹配的构造函数初始化
我有这样一个函数:C++ C++;[]没有匹配的构造函数初始化,c++,opencv,C++,Opencv,我有这样一个函数: #include <stdio.h> #include <opencv/cv.h> #include <opencv/highgui.h> #include <opencv/cxcore.h> #include <math.h> #define PI 3.14159265 #define GRADIENT_THRESHOLD 200.0 #define ANGLE_RANGE 20 using namespac
#include <stdio.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv/cxcore.h>
#include <math.h>
#define PI 3.14159265
#define GRADIENT_THRESHOLD 200.0
#define ANGLE_RANGE 20
using namespace cv;
using namespace std;
class Circle {
public:
int x;
int y;
int r;
Circle(int x, int y, int r) {
this->x = x;
this->y = y;
this->r = r;
}
double area() {
return PI * pow(r, 2);
}
};
Vector <Circle> collect_circles_from_houghSpace(Mat &houghSpaceCircle, double voting_threshold) {
int height = houghSpaceCircle.size[0];
int width = houghSpaceCircle.size[1];
int radius = houghSpaceCircle.size[2];
std::vector <Circle> circles;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
for (int r = 0; r < radius; r++) {
if (houghSpaceCircle.at<cv::Vec3i>(y, x)[r] > voting_threshold) {
circles.push_back(Circle(x, y, r));
}
}
}
}
return circles;
}
#包括
#包括
#包括
#包括
#包括
#定义PI 3.14159265
#定义梯度_阈值200.0
#定义角度范围20
使用名称空间cv;
使用名称空间std;
班级圈子{
公众:
int x;
int-y;
INTR;
圆(整数x,整数y,整数r){
这个->x=x;
这->y=y;
这个->r=r;
}
双区(){
返回PI*pow(r,2);
}
};
向量收集来自houghSpace的圈(Mat和houghSpace圈,双投票阈值){
int height=houghSpaceCircle.size[0];
int width=houghSpaceCircle.size[1];
int radius=houghSpaceCircle.size[2];
std::向量圆;
对于(int y=0;y表决阈值){
圆。推回(圆(x,y,r));
}
}
}
}
返回圈;
}
当我编译它时,我得到一个错误,它说:
在sobel.cpp中包含的文件中:2:
In file included from /usr/local/Cellar/opencv@2/2.4.13.7_5/include/opencv/cv.h:64:
In file included from /usr/local/Cellar/opencv@2/2.4.13.7_5/include/opencv2/core/core.hpp:4932:
/usr/local/Cellar/opencv@2/2.4.13.7_5/include/opencv2/core/operations.hpp:2415:23: error: no matching constructor for initialization of 'Circle []'
newData = new _Tp[newCapacity];
^
/usr/local/Cellar/opencv@2/2.4.13.7_5/include/opencv2/core/operations.hpp:2400:13: note: in instantiation of member function 'cv::Vector<Circle>::reserve' requested here
reserve(_size);
^
/usr/local/Cellar/opencv@2/2.4.13.7_5/include/opencv2/core/operations.hpp:2305:7: note: in instantiation of member function 'cv::Vector<Circle>::set' requested here
{ set(!vec.empty() ? (_Tp*)&vec[0] : 0, vec.size(), _copyData); }
^
sobel.cpp:166:12: note: in instantiation of member function 'cv::Vector<Circle>::Vector' requested here
return circles;
^
sobel.cpp:15:7: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided
class Circle {
^
sobel.cpp:21:5: note: candidate constructor not viable: requires 3 arguments, but 0 were provided
Circle(int x, int y, int r) {
^
1 error generated.
包含在/usr/local/ceral中的文件中/opencv@2/2.4.13.7_5/include/opencv/cv.h:64:
在/usr/local/ceral中包含的文件中/opencv@2/2.4.13.7_5/include/opencv2/core/core.hpp:4932:
/usr/本地/酒窖/opencv@2/2.4.13.7_5/include/opencv2/core/operations.hpp:2415:23:错误:没有用于初始化“Circle[]的匹配构造函数
newData=new_Tp[newCapacity];
^
/usr/本地/酒窖/opencv@2/2.4.13.7_5/include/opencv2/core/operations.hpp:2400:13:注意:在这里请求的成员函数“cv::Vector::reserve”的实例化中
储备(单位大小);
^
/usr/本地/酒窖/opencv@2/2.4.13.7_5/include/opencv2/core/operations.hpp:2305:7:注意:在成员函数“cv::Vector::set”的实例化中,此处请求
{set(!vec.empty()?(_-Tp*)&vec[0]:0,vec.size(),_-copyData);}
^
sobel.cpp:166:12:注意:在成员函数“cv::Vector::Vector”的实例化中,此处请求
返回圈;
^
sobel.cpp:15:7:注意:候选构造函数(隐式副本构造函数)不可行:需要1个参数,但提供了0
班级圈子{
^
sobel.cpp:21:5:注意:候选构造函数不可行:需要3个参数,但提供了0
圆(整数x,整数y,整数r){
^
生成1个错误。
有人能给我解释一下这个错误以及如何修复它吗?在我看来,我已经创建了一个包含3个参数的构造函数。我不知道为什么错误会说我提供了0个参数
sobel.cpp:15:7: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided
class Circle {
错误消息表明缺少复制构造函数。
这是有意义的,因为您在返回过程中复制向量函数的基本部分是:
cv::Vector<Circle> foo() {
std::vector <Circle> circles;
return circles;
}
它将被调用以执行转换
std::vector
本身不要求T
是默认可构造的,因此错误发生在cv::vector::vector
内部。其reserve()
成员函数在构造期间调用,具有以下行:
newData = new T[newCapacity];
此行确实要求T
是默认可构造的。这就是为什么会出现错误以及错误日志中提到reserve()
的原因。要修复它,请声明默认的默认构造函数:
class Circle {
public:
Circle() = default;
...
};
另外请注意,除非将copyData
设置为true
,否则圆
中的数据将不会复制到Vector
中
return {circles, true};
或
cv::Vector
将存储指向圆圈[0]
的指针,当函数返回时,当本地对象圆圈
被销毁后,该指针将变得无效
关于std::move
的注释。您可以尝试
return Vector<Circle>(std::move(circles), true);
为了避免不必要的复制,您可能希望从一开始就使用
cv::Vector circles;
。@Miki-hmm我明白了……这很奇怪。@nz\u 21这并不奇怪。假设您创建了std::Vector vec(2);std::vector将如何实例化Circle类型的这两个对象?@Miki-hmm…我宁愿利用移动语义,也不愿将其复制过来。我正在考虑执行return std::move(circles)
——这是个好主意吗?@nz_21,!注意cv::vector
(您的返回类型)与std::vector
(<代码> >类型>代码>)。您可能想坚持其中一个。(<代码> CV::向量在最近的OpenCV版本中不存在)。这是一个令人困惑的语言:(所以,如果我有这个权利,C++标准保证它将被移动(不复制)。但是,我仍然需要创建一个复制构造函数…不,Circle
确实有一个隐式定义的复制构造函数。这是因为您正在从std::Vector构造一个向量,它正在复制元素。我不熟悉Vector,但我想您可以尝试使用Vector circles
而不是std::Vector
forloop@Evg同意。但Vector在内部使用圆圈[]来存储元素,这需要默认构造函数
return cv::Vector<Circle>(circles, true);
return circles;
return Vector<Circle>(std::move(circles), true);
Vector(std::vector<T> vec) : internal_vec(std::move(vec)) { }