taị sao khai báo tu và mau rồi mà nó vẫn báo lỗi vậy mn
Cách sử dụng hàm friend để overload operator nhập xuất
chổ gạch dưới là màu đó đấy anh cụ thể là dưới a
Error 2 error C2248: ‘PS::mau’ : cannot access private member declared in class ‘PS’ c:\users\nhuy59\desktop\bai tap\baithuchanh\baithuchanh\ps.cpp 22 1 baithuchanh
8 IntelliSense: member “PS::tu” (declared at line 11 of “c:\users\nhuy59\desktop\bai tap\baithuchanh\baithuchanh\PS.h”) is inaccessible c:\Users\NhuY59\Desktop\bai tap\baithuchanh\baithuchanh\PS.cpp 28 8 baithuchanh
Biến tu
, mau
là biến private thì sao mà xài được?
nhưng trong hàm bạn ta dùng tham chiếu cho nó rồi mà a
Lần sau @nhuyngo_nhuyngo95 post code lên nhé, nhìn code trong file ảnh khó chịu quá.
- Xem lại phần khai báo và định nghĩa một đằng một nẻo ở operator
>>
và<<
, chỗ có khai báo&
thì không định nghĩa&
và ngược lại - Save file rồi compile lại
ok e cam ơn anh nhiều
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <math.h>
using namespace std;
class PS {
private:
int tu,mau; //ten thuoc tinh
public:
friend istream & operator >> (istream& is, PS &p);
friend ostream & operator << (ostream& os,PS p);
PS operator +(PS y);//y ten doi tuong
PS operator-(PS y);
PS operator*(PS y);
PS operator/(PS y);
};
int ucln(int a,int b) {
int ta;
ta=abs(a);
int tb;
tb=abs(b);
while(ta!=tb) {
if (ta>tb)
ta=ta-tb;
else
ta=tb-ta;
}
return(ta);
}
istream& operator>>(istream &is,PS &p) { //
cout<<"nhap tu so \n";
is>>p.tu;
cout<<"nhap mau \n";
is>>p.mau;
return is;
}
ostream& operator<<(ostream& os,PS p) {
os<< p.tu <<"/"<< p.mau;
return os;
}
PS PS::operator+(PS y) {
PS kq;
kq.tu=(tu*y.mau)+(mau*y.tu);
kq.mau=(mau*y.mau);
int tan=ucln(tu,mau);
kq.tu=kq.tu/tan;
kq.mau=kq.mau/tan;
return (kq);
}
PS PS::operator-(PS y) {
PS kq;
kq.tu=(tu*y.mau)-(y.tu*mau);
kq.mau=(mau*y.mau);
int tan=ucln(tu,mau);
kq.tu=kq.tu/tan;
kq.mau=kq.mau/tan;
return (kq);
}
PS PS::operator*(PS y) {
PS kq;
kq.tu=tu*y.tu;
kq.mau=mau*y.mau;
int tan=ucln(tu,mau);
kq.tu=kq.tu/tan;
kq.mau=kq.mau/tan;
return(kq);
}
PS PS::operator/(PS y) {
PS kq;
kq.tu=tu*y.mau;
kq.mau=mau*y.tu;
int tan=ucln(tu,mau);
kq.tu=kq.tu/tan;
kq.mau=kq.mau/tan;
return(kq);
}
void main() {
PS a,b,c,d,e,f;
cout<<"Nhap phan so x:\n";
cin>>a;
cout<<"nhap phan so b\n:";
cin>>b;
c=a+b;
d=a-b;
e=a*b;
f=a/b;
cout<< a <<" + "<< b <<" = "<< c<<endl;
cout<< a <<" - "<< b <<" = "<< d<<endl;
cout<< a <<" * "<< b <<" = "<< e<<endl;
cout<< a <<" / "<< b <<" = "<< f;
system (" pause ");
return ;
}
chương trình em chạy mà sao không xuất được vậy a
Hàm ucln của bạn chạy vô hạn.
sưa lại :
else tb-=ta;
Bạn nên dùng thuật toán euclid để chạy nhanh hơn
int ucln(int a,int b){
a=abs(a);
b=abs(b);
while(a!=0){
int x=b%a;
b=a;
a=x;
}
return b;
}
Bạn nên tối ưu hàm constructor
PS::PS(int _tu,int _mau){
int g=ucln(_tu,_mau);
tu=_tu/g;
mau=_mau/g;
}
PS PS::operator-(PS y) {
int _tu=(tu*y.mau)-(y.tu*mau);
int _mau=(mau*y.mau);
return PS(_tu,_mau);
}
Bạn có biết debug chưa? Bạn cần debug trước khi đặt câu hỏi, nếu bị lỗi tại dòng nào đó thì post lên để mọi người giúp hoặc trực tiếp tra google trước để chủ động.
cái này ham rut gọn hả p
tập debug đi bạn, lỗi ngay 2 hàm friend á, prototype 1 đằng, implement 1 nẻo
cái đó mình sửa và chạy rùi nhưng mà bị lổi á
mà debug là kiểm tra lỗi đúng ko bạn
sao nhập cùng mẫu số không được vậy bạn