gpt4 book ai didi

c++ - 错误 C2665 : 'cv::fillPoly' : none of the 2 overloads could convert all the argument types

转载 作者:行者123 更新时间:2023-11-28 06:32:50 24 4
gpt4 key购买 nike

我得到这个奇怪的错误,这段代码编译(并正确显示一个三角形):

Point pts[3] = { Point(20, 20), Point(60, 20), Point(40, 40) };
const Point *ptsp = pts;
int npts[1] = {3};
fillPoly(img, &ptsp, npts, 1, Scalar(255, 255, 0));

但如果我将 const Point *ptsp = pts; 替换为 Point *ptsp = pts,则会出现此错误:

1>C:\Workspace\ImageProcessing\Tutorials\src\main.cpp(16): error C2665: 'cv::fillPoly' : none of the 2 overloads could convert all the argument types
1> C:\Workspace\ImageProcessing\opencv\build\include\opencv2/core/core.hpp(2632): could be 'void cv::fillPoly(cv::Mat &,const cv::Point **,const int *,int,const cv::Scalar &,int,int,cv::Point)'
1> C:\Workspace\ImageProcessing\opencv\build\include\opencv2/core/core.hpp(2637): or 'void cv::fillPoly(cv::InputOutputArray,cv::InputArrayOfArrays,const cv::Scalar &,int,int,cv::Point)'
1> while trying to match the argument list '(cv::Mat, cv::Point **, int [1], int, cv::Scalar_<_Tp>)'
1> with
1> [
1> _Tp=double
1> ]

但通常你应该能够在函数需要 const 指针的地方传递非常量指针,那么为什么它在这里不起作用?

当然,一开始我使用了 Point *ptsp = pts;,我花了很长时间才弄清楚它需要是 const,这对我来说完全出乎意料。

最佳答案

这里有解释:http://www.parashift.com/c++-faq-lite/constptrptr-conversion.html

The reason the conversion from Foo** → Foo const** is dangerous is that it would let you silently and accidentally modify a const Foo object without a cast: class Foo { public: void modify(); // make some modification to the this object };

int main()
{
const Foo x;
Foo* p;
Foo const** q = &p; // q now points to p; this is (fortunately!) an error
*q = &x; // p now points to x
p->modify(); // Ouch: modifies a const Foo!!
...
}

If the q = &p line were legal, q would be pointing at p. The next line, *q = &x, changes p itself (since q is p) to point at x. That would be a bad thing, since we would have lost the const qualifier: p is a Foo but x is a const Foo. The p->modify() line exploits p's ability to modify its referent, which is the real problem, since we ended up modifying a const Foo.

关于c++ - 错误 C2665 : 'cv::fillPoly' : none of the 2 overloads could convert all the argument types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27226916/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com