#include <iostream>
#include <string.h>
using namespace std;class Str
{
public:
Str() {p=NULL;}
Str(char *str);
void display();
public:
char *p;
friend bool operator == (const Str& string1,const Str& string2);};Str::Str(char *str)
{ p=str;
}void Str::display()
{cout<<p;
}bool operator == (const Str &string1,const Str& string2)
{
if(strcmp(string1.p,string2.p)==0)
return true;
else
return false;
}
int main()
{
Str s1("hello");
Str s2("good");
if(s1==s2) cout<<"OK"<<endl;
else cout<<"false"<<endl;
return 0;
}如果编译有问题的话,先请你把friend bool operator ==这句话注释起来。再编译一下。
这个题目很典型哟!
#include <string.h>
using namespace std;class Str
{
public:
Str() {p=NULL;}
Str(char *str);
void display();
public:
char *p;
friend bool operator == (const Str& string1,const Str& string2);};Str::Str(char *str)
{ p=str;
}void Str::display()
{cout<<p;
}bool operator == (const Str &string1,const Str& string2)
{
if(strcmp(string1.p,string2.p)==0)
return true;
else
return false;
}
int main()
{
Str s1("hello");
Str s2("good");
if(s1==s2) cout<<"OK"<<endl;
else cout<<"false"<<endl;
return 0;
}如果编译有问题的话,先请你把friend bool operator ==这句话注释起来。再编译一下。
这个题目很典型哟!
#include <string.h>
using namespace std;class Str
{
public:
Str() {p = NULL;}
Str(char *str);
void display();
friend bool operator == (const Str& string1,const Str& string2);private:
char *p;
};
Str::Str(char *str)
{
p = str;
}void Str::display()
{
cout<<p<<endl;
}bool operator == (const Str &string1, const Str &string2)
{
if(strcmp(string1.p, string2.p) == 0)
return true;
else
return false;
}void main( void )
{
Str s1("w");
Str s2("w");
s1.display();
s2.display();
if(s1 == s2)
cout<<"OK"<<endl;
else
cout<<"false"<<endl;
}
-------
上面程序有什么问题..?
解决办法,是把实现代码放如类里面
class Str
{
public:
Str() {p=NULL;}
Str(char *str);
void display();
public:
char *p;
friend bool operator == (const Str& string1,const Str& string2)
{
if(strcmp(string1.p,string2.p)==0)
return true;
return false;
}
};