简体   繁体   English

来自&#39;const std :: vector的无效转换 <cv::Mat> *&#39;到&#39;std :: vector <cv::Mat> *&#39;在c ++ 14中

[英]Invalid conversion from ‘const std::vector<cv::Mat>*’ to ‘std::vector<cv::Mat>*’ in c++14

When running the following code 运行以下代码时

RM_Analyzer.h: RM_Analyzer.h:

struct FFeatureCalculationImages
{
    std::vector<cv::Mat>* cf_mat;
    std::vector<cv::Mat>* front_mat;
    std::vector<cv::Mat>* back_mat;
};

class RM_Analyzer
{
private: 
DLL_Mat<std::vector<cv::Mat> > sliding_window_frames;
void setImagesForExtendedFeatureCalculation(FFeatureCalculationImages &imgs) const;
}

RM_Analyzer.cpp RM_Analyzer.cpp

void RM_Analyzer::setImagesForExtendedFeatureCalculation(FFeatureCalculationImages &imgs) const
{
    imgs.cf_mat=sliding_window_frames[sliding_window_frames.size()-1-k];
    imgs.front_mat=sliding_window_frames.getFrontMat();
    imgs.back_mat=sliding_window_frames.getBackMat();
}

DLL_Mat.h DLL_Mat.h

template<class T>
class DLLNode
{
public:
    DLLNode<T>* next;
    DLLNode<T>* previous;
    T im;
};


template<class T>
class DLL_Mat
{
private:
    unsigned count;
    DLLNode<T>* front;
    DLLNode<T>* back;

public:
    T* getFrontMat() const;
    T* getBackMat() const;
    unsigned size() const;
    T* operator[](const std::size_t idx);
    const T* operator[](const std::size_t idx) const;
};

template <class T>
inline unsigned DLL_Mat<T>::size() const
{
    return count;
}

template <class T>
T* DLL_Mat<T>::operator[](const std::size_t idx)
{
    if (idx==0)
        return getFrontMat();
    else if (idx==(count-1))
        return getBackMat();
    else if (idx>=size())
        return nullptr;
    else
    {
        std::size_t i=0;
        for (DLLNode<T> *iter=front;i<count;i++,iter=iter->next)
        {
            if (i==idx)
                return &(iter->im);
        }
    }

    return nullptr;
}

template <class T>
const T* DLL_Mat<T>::operator[](const std::size_t idx) const
{
    if (idx==0)
        return getFrontMat();
    else if (idx==(count-1))
        return getBackMat();
    else if (idx>=size())
        return nullptr;
    else
    {
        std::size_t i=0;
        for (DLLNode<T> *iter=front;i<count;i++,iter=iter->next)
        {
            if (i==idx)
                return &(iter->im);
        }
    }

    return nullptr;
}

I have included the operator [] source code for convenience, although the function declarations in DLL_Mat.h and the RM_Analyzer source code are probably enough to find the culprit of the problem. 为了方便起见,我包含了operator []源代码,尽管DLL_Mat.h和RM_Analyzer源代码中的函数声明可能足以找到问题的根源。

The compiler seems to be calling a const version of the operator [], which in turn causes a conversion error. 编译器似乎正在调用运算符[]的const版本,这反过来会导致转换错误。 The error is (Line 3 in RM_Analyzer.cpp): 错误是(RM_Analyzer.cpp中的第3行):

error: invalid conversion from ‘const std::vector<cv::Mat>*’ to ‘std::vector<cv::Mat>*’ [-fpermissive]
     imgs.cf_mat=sliding_window_frames[sliding_window_frames.size()-1-k];

My explanation: 我的解释:
1) I seem to have made a logical error somewhere since the function setImagesForExtendedFeatureCalculation should obviously be const since I do not change any internal variable in that method 1)我似乎在某个地方犯了逻辑错误,因为函数setImagesForExtendedFeatureCalculation应该显然是const的,因为我没有在该方法中更改任何内部变量
2) The compiler cannot call non-const operator [] since I declared the method setImagesForExtendedFeatureCalculation as const so sliding_window_frames is treated as const as well 2)由于我将setImagesForExtendedFeatureCalculation方法声明为const,因此编译器无法调用非const运算符[],因此slide_window_frames也被视为const
3) I cannot call the const version of the operator [] since the argument (FFeatureCalculationImages) parameters are not declared const (specifically cf_mat, front_mat, back_mat). 3)由于参数(FFeatureCalculationImages)参数未声明为const(特别是cf_mat,front_mat,back_mat),因此无法调用运算符[]的const版本。

This seems to put me in a peculiar position where neither of the operator [] methods is a good fit, yet declaring a T* operator[] (const size_t idx) const; 这似乎使我处于一个特殊的位置,在这个位置上,两个operator []方法都不适合,但是却声明了一个T* operator[] (const size_t idx) const; is not a possible overload given the two existing methods in DLL_Mat.h. 给定DLL_Mat.h中的两个现有方法,这不可能重载。

Any advice on how to evade the invalid conversion error without throwing away the const function declarations? 关于如何避免无效转换错误而不丢弃const函数声明的任何建议? (since I think that would be a bit sloppy to not declare a function as const just because it is convenient for me) (因为我认为不方便将函数声明为const有点草率)

In sliding_window_frames[sliding_window_frames.size()-1-k] , the compiler does call the const version of operator[] . sliding_window_frames[sliding_window_frames.size()-1-k] ,编译器会调用operator[]的const版本。 Which returns a pointer to const std::vector<cv::Mat> . 它返回一个指向const std::vector<cv::Mat>的指针。 But then you are trying to assign that pointer to imgs.cf_mat , which is a pointer to non-const std::vector<cv::Mat> . 但是,然后您尝试将该指针分配给imgs.cf_mat ,该指针是指向非常量 std::vector<cv::Mat>的指针。 This violates const correctness, and so the compiler rightfully complains. 这违反了const正确性,因此编译器理应抱怨。

You have a function that can be called on a const instance of the object, trying to hand out mutable pointers to internals of that object. 您有一个可以在对象的const实例上调用的函数,试图将可变的指针分发给该对象的内部。 Either that function shouldn't have been declared const , or it should be handing out const pointers, or the internals it exposes should be marked mutable . 该函数不应该被声明为const ,或者应该分发const指针,或者其公开的内部应该标记为mutable

Both operator[] methods have same signature (return type is not part of signature; edit: they have different signature because of constness of this pointer) therefore you can't select which method you want to call. 两种operator []方法都具有相同的签名(返回类型不是签名的一部分;编辑:由于此指针的恒定性,它们具有不同的签名),因此您无法选择要调用的方法。 Having two identical methods doing same thing but one is returning const value isn't right solution: You can always cast to const but you cant otherway, therefore you should remove method returning const value. 有两个相同的方法做同样的事情,但一个返回const值是不正确的解决方案:您始终可以强制转换为const,但不能这样做,因此应删除返回const值的方法。

Edit: Field of const object have also const modifier, so operator[] with const this pointer are called (which return const pointer). 编辑:const对象的字段也具有const修饰符,因此调用具有该指针的operator [](返回const指针)。

PS Operator[] takes const argument in header but non-const argument in definition; PS Operator []在标头中使用const参数,但在定义中使用非const参数; size() should return std::size_t. size()应该返回std :: size_t。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM