繁体   English   中英

重载运算符&lt;&lt;:无法绑定&#39;std :: basic_ostream <char> &#39;左值到&#39;std :: basic_ostream <char> &amp;&amp;&#39;

[英]Overloading operator<<: cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’

搜索此问题的标题使我有很多人引用相同的错误,但是在不同的情况下,不幸的是,所提供的答案是针对他们所处的情况的,我看不出他们如何能帮助我。

我正在尝试为模板类重载operator<< 以下是一个测试案例:

Vector.h:

#ifndef __INCL_VECTOR_H__
#define __INCL_VECTOR_H__

#include <array>

template < class T, unsigned int N >
class Vector
{
public:
    Vector();
    Vector( std::array< T, N > );

    template < class U, unsigned int M > friend Vector< U, M > operator+ ( const Vector< U, M >&, const Vector< U, M >& );

    template < class U, unsigned int M > friend std::ostream& operator<< ( std::ostream&, Vector< U, M >& );

    T& operator[] ( const unsigned int& );

protected:
    std::array< T, N > _values;
};

#include "Vector.hpp"

#endif

Vector.hpp:

#include "Vector.h"
#include <iostream>

template < class T, unsigned int N >
Vector< T, N >::Vector()
{
}

template < class T, unsigned int N >
Vector< T, N >::Vector( std::array< T, N > otherArray )
{
    _values = *( new std::array< T, N >( otherArray ) );
}

template < class U, unsigned int M >
Vector< U, M > operator+ ( const Vector< U, M > &lhVector, const Vector< U, M > &rhVector )
{
    Vector< U, M > sumVector;

    for( unsigned int i = 0; i < M; i++ )
        sumVector[i] = lhVector[i] + rhVector[i];

    return sumVector;
}

template < class U, unsigned int M >
std::ostream& operator<< ( std::ostream &out, Vector< U, M > &cVector )
{
    out << "< ";

    for( int i = M - 1; i >= 0; i-- )
    {
        out << cVector[i];
        if( i )
            out << ", ";
    }

    out << " >";

    return out;
}

template < class T, unsigned int N >
T& Vector< T, N >::operator[] ( const unsigned int &index )
{
    return _values[ index ];
}

vectorTest.cpp:

#include "Vector.h"

#include <iostream>
#include <array>

using namespace std;

int main( int argc, char* argv[] )
{
    Vector< int, 3 > u( array< int, 3 > {  1, 4,  2 } );
    Vector< int, 3 > v( array< int, 3 > { -2, 3, -1 } );

    cout << "u = " << u << endl;
    cout << "v = " << v << endl;
    cout << "u + v = " << u + v << endl;

    return 0;
}

导致错误的行是cout << "u + v = " << u + v << endl; ; 前两行按预期工作。

错误消息如下(编译为g++ -std=c++11 Vector.h vectorTest.cpp ):

vectorTest.cpp: In function ‘int main(int, char**)’:
vectorTest.cpp:15:31: error: cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’
In file included from /usr/include/c++/4.7/iostream:40:0,
                 from Vector.hpp:2,
                 from Vector.h:34:
/usr/include/c++/4.7/ostream:600:5: error:   initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = Vector<int, 3u>]’
In file included from Vector.h:34:0:
Vector.hpp: In instantiation of ‘Vector<U, M> operator+(const Vector<U, M>&, const Vector<U, M>&) [with U = int; unsigned int M = 3u]’:
vectorTest.cpp:15:31:   required from here
Vector.hpp:40:9: error: passing ‘const Vector<int, 3u>’ as ‘this’ argument of ‘T& Vector<T, N>::operator[](const unsigned int&) [with T = int; unsigned int N = 3u]’ discards qualifiers [-fpermissive]
Vector.hpp:40:9: error: passing ‘const Vector<int, 3u>’ as ‘this’ argument of ‘T& Vector<T, N>::operator[](const unsigned int&) [with T = int; unsigned int N = 3u]’ discards qualifiers [-fpermissive]

我无法理解这些错误消息告诉我什么。 我将不胜感激。

第一个问题:

为了使您的程序编译,只需使用一个指向const的左值引用作为operator << (在friend -declaration和该函数的定义中)的第二个参数:

template < class U, unsigned int M >
std::ostream& operator<< ( std::ostream &out, Vector< U, M > const& cVector )
//                                                           ^^^^^

程序无法编译的原因是, operator <<的重载接受对非const的左值引用作为其第二个参数,而对非const左值引用不能绑定到右值。

由于Vector两个实例之间的operator +的结果是一个临时的,而临时的是一个右值,因此编译器无法调用您的operator << ,因此无法解析该调用。

第二个问题:

解决上述问题后,您必须解决第二个问题: Vector类模板未提供const版本的operator [] ,因此您重写后的operator <<现在接受了对const向量的引用,无法访问向量的元素。

template < class T, unsigned int N >
class Vector
{
    // ...

    T& operator[] ( const unsigned int& );

    T const& operator[] ( const unsigned int& ) const; // <== ADD THIS!

    // ...
};

当然还有相应的定义:

template < class T, unsigned int N >
T const& Vector< T, N >::operator[] ( const unsigned int &index ) const
{
    return _values[ index ];
}

更改此:

std::ostream& operator<< ( std::ostream&, Vector< U, M >& );

对此:

std::ostream& operator<< ( std::ostream&, const Vector< U, M >& );
//                                        ^^^^^

编译器告诉您C ++不允许您将诸如u + v类的临时Vector绑定到非const Vector&

而且您无需修改Vector ,因此它应该const开头。

暂无
暂无

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

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