• پایان فعالیت بخشهای انجمن: امکان ایجاد موضوع یا نوشته جدید برای عموم کاربران غیرفعال شده است

ارور در تابعی مربوط به operator overloding

silver boy

Registered User
تاریخ عضویت
7 می 2011
نوشته‌ها
56
لایک‌ها
0
سلام این سورس کد مربوط به سربار گذاری عملگر هاست دو تا ازش نوشتم ولی اور هاش باهم فرق می کنه لطفا کمک کنید

کد:
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>

class string{
friend ostream & operator<<(ostream & , const string &);
friend istream & operator>>(istream & , string &);
public:
string();
void getstring();
string& operator = (cost string &);
string & operator +=(const string &);
bool operator ! () const;
bool operator == (const string &) const ;
bool operator < (const string &) const ;
bool operator = (const string &) const ;
bool operator > (const string &) const ;
bool operator <= (const string &) const ;
bool operator >= (const string &) const ;
bool getlength() const ;
private:
int length;
char st[80];
void setstring (char *);};
string::string (){
st[0]='\0';}
void string ::getstring (){
cout << "pleease enter string :"<<endl;
gets(st);
this-> getlength();}
string& string::operator=(const string & right){
setstring(rigth.st);
return *this;}
string & string::operator += (const string & right){
strcat (this ->st , right.st);
return *this ;}
bool string:: operator !() const{
return (length==0);}
bool string ::operator ==(const string & right) const{
return(strcmp (st , right.st));}
bool string ::operator !=(const string & right) const{
return!(strcmp(st,right.st))==0);}
bool string ::operator <=(const string & right) const{
return(strcmp(st,right.st)<=0);}
bool string ::operator >(const string & right) const{
return (strcmp(st, right.st)>0);}
bool string ::operator >=(const string & right) const{
return (strcmp(st, right.st)>=0);}
void string ::setstring (const char *st2){
strcpy(st, st2);}
void string::getlength() const{
length= strlen(st);}
ostream & operator << (ostream & output , const string & s){
output<<s.st;
return output;}
istream&operator>>(istream & input , string &s){
input>>s.st;
s.getlength();
return input;}
void main(){
string s1, s2, s3, s4;
cout<<"please enter a string :"<<endl;
cin>>s1;
cout<<"please enter a string :"<<endl;
s2.getstring();
s3=s1; s4=s3; s4=s2;
cout<<"s1==s2?";
cout<<((s1==s2)?"true" : "false")<<endl;
cout<<"(s1>s2)?";
cout<<((s1>s2)?"true" : "false")<<endl;
//else


cout<<"s4 is nul? ";
cout<<((s4!)?"true" : "false")<<endl;
cout<<s1<<endl;
cout<<s2<<endl;
cout<<s3<<endl;
cout<<s4<<endl;}

اینم دومی ش
کد:
#include <conio.h>
#include <string.h>
#include <stdio.h>
#include <iostream.h>
class string{
friend ostream& operator<<(ostream&,const string &);
friend istream& operator>>(istream&, string &);
public:
string();
void getstring();
string& operator=(const string &);
string& operator+=(const string &);
bool operator!()const;
bool operator==(const string &)const;
bool operator<(const string &)const;
bool operator!=(const string &)const;
bool operator>(const string &)const;
bool operator<=(const string &)const;
bool operator>=(const string &)const;
void getlenght()const;
private:
int lenght;
char st[80];
void setstring(char *);};
string::string()
{
st[0]='\0';}
void string::getstring()
{
cout<<"please enter string:";
gets(st);
this->getlenght();
}
string& string::operator=(const string & right);
{setstring(right.st);
return *this;}
string& string::operator+=(const string & right)
{strcat(this->st,right.st);
return *this;}
bool string::operator!()const
{return(lenght==0);}
bool string::operator ==(const string & right)const
{return(strcmp(st,right.st)==0);}
bool string::operator <(const string & right)const
{return (strcmp(st,right.st)<0);}
bool string::operator !=(const string & right)const
{return !(strcmp(st,right.st)==0);}
bool string::operator <=(const string & right)const
{return (strcmp(st,right.st)<=0);}
bool string::operator >(const string & right)const
{return(strcmp(st,right.st)>0);}
bool string::operator >=(const string & right)const
{return(strcmp(st,right.st)<=0);}
void string::setstring(const char *st2)
{strcpy(st,st2);}
void string::getlenght() const
{lenght=strlen(st);}
ostream& operator<<(ostream&output,const string & s)
{output<<s.st;
return output;}
istream& operator>>(istream&input,string& s)
{input>>s.st;
s.getlenght();
return input;}
void main(){
string s1,s2,s3,s4;
cout<<"please enter a string:"<<endl;
cin>>s1;
cout<<"please enter a string:"<<endl;
s2.getstring();
s3=s1;
s4=s3;
s4+=s2;
cout<<"s1==s2?";
cout<<((s1==s2)?"true":"false")<<endl;
cout<<"(s1>s2)?";
cout<<((s4>s2)?"true":"false")<<endl;
cout<<"s4 is NULL?";
cout<<((s4)!:?"true":"false")<<endl;
cout<<s1<<endl;
cout<<s2<<endl;
cout<<s3<<endl;
cout<<s4<<endl;}
این دومی 2 تا اور میده:wacko:
 

Datis0

Registered User
تاریخ عضویت
7 ژوئن 2007
نوشته‌ها
497
لایک‌ها
42
محل سکونت
Esfahan
سلام این سورس کد مربوط به سربار گذاری عملگر هاست دو تا ازش نوشتم ولی اور هاش باهم فرق می کنه لطفا کمک کنید

کد:
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>

class string{
friend ostream & operator<<(ostream & , const string &);
friend istream & operator>>(istream & , string &);
public:
string();
void getstring();
string& operator = (cost string &);
string & operator +=(const string &);
bool operator ! () const;
bool operator == (const string &) const ;
bool operator < (const string &) const ;
bool operator = (const string &) const ;
bool operator > (const string &) const ;
bool operator <= (const string &) const ;
bool operator >= (const string &) const ;
bool getlength() const ;
private:
int length;
char st[80];
void setstring (char *);};
string::string (){
st[0]='\0';}
void string ::getstring (){
cout << "pleease enter string :"<<endl;
gets(st);
this-> getlength();}
string& string::operator=(const string & right){
setstring(rigth.st);
return *this;}
string & string::operator += (const string & right){
strcat (this ->st , right.st);
return *this ;}
bool string:: operator !() const{
return (length==0);}
bool string ::operator ==(const string & right) const{
return(strcmp (st , right.st));}
bool string ::operator !=(const string & right) const{
return!(strcmp(st,right.st))==0);}
bool string ::operator <=(const string & right) const{
return(strcmp(st,right.st)<=0);}
bool string ::operator >(const string & right) const{
return (strcmp(st, right.st)>0);}
bool string ::operator >=(const string & right) const{
return (strcmp(st, right.st)>=0);}
void string ::setstring (const char *st2){
strcpy(st, st2);}
void string::getlength() const{
length= strlen(st);}
ostream & operator << (ostream & output , const string & s){
output<<s.st;
return output;}
istream&operator>>(istream & input , string &s){
input>>s.st;
s.getlength();
return input;}
void main(){
string s1, s2, s3, s4;
cout<<"please enter a string :"<<endl;
cin>>s1;
cout<<"please enter a string :"<<endl;
s2.getstring();
s3=s1; s4=s3; s4=s2;
cout<<"s1==s2?";
cout<<((s1==s2)?"true" : "false")<<endl;
cout<<"(s1>s2)?";
cout<<((s1>s2)?"true" : "false")<<endl;
//else


cout<<"s4 is nul? ";
cout<<((s4!)?"true" : "false")<<endl;
cout<<s1<<endl;
cout<<s2<<endl;
cout<<s3<<endl;
cout<<s4<<endl;}

اینم دومی ش
کد:
#include <conio.h>
#include <string.h>
#include <stdio.h>
#include <iostream.h>
class string{
friend ostream& operator<<(ostream&,const string &);
friend istream& operator>>(istream&, string &);
public:
string();
void getstring();
string& operator=(const string &);
string& operator+=(const string &);
bool operator!()const;
bool operator==(const string &)const;
bool operator<(const string &)const;
bool operator!=(const string &)const;
bool operator>(const string &)const;
bool operator<=(const string &)const;
bool operator>=(const string &)const;
void getlenght()const;
private:
int lenght;
char st[80];
void setstring(char *);};
string::string()
{
st[0]='\0';}
void string::getstring()
{
cout<<"please enter string:";
gets(st);
this->getlenght();
}
string& string::operator=(const string & right);
{setstring(right.st);
return *this;}
string& string::operator+=(const string & right)
{strcat(this->st,right.st);
return *this;}
bool string::operator!()const
{return(lenght==0);}
bool string::operator ==(const string & right)const
{return(strcmp(st,right.st)==0);}
bool string::operator <(const string & right)const
{return (strcmp(st,right.st)<0);}
bool string::operator !=(const string & right)const
{return !(strcmp(st,right.st)==0);}
bool string::operator <=(const string & right)const
{return (strcmp(st,right.st)<=0);}
bool string::operator >(const string & right)const
{return(strcmp(st,right.st)>0);}
bool string::operator >=(const string & right)const
{return(strcmp(st,right.st)<=0);}
void string::setstring(const char *st2)
{strcpy(st,st2);}
void string::getlenght() const
{lenght=strlen(st);}
ostream& operator<<(ostream&output,const string & s)
{output<<s.st;
return output;}
istream& operator>>(istream&input,string& s)
{input>>s.st;
s.getlenght();
return input;}
void main(){
string s1,s2,s3,s4;
cout<<"please enter a string:"<<endl;
cin>>s1;
cout<<"please enter a string:"<<endl;
s2.getstring();
s3=s1;
s4=s3;
s4+=s2;
cout<<"s1==s2?";
cout<<((s1==s2)?"true":"false")<<endl;
cout<<"(s1>s2)?";
cout<<((s4>s2)?"true":"false")<<endl;
cout<<"s4 is NULL?";
cout<<((s4)!:?"true":"false")<<endl;
cout<<s1<<endl;
cout<<s2<<endl;
cout<<s3<<endl;
cout<<s4<<endl;}
این دومی 2 تا اور میده:wacko:
تو لینوکسم نمی تونم conio کامپایل کنم همین طوری یه نگاه کلی انداحنم ( تو هم نو هم بود حال نداشتم بخونم :دی ) ولی اولی خیلی مشکل داره مثلا اسپیس بعد از operator غیر مجازه تو دومی درسته
با چی کامپایل می کنی و ارور ها چیه ؟؟ تا کمکت کنیم :)
 

silver boy

Registered User
تاریخ عضویت
7 می 2011
نوشته‌ها
56
لایک‌ها
0
کد:
#include <iostream.h>
#include <conio.h>
#include <stdio.h>

class string{
friend ostream & operator<<(ostream & , const string &);
friend istream & operator>>(istream & , string &);
public:
string();
void getstring();
[COLOR="Yellow"]string & operator = (cost string &);[/COLOR]
string & operator +=(const string &);
bool operator ! () const;
bool operator == (const string &) const ;
bool operator < (const string &) const ;
bool operator = (const string &) const ;
bool operator > (const string &) const ;
bool operator <= (const string &) const ;
bool operator >= (const string &) const ;
bool getlength() const ;
private:
int lenghth;
char st[80];
void setstring (char *);};
string::string (){
st[0]='\0';}
void string ::getstring (){
cout << "pleease enter string :"<<endl;
gets(st);
this-> getlength()}
[COLOR="Yellow"]string & string ::operator =(const string & right){[/COLOR]
setstring(rigth.st);
return *this;}
string & string::operator += (const string & right){
str cat (this ->st , right.st);
return *this ;}
bool string:: operator !() const{
return (length==0);}
bool string ::operator ==(const string & right) const{
return(strcmp (st , right.st)<0;)}
[COLOR="Yellow"]bool string ::operator !=(const string & right) const{[/COLOR]
return!(strcmp(st,right.st))==0);}
bool string ::operator <=(const string & right) const{
return(strcmp(st,right.st)<=0);}
bool string ::operator >(const string & right) const{
return (strcmp(st, right.st)>0)}
bool string ::operator >=(const string & right) const{
return (strcmp(st, right.st)>=0)}
[COLOR="Yellow"]void string ::setstring (const char *st2){[/COLOR]
strcpy(st, st2);}
[COLOR="Yellow"]void string::gft length() const{[/COLOR]
[COLOR="Yellow"]lenght= strlen(st);}[/COLOR]
ostram & operator << (ostream & output , const string & s){
output<<s.st;
return output;}
istream&operator>>(istream & input , string &s){
input>>s.st;
s.getlength();
return input;}
void main{
string s1, s2, s3, s4;
cout<<"please enter a string :"<<endl;
cin>>s1;
cout<<"please enter a string :"<<endl;
s2.getstring();
s3=s1; s4=s3; s4=s2;
cout<<"s1==s2?";
cout<<((s1==s2)?"true" : "false")<<endl;
cout<<"(s1>s2)?";
cout<<((s1>s2)?"true" : "false")<<endl;
//else


coout<<"s4 is nul? ";
[COLOR="Yellow"]cout<<((s4!)?:"true" : "false")<<endl;[/COLOR]
cout<<s1<<endl;
cout<<s2<<endl;
cout<<s3<<endl;
cout<<s4<<endl;}

