引用类型的初始化无效

invalid initialization of reference type?

本文关键字:无效 初始化 引用类型      更新时间:2023-10-16

发生了什么事?以下是它引用的函数。我正试图让它作为一个复制构造函数工作

template <class T>
const queue<Base>& queue<T>::operator=(const queue<Base> &q){
// Doesn't need to copy if they are the same object
if (this != &q){
    delete [] data;
    length = q.length;
    capacity = q.capacity;
    front = q.front;
    data = new T[capacity];
    for (int i = 0; i < capacity; i++){
        data[i] = q.data[i];
    }
}
return this;
}

这是您的错误

return this;

this是一个指针。您的operator =被声明为返回引用。指针无法转换为引用。这就是错误消息告诉您的内容。

相关文章: