Từ 1 tới 2 trên tổng số 2 kết quả

Đề tài: sửa lỗi định nghĩa toán tử << và>> trong class hộ mình với

  1. #1
    Ngày gia nhập
    05 2011
    Bài viết
    1

    Mặc định sửa lỗi định nghĩa toán tử << và>> trong class hộ mình với

    xem giúp mình cái lỗi trong class này sửa thế nào:error C2248: 'heso' : cannot access private member declared in class 'donthuc'
    đây là code:

    #include<iostream>
    using namespace std;
    #include<stdio.h>
    #include<math.h>
    class donthuc
    {

    private:
    float somu;
    float heso;
    public:
    donthuc();
    donthuc(float a, float b);
    donthuc(const donthuc &b);
    ~donthuc();

    void nhapdt();
    void xuatdt();

    int layhs();
    int laysm();
    void gan_hs(int a);
    void gan_sm(int b);
    int giatridt(int x);
    void daohamdt();
    void nguyenhamdt();

    donthuc congdt(const donthuc &b);
    donthuc trudt(const donthuc &b);
    donthuc nhandt(const donthuc &b);
    donthuc chiadt(const donthuc &b);
    int sosanhdt(const donthuc &b);

    donthuc operator+(const donthuc &a);
    donthuc operator-(const donthuc &a);
    donthuc operator*(const donthuc &a);
    donthuc operator/(const donthuc &a);
    donthuc operator=(const donthuc &a);
    donthuc operator+=(const donthuc &a);
    donthuc operator-=(const donthuc &a);

    int operator>(const donthuc &a);
    int operator<(const donthuc &a);
    int operator>=(const donthuc &a);
    int operator<=(const donthuc &a);
    int operator==(const donthuc &a);
    int operator!=(const donthuc &a);

    donthuc operator ++();
    donthuc operator --();
    friend istream& operator>>(istream &is,donthuc &a);
    friend ostream& operator<<(ostream &os,const donthuc &a);
    donthuc operator !();
    donthuc operator ~();
    };
    donthuc::donthuc()
    {
    heso = 0;
    somu = 0;
    }
    donthuc::donthuc(float a, float b)
    {
    heso = a;
    somu = b;
    }
    donthuc::donthuc(const donthuc &b)
    {
    heso = b.heso;
    somu = b.somu;
    }
    donthuc::~donthuc(){}
    void donthuc::nhapdt()
    {
    cout<<"nhap he so va so mu: "<<endl;
    cin>> heso >>somu ;

    }
    void donthuc::xuatdt()
    {
    cout<<heso<<"x^"<<somu;
    }
    int donthuc::layhs()
    {
    return heso;
    }
    int donthuc::laysm()
    {
    return somu;
    }
    void donthuc::gan_hs(int a)
    {
    heso = a;
    }
    void donthuc::gan_sm(int b)
    {
    somu = b;
    }
    int donthuc::giatridt(int x)
    {
    return heso*pow(x,somu);
    }
    void donthuc::daohamdt()
    {
    heso = heso * somu;
    somu = somu -1;
    }
    void donthuc::nguyenhamdt()
    {
    somu = somu + 1;
    heso = heso/somu;
    }
    donthuc donthuc::congdt(const donthuc &b)
    {
    heso = heso + b.heso;
    return *this;
    }
    donthuc donthuc::trudt(const donthuc &b)
    {
    heso = heso - b.heso;
    return *this;
    }
    donthuc donthuc::nhandt(const donthuc &b)
    {
    heso = heso*b.heso;
    somu = somu + b.somu;
    return *this;
    }
    donthuc donthuc::chiadt(const donthuc &b)
    {
    heso = heso/b.heso;
    somu = somu - b.somu;
    return *this;
    }
    int donthuc::sosanhdt(const donthuc &b)
    {
    if((heso == b.heso) &&(somu == b.somu))
    return 0;
    else
    if ((somu > b.somu)|| ((somu== b.somu)&&(heso > b.heso)))
    return 1;
    return 0;
    }
    donthuc donthuc::operator+(const donthuc &a)
    {
    return congdt(a);

    }
    donthuc donthuc::operator-(const donthuc &a)
    {
    return trudt(a);
    }
    donthuc donthuc::operator*(const donthuc &a)
    {
    return nhandt(a);
    }
    donthuc donthuc::operator/(const donthuc &a)
    {
    return chiadt(a);
    }
    donthuc donthuc::operator=(const donthuc &a)
    {
    heso = a.heso;
    somu = a.somu;
    return *this;
    }
    donthuc donthuc::operator+=(const donthuc &a)
    {
    heso += a.heso;
    return *this;
    }
    donthuc donthuc::operator-=(const donthuc &a)
    {
    heso -= a.heso;
    return *this;
    }
    int donthuc::operator>(const donthuc &a)
    {
    if((somu > a.somu) || (heso > a.heso && a.somu == somu ))
    return 1;
    else
    return 0;
    }
    int donthuc::operator<(const donthuc &a)
    {

    if((somu < a.somu) || (heso < a.heso && a.somu == somu ))
    return 1;
    else
    return 0;
    }
    int donthuc::operator==(const donthuc &a)
    {
    if((heso == a.heso) && (somu == a.somu))
    return 1;
    else
    return 0;
    }
    int donthuc::operator>=(const donthuc &a)
    {
    if((heso >= a.heso) && (somu >= a.somu))
    return 1;
    else
    return 0;
    }
    int donthuc::operator<=(const donthuc &a)
    {
    if((heso <= a.heso) && (somu <= a.somu))
    return 1;
    else
    return 0;
    }
    int donthuc::operator!=(const donthuc &a)
    {
    if((heso != a.heso) || (somu != a.somu))
    return 1;
    else
    return 0;
    }
    donthuc donthuc::operator++()
    {
    somu ++;
    return *this;
    }
    donthuc donthuc::operator--()
    {
    somu --;
    return *this;
    }
    istream& operator>>(istream &is,donthuc &a)
    {
    is>>a.heso>>a.somu;
    return is;
    }
    ostream& operator<<(ostream &os,const donthuc &a)
    {
    os<<"He so = "<<a.heso<<endl;
    os<<"So mu = "<<a.somu<<endl;
    return os;
    }
    donthuc donthuc::operator!()
    {
    heso = heso*somu;
    somu = somu - 1;
    return *this;
    }
    donthuc donthuc::operator~()
    {
    somu = somu + 1;
    heso = heso/somu;
    return *this;
    }
    mình mới học class nên chưa rõ lắm:
    Đã được chỉnh sửa lần cuối bởi mavuong2000 : 08-10-2011 lúc 02:52 PM. Lý do: sửa lại tiêu đề

  2. #2
    Ngày gia nhập
    09 2011
    Bài viết
    16

    mình đã chạy thử bài của bạn và mình thấy 2 toán tử << và >> hoạt động bình thường mà có bất thường nào đâu???

