重载操作符<< ;: can not bind'std :: basic_ostream< char>'lvalue to'std :: basic_ostream< char>&& [英] Overloading operator<<: cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’
问题描述
搜索这个问题的标题给了我一些人引用相同的错误,但在不同的情况下,不幸的是,提供的答案具体到他们的情况,我不明白他们能如何帮助我。 / p>
我试图为模板类重载运算符<
。以下是一个测试用例:
Vector.h:
ifndef __INCL_VECTOR_H__
#define __INCL_VECTOR_H__
#include< array>
template<类T,无符号整数N>
class Vector
{
public:
Vector();
Vector(std :: array< T,N>);
template< class U,unsigned int M>朋友矢量& U,M>运算符+(const Vector< U,M&& const vector< U,M&
template< class U,unsigned int M> friend std :: ostream&运算符<< (std :: ostream& amp; Vector< U,M>&
T& operator [](const unsigned int&);
protected:
std :: array< T,N> _values;
};
#includeVector.hpp
#endif
$ b b
Vector.hpp:
#includeVector.h
#include< iostream>
template<类T,无符号整数N>
Vector< T,N> :: Vector()
{
}
template<类T,无符号整数N>
Vector< T,N> :: Vector(std :: array< T,N> otherArray)
{
_values = *(new std :: array&
}
template< class U,unsigned int M>
Vector< U,M>运算符+(const Vector< U,M>& lhVector,const Vector< U,M& U,M> sumVector;
for(unsigned int i = 0; i sumVector [i] = lhVector [i] + rhVector [i]
return sumVector;
}
template< class U,unsigned int M>
std :: ostream&运算符<< (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<类T,无符号整数N>
T&另外, T,N> :: operator [](const unsigned int& index)
{
return _values [index]
}
vectorTest.cpp:
#includeVector.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:在函数'int main )':
vectorTest.cpp:15:31:error:can not bind'std :: basic_ostream< char>'lvalue to'std :: basic_ostream< char>&来自/usr/include/c++/4.7/iostream:40:0,来自Vector.hpp:2的
,来自Vector.h:34的
:34:
/usr/include/c++/4.7 / ostream:600:5:error:初始化'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>]'
在Vector.h:34:0中包括的文件中:
Vector.hpp:在'Vector< U,M&运算符+(const Vector< U,M&& const Vector< U,M& unsigned int M = 3u]':
vectorTest.cpp:15:31:这里需要
Vector.hpp:40:9:error:传递const Vector< int,3u& 'T& Vector< T,N> :: operator [](const unsigned int&)[with T = int; unsigned int N = 3u]'丢弃限定符[-fpermissive]
Vector.hpp:40:9:error:传递'const Vector< int,3u& Vector< T,N> :: operator [](const unsigned int&)[with T = int; unsigned int N = 3u]'丢弃限定符[-fpermissive]
我无法理解这些错误消息告诉我。
第一个问题:
要使程序编译,只需使用 const
的左值引用作为运算符的第二个参数<
(在 friend
-declaration和该函数的定义中):
template< class U,unsigned int M>
std :: ostream&运算符<< (std :: ostream& out,Vector< U,M> const& cVector)
// ^^^^^
你的程序不能编译的原因是你的重载 operator<<
code> const 作为其第二个参数,并且对非 - const
的lvalue引用不能绑定到rvalues。
由于运算符+
在 Vector $ c $的两个实例之间的结果c>是临时的,而临时的是一个右值,编译器不能调用你的
运算符<<
,因此无法解析该调用。第二个问题:
修正上述问题后,您必须解决第二个问题:您的向量
类模板不提供运算符的
,因此您重写的 const
] 运算符
const
vector,将无法访问该向量的元素。
template<类T,无符号整数N>
class Vector
{
// ...
T& operator [](const unsigned int&);
T const& operator [](const unsigned int&)const; //< == ADD THIS!
// ...
};
当然还有相应的定义:
template<类T,无符号整数N>
T const&另外, T,N> :: operator [](const unsigned int& index)const
{
return _values [index]
}
Searching for the title of this question gives me a number of people quoting the same error, but under different circumstances, and unfortunately the answers there provided are specific to their situation, and I do not see how they can help me.
I am trying to overload operator<<
for a template class. Following is a test case:
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;
}
The line which causes the error is cout << "u + v = " << u + v << endl;
; the previous two lines work as expected.
The error message is as follows (compiling as 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]
I'm unable to understand what these error messages are telling me. I'd appreciate any assistance.
FIRST PROBLEM:
To make your program compile, just use an lvalue reference to const
as the second parameter of your operator <<
(both in the friend
-declaration and in the definition of that function):
template < class U, unsigned int M >
std::ostream& operator<< ( std::ostream &out, Vector< U, M > const& cVector )
// ^^^^^
The reason why your program won't compile is that your overload of operator <<
accepts an lvalue reference to non-const
as its second argument, and lvalue references to non-const
cannot bind to rvalues.
Since the result of operator +
between two instances of Vector
is a temporary, and a temporary is an rvalue, the compiler can't invoke your operator <<
, and is therefore unable to resolve the call.
SECOND PROBLEM:
Once you fixed the above issue, you'll have to solve a second one: your Vector
class template does not provide a const
version of operator []
, so your rewritten operator <<
, which now accepts a reference to a const
vector, won't be able to access the vector's elements.
template < class T, unsigned int N >
class Vector
{
// ...
T& operator[] ( const unsigned int& );
T const& operator[] ( const unsigned int& ) const; // <== ADD THIS!
// ...
};
And of course the corresponding definition:
template < class T, unsigned int N >
T const& Vector< T, N >::operator[] ( const unsigned int &index ) const
{
return _values[ index ];
}
这篇关于重载操作符<< ;: can not bind'std :: basic_ostream< char>'lvalue to'std :: basic_ostream< char>&&的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!