Announcement

Collapse
No announcement yet.

[C++] Help về cách sử dụng khuôn lớp template!!!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • [C++] Help về cách sử dụng khuôn lớp template!!!

    Mọi người giúp e sửa lại chổ lỗi trong đoạn code này với
    PHP Code:
    template <class T> class point
    {
    public:
        
    T x;
        
    T y;
    public:
        
    point(T abs=0,T ord=0)
        {
            
    x=abs;
            
    y=ord;
        }
        
    friend ostream  &operator<<(ostream &o,point &);
        
    void display();
    };
    ostream &operator<<(ostream &o,point &m)  // lỗi
    {
        
    o<<m.x<<","<<m.y<<endl;
        return 
    o;
    }
    template <class Tvoid point<T>::display()
    {
        
    cout<<x<<","<<y<<endl;

    Last edited by 11520036; 03-10-2012, 17:00.
    ------"Some Will, Some Won't, So What? Someone's Waiting!"------

  • #2
    Code:
    #include <conio.h>
    #include <stdio.h>
    #include <iostream>
    
    using namespace std;
    
    template <class T> 
    class point
    {
    public:
        T x;
        T y;
    public:
        [COLOR="#FF0000"]point(T abs=0,T ord=0)[/COLOR]
        {
            x=abs;
            y=ord;
        }		
        [COLOR="#FF0000"]template<class U> friend ostream& operator<<(ostream &o, point<U> &m);[/COLOR]
        void display();
    };
    
    [COLOR="#FF0000"]template <class U> 
    ostream& operator<<(ostream &o,point<U> &m) [/COLOR]
    {
        o<<(int)m.x<<","<<(int)m.y;
        return o;
    }
    template <class T>
    void point<T>::display()
    {
        cout<<x<<","<<y<<endl;
    }  
    
    int main()
    {
    	point<int> p(3,4);
    	cout << p;
    	getch();
    }
    Last edited by 11520185; 03-10-2012, 22:07.
    Blog: http://khuongntrd.blogspot.com/ Email: khuongntrd@gmail.com
    Facebook: https://www.facebook.com/dkuns2

    Comment

    LHQC

    Collapse
    Working...
    X