이 질문의 제목을 검색하면 동일한 오류를 인용하는 사람들이 많지만, 불행하게도 거기에 제공된 답변은 상황에 따라 다르며 어떻게 도와 줄 수 있는지 알지 못합니다.연산자 오버로딩 << : std :: basic_ostream <char>을 '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;
; 이전 두 줄은 예상대로 작동합니다.
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]
나는 이러한 오류 메시지가 나에게 말하고있는 것을 이해 드릴 수 없습니다 :
오류 메시지는 다음과 같이 (g++ -std=c++11 Vector.h vectorTest.cpp
로 컴파일)입니다. 어떤 도움을 주시면 감사하겠습니다.
연산자 '[]'비 'const'이지만 시도는''const' Vector'에 사용하게되고있는 불법입니다. – hmjd