Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 9 của 9
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    lỗi ở từ khóa friend trong bài phân số C++

    Chào các pac,em dang lam bài class PhanSo mon huong doi tuong nhung mới viết được có 2 hàm đã khựng rùi, em co 3 file nhu sau:
    thay đổi thu viện liên tục nhưng nó cũng không chịu hiểu dùm em cái hàm friend trong file PhanSo.cpp. bác nào bit xin chỉ dùm em.
    Mã:
    //file PhanSo.h
    #include <iostream>
    
    using namespace std;
    
    class PhanSo
    {
    private:
    	int m_tu;
    	int m_mau;
    
    public:
    	// Nhóm tạo hủy.
    	PhanSo();
    	PhanSo(int tu, int mau);
    	PhanSo(int giaTri);
    	PhanSo(const PhanSo &p);
    	~PhanSo();
    
    	// Nhóm cung cấp thông tin.
    	int layTu() const;
    	int layMau() const;
    	void nhapphanso(const PhanSo &a );
    	void xuatphanso(const PhanSo &a );
    	// Nhóm xử lý nghiệp vụ.
    	float tinhGiaTri() const;
    	PhanSo nghichDao() const;
    	PhanSo rutGon() const;
    
    	int soSanh(const PhanSo &p) const;
    	PhanSo cong(const PhanSo &p) const;
    	PhanSo tru(const PhanSo &p) const;
    	PhanSo nhan(const PhanSo &p) const;
    	PhanSo chia(const PhanSo &p) const;
    
    	// Nhóm toán tử.
    	PhanSo& operator ++();
    	PhanSo& operator ++(int i);
    	PhanSo& operator --();
    	PhanSo& operator --(int i);
    
    	bool operator >(const PhanSo &p) const;
    	bool operator <(const PhanSo &p) const;
    	bool operator ==(const PhanSo &p) const;
    	bool operator !=(const PhanSo &p) const;
    	bool operator >=(const PhanSo &p) const;
    	bool operator <=(const PhanSo &p) const;
    
    	PhanSo operator +(const PhanSo &p) const;
    	PhanSo operator -(const PhanSo &p) const;
    	PhanSo operator *(const PhanSo &p) const;
    	PhanSo operator /(const PhanSo &p) const;
    	PhanSo& operator +=(const PhanSo &p);
    	PhanSo& operator -=(const PhanSo &p);
    	PhanSo& operator *=(const PhanSo &p);
    	PhanSo& operator /=(const PhanSo &p);
    
    	operator int() const;
    	operator float() const;
    
    	friend istream& operator >>(istream &is, PhanSo &p);
    	friend ostream& operator <<(ostream &os, const PhanSo &p);
    	
    };
    //__________________________________________________________________
    
    //file PhanSo.cpp
    
    #include "PhanSo.h"
    
    
    friend istream& operator >>(istream &is, PhanSo &p);
    	{
    		os<<p.m_tu<<"/"<<;p.m_mau;
    		return os;
    	}
    friend ostream& operator <<(ostream &os, const PhanSo &p);
    	{
    		cout<<"
    Nhap  Tu so:";is>>p.m_tu;
    		do
    		{
    			cout<<"
    Nhap Mau so :";
    			is>>p.m_mau;
    		}
    		while(p.m_mau==0);
    		return is;
    	} 
    /* PhanSo PhanSo::cong(const PhanSo &a)
    {
    	PhanSo t;
    	t.m_tu=this->m_tu*a.m_mau+this->m_mau*a.m_tu;
    	t.m_mau=this->m_mau*a.m_mau;
    	return t;
    } */
    
    //___________________________
    va file Main.cpp
    
    #include "PhanSo.cpp"
    void Main()
    {
    	PhanSo a,b;
    	cout<<"Nhap phan so 1: ";
    	cin>>a;
    	cout<<"Nhap phan so 2: ";
    	cin>>b;
    	
    }

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Mã nguồn PHP:
    friend istream& operator >>(istream &is, PhanSo &p); // dấu này ";" mắc cười quá { os<<p.m_tu<<"/"<<;p.m_mau; //khai báo is mà xài os ở đâu đây,sửa lại nha bạn return os; }friend ostream& operator <<(ostream &os, const PhanSo &p); //đấu ";" để đây làm j { cout<<"
    Nhap Tu so:";is>>p.m_tu; //đây nữa do { cout<<"
    Nhap Mau so :"; is>>p.m_mau; } while(p.m_mau==0); return is; }  
    Sửa lại nè:
    Mã nguồn PHP:
    friend istream& operator >>(istream &is, PhanSo &p) { cout<<"
    Nhap Tu so:"
    ; is>>p.m_tu; do { cout<<"
    Nhap Mau so :"
    ; is>>p.m_mau; }while(p.m_mau==0); return is; } friend ostream& operator <<(ostream &os, const PhanSo &p) { os<<"PS :"<<p.m_mau<<"/"<<p.m_tu; return os; }  

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    ac,:d zay ma hem thấy, nhưng mà mình sữa lại như bạn rùi vẫn báo loi

    trong file PhanSo.cpp sua lai nhu sau:
    Mã:
    #include "PhanSo.h"
    
    
    
    
    istream& operator >>(istream &is, PhanSo &p)
        {
                    cout<<"
    Nhap  Tu so:";
            is>>p.m_tu;
                    do
            {
                cout<<"
    Nhap Mau so :";
                is>>p.m_mau;
            }while(p.m_mau==0);
            return is;
        }
    ostream& operator <<(ostream &os, const PhanSo &p)
        {
              os<<"PS :"<<p.m_mau<<"/"<<p.m_tu;
                      return os;
        }

  4. #4
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trích dẫn Gửi bởi balaxanh27
    ac,:d zay ma hem thấy, nhưng mà mình sữa lại như bạn rùi vẫn báo loi

    trong file PhanSo.cpp sua lai nhu sau:
    Mã:
    #include "PhanSo.h"
    
    
    
    
    istream& operator >>(istream &is, PhanSo &p)
        {
                    cout<<"
    Nhap  Tu so:";
            is>>p.m_tu;
                    do
            {
                cout<<"
    Nhap Mau so :";
                is>>p.m_mau;
            }while(p.m_mau==0);
            return is;
        }
    ostream& operator <<(ostream &os, const PhanSo &p)
        {
              os<<"PS :"<<p.m_mau<<"/"<<p.m_tu;
                      return os;
        }
    Chính xác ,phải bỏ chữ friend đi,lâu rồi nên quên.hihihi

  5. #5
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    umh, minh da sua lai nhu vay rui nhung van chua chay duoc, bao lỗi tùm lum, pac có thể sữa lại cho nó chạy được luôn được hem, mình đang làm bài nhưng den đây chạy hok duoc cũng po tay,

  6. #6
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trích dẫn Gửi bởi balaxanh27
    umh, minh da sua lai nhu vay rui nhung van chua chay duoc, bao lỗi tùm lum, pac có thể sữa lại cho nó chạy được luôn được hem, mình đang làm bài nhưng den đây chạy hok duoc cũng po tay,
    OK.chờ chút nha.

  7. #7
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Mà bạn coi bài thử bài nay trước :
    Mã nguồn PHP:
    #include<iostream> #include<conio.h> #include<stdio.h> #include<math.h> using namespace std; class PS { private: int ts; int ms; public: PS(); friend istream& operator>>(istream &i,PS &a); friend ostream& operator<<(ostream &o,PS &a); PS operator+(PS a); PS operator-(PS a); PS operator*(PS a); PS operator/(PS a); int UC(); // lay uoc chung lon nhat cua tu va mau void RutGon(); int operator <(PS a); int operator >(PS a); int operator==(PS a); }; PS::PS() { ts=ms=1; } istream& operator>>(istream &i,PS &a) { i>>a.ts>>a.ms; return i; } ostream& operator<<(ostream &o,PS &a) { a.RutGon(); if(a.ts!=0) o<<a.ts<<"/"<<a.ms; else o<<"0"; return o; } int PS::UC() { int a=abs(this->ts); int b=abs(this->ms); while(a!=b) { if(a>b) a=a-b; else b=b-a; } return a; }; PS PS::operator+(PS a) { PS temp; temp.ts=this->ts*a.ms+this->ms*a.ts; temp.ms=this->ms*a.ms; return temp; } PS PS::operator-(PS a) { PS temp; temp.ts=this->ts*a.ms-this->ms*a.ts; temp.ms=this->ms*a.ms; return temp; } PS PS::operator*(PS a) { PS temp; temp.ts=this->ts*a.ts; temp.ms=this->ms*a.ms; return temp; } PS PS::operator/(PS a) { PS temp; temp.ts=this->ts*a.ms; temp.ms=this->ms*a.ts; return temp; } void PS::RutGon() { int uoc=UC(); ts/=uoc; ms/=uoc; } int PS::operator <(PS a) { PS temp; temp.ts=this->ts*a.ms; temp.ms=this->ms*a.ms; a.ts*=this->ms; a.ms*=this->ms; return (temp.ts<a.ts)?1:0; } int PS::operator >(PS a) { PS temp; temp.ts=this->ts*a.ms; temp.ms=this->ms*a.ms; a.ts*=this->ms; a.ms*=this->ms; return (temp.ts>a.ts)?1:0; } int PS::operator ==(PS a) { PS temp; temp.ts=this->ts; temp.ms=this->ms; temp=temp/a; if(temp.ms==temp.ts==1) return 1; return 0; } void main() { PS a,b; cout<<"Nhap phan so a:";cin>>a; cout<<"Nhap phan so b:";cin>>b; cout<<"=========Cac Phep Toan=============
    "
    ; cout<<"a la: "<<a<<"
    "
    ; cout<<"b la: "<<b; cout<<"
    a+b = "
    <<a+b<<endl; //cout<<"a-b = "<<a-b<<endl; cout<<"a*b = "<<a*b<<endl; cout<<"a/b = "<<a/b<<endl; if(a<b) cout<<a<<" nho hon "<<b<<endl; if(a==b) cout<<a<<" bang nhau "<<b<<endl; getch(); }  

  8. #8
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    code thì mình có thể viết được nhưng chia nó thành 3file nhu kieu minh lam kìa, 1 file .h chứa class, 1 file Phanso.cpp, 1 file main.cpp. Bạn giúp mình cho trót lun nhé, hì

  9. #9
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    code thì mình có thể hiểu và viết được nhưng chia nó ra làm 3 file như mình làm kìa, 1 file chứa class PhanSo.h, 1 file phanso.cpp chua cac phuong thuc cua class, còn file còn lại chứa hàm main. bạn sửa và chạy dùm mình cái nha, thank you

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •