char[]을 클래스의 값으로 받아들이는 방법
조회수 1126회
#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:
char *str;
public:
MyString():str(""){}
MyString( 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(0) {
elem = new T[size];
for (int i = 0; i < size; i++) {
elem[i] = another.elem[i];
currentSize++;
}
}
int add(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];
}
}
bool operator = (const List& L){
int count = 0;
for (int i = 0; i < L.currentSize; i++) {
if (elem[i] == L.elem[i]) {
count++;
}
}
if (L.currentSize == count) {
return true;
}
else {
return false;
}
}
};
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");
}
자꾸 "abc"같은 string 부분에서 컴파일오류가 나는데 왜 그런지 모르겠습니다.
E0434 "MyString &" 형식(const 한정 형식 아님)의 참조를 "const char [4]" 형식의 값으로 초기화할 수 없습니다.
그리고
C2664 'int List::add(T &)': 인수 1을(를) 'const char [4]'에서 'T &'(으)로 변환할 수 없습니다.
라는데 무슨 말인지...ㅠㅠ
도와주시면 감사하겠습니다.
댓글 입력