언어 C++ 질문입니다. 부디 고수들님들 답변 부탁드립니다..
2006.05.04 20:56
요즘 Operating Overloading과 Template을 배우는중입니다. 단순한 문제인것 같기도 하지만은, 도저히 어디서 잘못꿩S쩝……찾을수가 없어서 아래와 같이 코드를 올려놓읍니다.
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;
class Point {
friend int operator+(Point *pt1, Point *pt2);
friend ostream operator<<(ostream& ostr, Point& pt);
private:
int x, y;
public:
Point() {
}
Point(int newX, int newY) {
x = newX;
y = newY;
}
};
int operator+(Point *pt1, Point *pt2) {
return pt1.x + pt2.x;
}
ostream operator<<(ostream& ostr, Point& pt) {
ostr << "(" << pt.x << "," << pt.y << ")";
return ostr;
}
template<typename Type>
class Node {
public:
Node() {
}
Type data;
Node *next;
};
template<typename ListType>
class List {
private:
Node<ListType> *front;
public:
List() {
front = 0;
}
void insert(ListType newInt) {
Node<ListType> *newNode = new Node<ListType>;
newNode->data = newInt;
newNode->next = front;
front = newNode;
}
void displayAll() {
Node<ListType> *curr = front;
while (curr != 0) {
cout << curr->data << " " << endl;
curr = curr->next;
}
}
};
int main() {
List<Point> myList;
Point pt3(3,3), pt4(4,4), pt5(5,5);
cout << pt3 + pt4 << endl;
myList.insert(pt3);
myList.insert(pt4);
myList.insert(pt5);
myList.displayAll();
return 0;
}
그럼 좋은 답변이 있기를..
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;
class Point {
friend int operator+(Point *pt1, Point *pt2);
friend ostream operator<<(ostream& ostr, Point& pt);
private:
int x, y;
public:
Point() {
}
Point(int newX, int newY) {
x = newX;
y = newY;
}
};
int operator+(Point *pt1, Point *pt2) {
return pt1.x + pt2.x;
}
ostream operator<<(ostream& ostr, Point& pt) {
ostr << "(" << pt.x << "," << pt.y << ")";
return ostr;
}
template<typename Type>
class Node {
public:
Node() {
}
Type data;
Node *next;
};
template<typename ListType>
class List {
private:
Node<ListType> *front;
public:
List() {
front = 0;
}
void insert(ListType newInt) {
Node<ListType> *newNode = new Node<ListType>;
newNode->data = newInt;
newNode->next = front;
front = newNode;
}
void displayAll() {
Node<ListType> *curr = front;
while (curr != 0) {
cout << curr->data << " " << endl;
curr = curr->next;
}
}
};
int main() {
List<Point> myList;
Point pt3(3,3), pt4(4,4), pt5(5,5);
cout << pt3 + pt4 << endl;
myList.insert(pt3);
myList.insert(pt4);
myList.insert(pt5);
myList.displayAll();
return 0;
}
그럼 좋은 답변이 있기를..
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
187 | rpg xp 캐릭터 칩 수정 하는 방법 | 호야군☆ | 2006.05.31 | 295 |
186 | rpg xp 캐릭터 칩 수정 하는 방법 | 향단 | 2006.05.29 | 346 |
185 | 클리어 세이브가 안되는군요 [1] | 나루(호)토? | 2006.05.26 | 73 |
184 | c언어 시작 하는 사람인데요... | TitanBlow | 2006.05.20 | 4 |
183 | c언어 시작 하는 사람인데요... [1] | 기다림의끝 | 2006.05.19 | 89 |
182 | \r 이 뭔가요?(복구라고 나와있는데 뭔소린지...) [5] | leecyid | 2006.05.10 | 102 |
181 | 대략 | leecyid | 2006.05.10 | 65 |
» | C++ 질문입니다. 부디 고수들님들 답변 부탁드립니다.. [1] | 무라도배라 | 2006.05.04 | 89 |
179 | 글시가 안보입니다! | 빛과 어둠 | 2006.05.04 | 4 |
178 | RPGXP 아이템창 스크립트에 관해서.. [1] | 머리아퍼~ | 2006.05.02 | 294 |
177 | 그림잘그리는 방법 [4] | 코로네 | 2006.04.23 | 77 |
176 | 닝구님 꼭 봐주세요 전투 애니 2 에서 입니다 [1] | 관창이 | 2006.04.20 | 95 |
175 | C++처음 시작한 사람입니다,... [3] | jong1457 | 2006.04.19 | 145 |
174 | rpg2000 어디서 다운 받나요?? ㅜ | 겜만들고싶당 | 2006.04.15 | 250 |
173 | 음.. VB에서 | 카디 | 2006.04.15 | 62 |
172 | rpg2000 어디서 다운 받나요?? ㅜ [1] | 겜만들고싶당 | 2006.04.15 | 315 |
171 | c언어에서 궁금한게 있어서요 [9] | 랑유 | 2006.04.05 | 97 |
170 | 알피지만들때요 [1] | RPG만드는중 | 2006.04.02 | 39 |
169 | rpgxp 스샷 어케 올리죠? | 노란상자 | 2006.03.15 | 375 |
168 | 캐릭터만드는데... | Ress | 2006.03.12 | 8 |
이미 이유를 알아버리셨을듯;;
첫째로 왼편 피연산자가 +의 오버로딩 함수를 가지고 있어야 하는데 +오버로딩 은 전역으로 선언이 되어있었다는게 문제고..
(point객체의 멤버함수로서 넣어주세요)
둘째로는 연산자 오버로딩 호출 수식에서는 값에 의한 전달을 하고 있지만 오버로딩 함수가 이를 포인터로 받음으로서 문제가 생긴 것입니다.
(포인터가 아닌 레퍼런스를 사용해주세요. 대개 연산자 오버로딩 함수에서는 인수를 레퍼런스로 받습니다.
일반적으로 a+b 이런식으로 사용하지 &a+&b이런식으로 사용하지 않지요)
링크드 리스트는 논리상 하자가 없는것 같네요.|+rp2+|3517|+rp3+|qna