메뉴 건너뛰기

창조도시 기록보관소

요즘 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;
}

그럼 좋은 답변이 있기를..