[英]Implicit conversion to std::vector
I've trying to make an implicit conversion from a class that wraps a std::vector to std::vector, but I keep getting this error: 我正在尝试从将std :: vector包装到std :: vector的类进行隐式转换,但是我一直收到此错误:
error: conversion from 'const value_type {aka const MatrixRow}' to non-scalar type 'std::vector' 错误:从'const value_type {aka const MatrixRow}'转换为非标量类型'std :: vector'
My class MatrixRow is defined like this: 我的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_;
}
The error occurs when I try to make the following in other part of my code: 当我尝试在代码的其他部分进行以下操作时,将发生错误:
std::vector<NumericType> row = obj.matrix_[0]; //obj.matrix_[0] is an object of type MatrixRow<NumericType>
It is the first time that I'm using implicit conversions so probably I didn't understood how to use them properly. 这是我第一次使用隐式转换,因此可能我不了解如何正确使用它们。 What I'm doing wrong? 我做错了什么?
As your operator is explicit
, you should use different syntax: 由于运算符是explicit
,因此应使用不同的语法:
std::vector<NumericType> row(obj.matrix_[0]);
BTW, you may return const reference to avoid copy: 顺便说一句,您可以返回const引用以避免复制:
explicit operator const std::vector<NumericType>&() const {return row_;}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.