简体   繁体   English

从T **重构到vec时返回对临时错误的引用 <vec<T> &gt;

[英]Returning reference to temporary error when remodeling from T ** to vec<vec<T>>

I encountered an error that happend in my proxy class for operator [] . 我在operator []代理类中遇到了一个错误。 It was checking if index is in range, and worked fine when I implemented my class template with T** values. 它正在检查索引是否在范围内,并且当我用T** values.实现我的类模板时工作正常T** values.

But I felt like change whole implementation to std::vector<std::vector<T>> . 但是我感觉就像将整个实现更改为std::vector<std::vector<T>> Everything is fine, expect said operator[] . 一切都很好,期望operator[]

Matrix class operator(s) 矩阵类运算符

//***************************************************************************
template <typename T>
X_Proxy<T> Matrix<T>::operator [](const size_t& j)
{
    if(j >= y)
        ERROR_MSG(Y_OUT_RANGE);
    return X_Proxy<T>(inner[j], x);
}

//***************************************************************************
template <typename T>
const X_Proxy<T> Matrix<T>::operator [](const size_t& j) const
{
    if(j >= y)
        ERROR_MSG(Y_OUT_RANGE);
    return X_Proxy<T>(inner[j], x);
}
//***************************************************************************

Proxy class template definition: 代理类模板定义:

template <typename T>
struct X_Proxy
{
    X_Proxy(std::vector<T> PTR, const size_t X) : x_ptr(PTR), x(X) {}
    T& operator [] (size_t pos);
    const T& operator [] (size_t pos) const;
    std::vector<T>& x_ptr;
    const size_t& x;
};

Proxy class operator(s): 代理类运算符:

//***************************************************************************
template <typename T>
T& X_Proxy<T>::operator [] (size_t pos)
{
    if (pos > x-1)
        Matrix<T>::ERROR_MSG(Matrix<T>::X_OUT_RANGE);
    return x_ptr[pos];
}
//***************************************************************************
template <typename T>
const T& X_Proxy<T>::operator [] (size_t pos) const
{
    if (pos > x-1)
        Matrix<T>::ERROR_MSG(Matrix<T>::X_OUT_RANGE);
    return x_ptr[pos];  // <--- the error line
}

//***************************************************************************

Matrix error function: 矩阵误差函数:

template <typename T>
void Matrix<T>::ERROR_MSG(const int& MSG)
{
    std::cerr << info[MSG] << std::endl;
    exit(MSG);
}

Compilation error: 编译错误:

..\matrix.h:47: error: returning reference to temporary [-Wreturn-local-addr]
         return x_ptr[pos];
                         ^

What could go wrong with our lovely template library? 我们可爱的模板库可能会出什么问题?

Your X_Proxy constructor is storing a reference to a temporary: 您的X_Proxy构造函数正在存储对临时文件的引用:

X_Proxy(std::vector<T> PTR, const size_t X) : x_ptr(PTR), x(X) {}

Here, PTR is a local temporary, and x_ptr is an lvalue reference: 在这里, PTR是本地临时x_ptr ,而x_ptr是左值引用:

std::vector<T>& x_ptr;

This isn't standard C++, so it shouldn't even compile. 这不是标准的C ++,因此甚至不应该编译。 But your compiler allows it, leaving you with a dangling reference. 但是您的编译器允许这样做,给您留下了麻烦的参考。

Perhaps you want to store a reference to a valid vector: 也许您想存储对有效向量的引用:

X_Proxy(std::vector<T>& PTR, const size_t X) : x_ptr(PTR), x(X) {}
                      ^

This will work as long as the vector referred to by PTR outlives the X_Proxy instance. 只要由PTR引用的向量超过X_Proxy实例, X_ProxyX_Proxy

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

相关问题 将本地引用返回到本地变量时,编译器为什么不警告“返回本地变量或临时地址”? - Why doesn't the compiler warn “returning address of local variable or temporary” when returning a local reference to a local variable? 用于将向量列表从 vec[0] 返回到 vec[n-1] 的 C 预处理器宏 - C preprocessor macro for returning a vector list from vec[0] to vec[n-1] C ++“警告:返回对临时的引用” - 但事实并非如此 - C++ “warning: returning reference to temporary ” - But it isn't 返回对临时对象的引用时出错 - Error with returning reference to temporary object 错误 - “初始化”:无法从“glm::vec2”转换为“glm::vec3” - Error - 'initializing': cannot convert from 'glm::vec2' to 'glm::vec3' 为什么我不能创建包含vec3对象的联合? - Why can't I make a union containing a vec3 object? 转换std :: vector <const Type*> 到const std :: vector <T, A> &Vec - Convert std::vector<const Type*> to const std::vector<T, A> &Vec 在 for 循环中使用 vec.size() 时出现运行时错误 - Runtime Error when using vec.size() in the for loop 使用glm :: vec &lt;3时出错,double&gt; - Error using glm::vec< 3 , double > 当我使用 size(vec) 而不是 vec.size() 来查找向量的大小时出现错误。 如何解决这个问题? - I'm getting an error when I use size(vec) to find size of a vector instead of vec.size(). How to fix this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM