저는 여기에 새로 왔으며 (실제로 이것은 내 첫 번째 질문입니다) 데이터 구조 클래스에 대한 작업에 도움이되는 프로그램을 찾고 있습니다.연산자 템플릿을 사용하여 오버로드 지정 - C++
=, + 및 < <의 연산자 오버로드에 관한 것입니다. 지금
, 내가 선언과 정의를 포함, 기능 템플릿이있는 .cpp 파일을 사용하고 내 다른 .cpp 파일에서#include <iostream>
using namespace std;
template <class t_type>
class TLIST {
public:
TLIST();
TLIST(const TLIST<t_type> &);
bool IsEmpty();
bool IsFull();
int Search(t_type);
TLIST<t_type> & TLIST<t_type>::operator+(const t_type &rhs);
void Remove(const t_type);
// TLIST<t_type> & operator=(const TLIST<t_type> &);
// friend operator<<(ostream &, TLIST<t_type> &);
void Double_Size();
/*other functions you may want to implement*/
private:
t_type *DB;
int count;
int capacity;
/*additonal state variables you may wish add*/
};
, 나는 이전 파일을 포함하고이 다음을 코드 :
TLIST<char> Char_List,TempChar1, TempChar2;
Char_List + 'W' + 'E' + 'L' + 'C' + 'O' + 'M' + 'E'; // chaining
이제 "+"연산자를 오버로드하려고합니다.
template <class t_type>
TLIST<t_type> & TLIST<t_type>::operator+(const t_type &rhs)
{
TLIST <t_type> lhs;
lhs += rhs;
return *this;
}
나는이 오류가 계속 :
Error 1 error C2676: binary '+=' : 'TLIST' does not define this operator or a conversion to a type acceptable to the predefined operator c:\users\negri\dropbox\visual studio\projects\assignment 1 (tlist2)\assignment 1 (tlist2)\tlist.cpp 53 1 Assignment 1 (TLIST2)을 내 선언이 맞다면 내가 교수에게 물었다 그는 그것이하지만 정의 안에 뭔가를 누락 나에게 말했다 어디 지점에서 오전
나는 이해할 수있다. 왜냐하면 내가 lhs에 chars의 템플릿을 가지고 있고 rhs에 char 만 갖고 있기 때문이다.
어떻게 해결해야합니까?
감사합니다.
첫째,'operator +'는'class TLIST'에 대한 friend 함수 여야합니다. 둘째,'operator + ='가 정의되어야한다. – YaleCheung