C++ 질문입니다. 부디 고수들님들 답변 부탁드립니다..

by 무라도배라 posted May 12, 2006
?

Shortcut

Prev前へ 書き込み

Next次へ 書き込み

ESC閉じる

Larger Font Smaller Font 上へ 下へ Go comment 印刷
요즘 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;
}

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

Articles

1 2 3 4 5 6 7 8 9 10