템플릿 클래스끼리 비교할 방법이 없을까요?
조회수 454회
find랑 remove함수에서 인자로 받은 매개변수랑 템플릿 클래스를 비교하려고 하니 if (elem[i] == anElem) 부분에서 C2678 이항 '==': 왼쪽 피연산자로 'T' 형식을 사용하는 연산자가 없거나 허용되는 변환이 없습니다. 라는 오류가 나네요 ㅠㅠ 왜 그런가요? 비교가 되어야 뭘 하고 말고 하는데 ㅠㅠ 해결 방법은 뭘까요?
#include <iostream>
#include <string>
using namespace std;
class Complex {
private:
float real, imaginary;
public:
Complex(float _real, float _imaginary) : real(_real), imaginary(_imaginary) {};
};
class MyString {
private:
const char *str;
public:
MyString():str(""){}
MyString( const char *_str) : str(_str) {}
};
template <class T, int size>
class List {
T *elem;
int size;
int currentSize;
public:
List() : currentSize(0) {}
List(int _size) : size(_size), currentSize(0) { elem = new T[_size]; }
List(const List& another) : size(another.size), currentSize(another.currentSize) {
elem = new T[size];
for (int i = 0; i < currentSize; i++) {
elem[i] = another.elem[i];
}
}
int add(const T &anElem) {
elem[currentSize] = anElem;
return currentSize++;
}
void find(const T &anElem) {
for (int i = 0; i < currentSize; i++) {
if (elem[i] == anElem) {
cout << "exist" << endl;
}
else {
cout << "not exist" << endl;
}
}
}
void remove(const T &anElem) {
for (int i = 0; i < currentSize; i++) {
if (elem[i] == anElem) {
for (int j = i; j < (currentSize - 1); j++) {
elem[j] = elem[j + 1];
}
}
else {
cout << "not exist" << endl;
}
}
}
void remove(const int location) {
for (int j = location; j < (currentSize - 1); j++) {
elem[j] = elem[j + 1];
}
}
List operator = (const List& L){
for (int i = 0; i < currentSize; i++) {
L.elem[i] = elem[i];
}
}
};
int main() {
List<Complex, 100> cList;
List<MyString, 200> sList;
int i1 = cList.add(Complex(0, 0));
cList.add(Complex(1, 1));
int i2 = sList.add("abc");
sList.add("def");
cList.find(Complex(1, 0));
sList.find("def");
cList.remove(i1);
sList.remove("abc");
List<MyString, 200> s2List(sList);
List<MyString, 200> s3List;
s3List.add("123");
s3List = s2List;
s3List.remove("def");
}
댓글 입력