繁体   English   中英

隐式转换为std :: vector

[英]Implicit conversion to std::vector

我正在尝试从将std :: vector包装到std :: vector的类进行隐式转换,但是我一直收到此错误:

错误:从'const value_type {aka const MatrixRow}'转换为非标量类型'std :: vector'

我的MatrixRow类的定义如下:

template <typename NumericType>
class MatrixRow{
public:

    // a lot of other methods here
    //....
    //......
    explicit operator std::vector<NumericType>() {return row_;}
    //...
    //...

private:
    std::vector<NumericType> row_;
}

当我尝试在代码的其他部分进行以下操作时,将发生错误:

std::vector<NumericType> row = obj.matrix_[0]; //obj.matrix_[0] is an object of type MatrixRow<NumericType>

这是我第一次使用隐式转换,因此可能我不了解如何正确使用它们。 我做错了什么?

由于运算符是explicit ,因此应使用不同的语法:

std::vector<NumericType> row(obj.matrix_[0]);

顺便说一句,您可以返回const引用以避免复制:

explicit operator const std::vector<NumericType>&() const {return row_;}

暂无
暂无

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

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