小编Chr*_*omz的帖子

隐式转换为 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_;
}
Run Code Online (Sandbox Code Playgroud)

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

std::vector<NumericType> row = obj.matrix_[0]; //obj.matrix_[0] is an object of type MatrixRow<NumericType>
Run Code Online (Sandbox Code Playgroud)

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

c++ templates vector c++11

3
推荐指数
1
解决办法
511
查看次数

标签 统计

c++ ×1

c++11 ×1

templates ×1

vector ×1