Các đề tài tương tự

  1. Ý nghĩa của hàm nextInt(n) trong class Random
    Gửi bởi friendly_smile trong diễn đàn Nhập môn lập trình Java
    Trả lời: 0
    Bài viết cuối: 09-03-2013, 07:32 PM
  2. Định nghĩa các sự kiện cho class trong C# như thế nào ?
    Gửi bởi logweb176 trong diễn đàn Thắc mắc lập trình C#
    Trả lời: 2
    Bài viết cuối: 27-06-2012, 11:09 AM
  3. tự định nghĩa nhập xuất(>>, <<) trong class phân số
    Gửi bởi satthuvotinh trong diễn đàn Thắc mắc lập trình C/C++/C++0x
    Trả lời: 1
    Bài viết cuối: 01-09-2011, 09:32 AM
  4. CONST trong khai báo hàm của class sentence có nghĩa là gì?
    Gửi bởi oxfordthuongyeu trong diễn đàn Thắc mắc lập trình C/C++/C++0x
    Trả lời: 6
    Bài viết cuối: 20-05-2011, 09:57 AM
  5. Class trong lập trình Visual C++ nghĩa là gì?
    Gửi bởi Zack Fair trong diễn đàn Thắc mắc lập trình Visual C++
    Trả lời: 1
    Bài viết cuối: 09-02-2011, 10:54 AM

Quyền hạn của bạn

  • Bạn không thể gửi đề tài mới
  • Bạn không thể gửi bài trả lời
  • Bạn không thể gửi các đính kèm
  • Bạn không thể chỉnh sửa bài viết của bạn