重载运算符<<:无法将"std::<char>basic_ostream"左值绑定到"std::basic_ostream<char>&&"
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 ];
}
向量测试.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]
我无法理解这些错误消息告诉我什么。我将不胜感激任何帮助。
第一个问题:
要使程序编译,只需使用 lvalue 引用 const
作为operator <<
的第二个参数(在 friend
-声明和该函数的定义中(:
template < class U, unsigned int M >
std::ostream& operator<< ( std::ostream &out, Vector< U, M > const& cVector )
// ^^^^^
程序无法编译的原因是operator <<
重载接受对非const
的左值引用作为其第二个参数,而对非const
的左值引用不能绑定到右值。
由于两个Vector
实例之间的operator +
结果是临时的,而临时是右值,因此编译器无法调用您的operator <<
,因此无法解析调用。
第二个问题:
解决上述问题后,您必须解决第二个问题:您的Vector
类模板不提供operator []
的const
版本,因此您重写的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++不会允许您将临时Vector
(如 u + v
(绑定到非常量Vector&
。
而且你不修改那个Vector
,所以它应该一开始就const
。
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- ///<评论></评论>在Visual Studio中