expected
string::eek:perator=(const string & ) is not amember of 'string'
string::eek:perator!=(const string & ) is not amember of 'string'
string ::setstring (const char *) is not member of string
string::getlength() const is not member of string
cannot modify a const object
Fuction should return a value
expected

کد:
#include <conio.h>
#include <string.h>
#include <stdio.h>
#include <iostream.h>
class string{
friend ostream& operator<<(ostream&,const string &);
friend istream& operator>>(istream&, string &);
public:
string();
void getstring();
string& operator=(const string &);
string& operator+=(const string &);
bool operator!()const;
bool operator==(const string &)const;
bool operator<(const string &)const;
bool operator!=(const string &)const;
bool operator>(const string &)const;
bool operator<=(const string &)const;
bool operator>=(const string &)const;
void getlenght()const;
private:
int lenght;
char st[80];
void setstring(char *);};
string::string()
{
st[0]='\0';}
void string::getstring()
{
cout<<"please enter string:";
gets(st);
this->getlenght();
}
[COLOR="Yellow"]string& string::operator=(const string & right);[/COLOR]
[COLOR="Yellow"]{setstring(right.st);[/COLOR]
return *this;}
string& string::operator+=(const string & right)
{strcat(this->st,right.st);
return *this;}
bool string::operator!()const
{return(lenght==0);}
bool string::operator ==(const string & right)const
{return(strcmp(st,right.st)==0);}
bool string::operator <(const string & right)const
{return (strcmp(st,right.st)<0);}
bool string::operator !=(const string & right)const
{return !(strcmp(st,right.st)==0);}
bool string::operator <=(const string & right)const
{return (strcmp(st,right.st)<=0);}
bool string::operator >(const string & right)const
{return(strcmp(st,right.st)>0);}
bool string::operator >=(const string & right)const
{return(strcmp(st,right.st)<=0);}
void string::setstring(const char *st2)
{strcpy(st,st2);}
void string::getlenght() const
{lenght=strlen(st);}
ostream& operator<<(ostream&output,const string & s)
{output<<s.st;
return output;}
istream& operator>>(istream&input,string& s)
{input>>s.st;
s.getlenght();
return input;}
void main(){
string s1,s2,s3,s4;
cout<<"please enter a string:"<<endl;
cin>>s1;
cout<<"please enter a string:"<<endl;
s2.getstring();
s3=s1;
s4=s3;
s4+=s2;
cout<<"s1==s2?";
cout<<((s1==s2)?"true":"false")<<endl;
cout<<"(s1>s2)?";
cout<<((s4>s2)?"true":"false")<<endl;
cout<<"s4 is NULL?";
cout<<((s4)!:?"true":"false")<<endl;
cout<<s1<<endl;
cout<<s2<<endl;
cout<<s3<<endl;
cout<<s4<<endl;}

Class member 'string::eek:perator=(const string &)' declared outside its class
Declaration terminated incorrectly
 

silver boy

Registered User
تاریخ عضویت
7 می 2011
نوشته‌ها
56
لایک‌ها
0
تو لینوکسم نمی تونم conio کامپایل کنم همین طوری یه نگاه کلی انداحنم ( تو هم نو هم بود حال نداشتم بخونم :دی ) ولی اولی خیلی مشکل داره مثلا اسپیس بعد از operator غیر مجازه تو دومی درسته
با چی کامپایل می کنی و ارور ها چیه ؟؟ تا کمکت کنیم :)
ممنون از کمکتون ولی متوجه نشدم کجا رو گفتید:blush:
من با بورلند می نویسم
 
بالا