帮我看看吧,我在一个CPP文件中怎么不可以编译成功?

liutengfeigo 2010-05-25 10:19:58
这是课本习题。。。错误出在重载<<和>>这里。我真的不晓得该怎么改了。。。。
#include <iostream>
#include <string>
using namespace std;
class String
{
char * str;
int len;
static int num_string;
static const int CINLIM = 80;
public:
String(const char * s);
String();
String(const String &);
~String();
int length()const;

String & operator=(const String &);
String & operator=(const char *);
char & operator[](int i);
const char & operator[](int i)const;

friend bool operator<(const String &st, const String &st2);
friend bool operator>(const String &st, const String &st2);
friend bool operator==(const String &st,const String &st2);

friend ostream & operator<<(ostream & os, const String &st);
friend istream & operator>>(istream & is, const String &st);

static int HowMany();
};

int String::num_string = 0;
int String::HowMany()
{ return num_string;}

String::String(const char * s)
{ len = strlen(s);
str = new char[len+1];
strcpy(str,s);
num_string++;
}
String::String()
{ len = 4 ;
str = new char[1];
str[0] = '\0';
num_string++;
}
String::String(const String & st)
{ len = st.len;
str = new char[len+1];
strcpy(str,st.str);
num_string++;
}

String::~String()
{ num_string--;
delete [] str;
}

String & String::operator=(const String & st)
{ if (this == &st) return *this;
delete []str;
len = st.len;
str = new char[len+1];
strcpy(str,st.str);
return *this;
}
String & String::operator=(const char * s)
{ delete [] str;
len = strlen(s);
str = new char[len+1];
strcpy(str,s);
return *this;
}
char & String::operator[](int i)
{ return str[i];}
const char & String::operator[](int i)const
{ return str[i];}

bool operator<(const String &st,const String &st1)
{ return (strcmp(st.str,st1.str) < 0);}
bool operator>(const String & st,const String &st1)
{return st1.str < st.str;}
bool operator==(const String &st,const String &st1)
{ return (strcmp(st.str,st1.str) == 0);}

ostring & operator<<(const ostream & os,const String &st)
{ os << st.str;
return os;
}
ostream & operator>>(const istream & is,const String &st)
{ char temp[80];
is.get(temp,80);
if(is)
st = temp;
while(is && is.get() != '\n')
continue;
return is;
}
int main(void)
{
String a;
cin >> a;
String b = "wo ai ni";
cout << a << b << endl;;
if (a > b ) cout << "sssssssssssssssssssssss" << endl;
b = a;
cout << a << b << endl;
if (a == b) cout << "xxxxxxxxxxxxxxxxxxxxxxxx" << endl;
system("pause");
return 0;
}
...全文
152 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
cattycat 2010-05-25
  • 打赏
  • 举报
回复
friend ostream & operator<<(ostream & os, const String &st);
friend istream & operator>>(istream & is, String &st);
ostream和istream前的const去掉,第二个st的const去掉
liutengfeigo 2010-05-25
  • 打赏
  • 举报
回复
还是不行,求助~~~
liutengfeigo 2010-05-25
  • 打赏
  • 举报
回复
还是不行啊。。。。~!
selooloo 2010-05-25
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 lthyxy 的回复:]
假如该成这样。
#include <iostream>
#include <string>
using namespace std;
class String
{
char * str;
int len;
static int num_string;
static const int CINLIM = 80;
public:
String(const char * s)……
[/Quote]

注意你的声明和实现
friend istream & operator>>(istream & is, const String &st);
istream & operator>>(istream & is, String &st);//这里被当做一个新的函数了,所以不能访问私有变量
太乙 2010-05-25
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 lthyxy 的回复:]
假如该成这样。
#include <iostream>
#include <string>
using namespace std;
class String
{
char * str;
int len;
static int num_string;
static const int CINLIM = 80;
public:
String(const char * s)……
[/Quote]


static const int CINLIM = 80;
之前
加上
friend istream & operator>>(istream & is, String &st);

非友元函数不能访问类的私有成员
liutengfeigo 2010-05-25
  • 打赏
  • 举报
回复
假如该成这样。
#include <iostream>
#include <string>
using namespace std;
class String
{
char * str;
int len;
static int num_string;
static const int CINLIM = 80;
public:
String(const char * s);
String();
String(const String &);
~String();
int length()const;

String & operator=(const String &);
String & operator=(const char *);
char & operator[](int i);
const char & operator[](int i)const;

friend bool operator<(const String &st, const String &st2);
friend bool operator>(const String &st, const String &st2);
friend bool operator==(const String &st,const String &st2);

friend ostream & operator<<( ostream & os, const String &st);
friend istream & operator>>(istream & is, const String &st);

static int HowMany();
};

int String::num_string = 0;
int String::HowMany()
{ return num_string;}

String::String(const char * s)
{ len = strlen(s);
str = new char[len+1];
strcpy(str,s);
num_string++;
}
String::String()
{ len = 4 ;
str = new char[1];
str[0] = '\0';
num_string++;
}
String::String(const String & st)
{ len = st.len;
str = new char[len+1];
strcpy(str,st.str);
num_string++;
}

String::~String()
{ num_string--;
delete [] str;
}

String & String::operator=(const String & st)
{ if (this == &st) return *this;
delete []str;
len = st.len;
str = new char[len+1];
strcpy(str,st.str);
return *this;
}
String & String::operator=(const char * s)
{ delete [] str;
len = strlen(s);
str = new char[len+1];
strcpy(str,s);
return *this;
}
char & String::operator[](int i)
{ return str[i];}
const char & String::operator[](int i)const
{ return str[i];}

bool operator<(const String &st,const String &st1)
{ return (strcmp(st.str,st1.str) < 0);}
bool operator>(const String & st,const String &st1)
{return (strcmp(st.str,st1.str) >0);}
bool operator==(const String &st,const String &st1)
{ return (strcmp(st.str,st1.str) == 0);}

ostream & operator<<(ostream & os,const String &st)
{ os << st.str;
return os;
}
istream & operator>>(istream & is, String &st)
{ char temp[String::CINLIM];
is.get(temp,String::CINLIM);
if(is)
st = temp;
while(is && is.get() != '\n')
continue;
return is;
}

int main(void)
{
String a;
cin >> a;
String b = "wo ai ni";
cout << a << b << endl;;
if (a < b ) cout << "sssssssssssssssssssssss" << endl;
b = a;
cout << a << b << endl;
if (a == b) cout << "xxxxxxxxxxxxxxxxxxxxxxxx" << endl;
system("pause");
return 0;
}
是不可以通过编译的。错误也是在 <<这里,请高手告诉我原因咯。。。
liutengfeigo 2010-05-25
  • 打赏
  • 举报
回复
devc++ 怎么帮我?你们教教我撒。~或者给篇文章
黑娃 2010-05-25
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 lthyxy 的回复:]
看来我不适合做程序员啊。这样的错误都没发现。。。。
[/Quote]

让编译器帮你
liutengfeigo 2010-05-25
  • 打赏
  • 举报
回复
连不舒服。看着眼都花了,,,,。
liutengfeigo 2010-05-25
  • 打赏
  • 举报
回复
看来我不适合做程序员啊。这样的错误都没发现。。。。
黑娃 2010-05-25
  • 打赏
  • 举报
回复
滥用const了
  • 打赏
  • 举报
回复
函数定义跟声明参数不一致
pengzhixi 2010-05-25
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
using namespace std;
class String
{
char * str;
int len;
static int num_string;
static const int CINLIM = 80;
public:
String(const char * s);
String();
String(const String &);
~String();
int length()const;

String & operator=(const String &);
String & operator=(const char *);
char & operator[](int i);
const char & operator[](int i)const;

friend bool operator<(const String &st, const String &st2);
friend bool operator>(const String &st, const String &st2);
friend bool operator==(const String &st,const String &st2);

friend ostream & operator<<(ostream & os, const String &st);
friend istream & operator>>(istream & is, String &st);

static int HowMany();
};

int String::num_string = 0;
int String::HowMany()
{ return num_string;}

String::String(const char * s)
{ len = strlen(s);
str = new char[len+1];
strcpy(str,s);
num_string++;
}
String::String()
{ len = 4 ;
str = new char[1];
str[0] = '\0';
num_string++;
}
String::String(const String & st)
{ len = st.len;
str = new char[len+1];
strcpy(str,st.str);
num_string++;
}

String::~String()
{ num_string--;
delete [] str;
}

String & String::operator=(const String & st)
{ if (this == &st) return *this;
delete []str;
len = st.len;
str = new char[len+1];
strcpy(str,st.str);
return *this;
}
String & String::operator=(const char * s)
{ delete [] str;
len = strlen(s);
str = new char[len+1];
strcpy(str,s);
return *this;
}
char & String::operator[](int i)
{ return str[i];}
const char & String::operator[](int i)const
{ return str[i];}

bool operator<(const String &st,const String &st1)
{ return (strcmp(st.str,st1.str) < 0);}
bool operator>(const String & st,const String &st1)
{return st1.str < st.str;}
bool operator==(const String &st,const String &st1)
{ return (strcmp(st.str,st1.str) == 0);}

ostream & operator<<(ostream & os,const String &st)
{ os << st.str;
return os;
}
istream & operator>>(istream & is,String &st)
{ is>>st.str;
return is;
}
int main(void)
{
String a;
cin >> a;
String b = "wo ai ni";
cout << a << b << endl;;
if (a > b ) cout << "sssssssssssssssssssssss" << endl;
b = a;
cout << a << b << endl;
if (a == b) cout << "xxxxxxxxxxxxxxxxxxxxxxxx" << endl;
system("pause");
return 0;
}


自行对比
selooloo 2010-05-25
  • 打赏
  • 举报
回复
去掉const,并注意拼写

ostream & operator<<(ostream & os,const String &st)
{ os << st.str;
return os;
}
istream & operator>>(istream & is, String &st)
{ char temp[80];
is.get(temp,80);
if(is)
st = temp;
while(is && is.get() != '\n')
continue;
return is;
}
zhangyafei13 2010-05-25
  • 打赏
  • 举报
回复
我来看看..







郁闷。

.
.
.
.
.






不懂
liutengfeigo 2010-05-25
  • 打赏
  • 举报
回复
这是一个模拟string类的类。
求助,谢谢了啊。
patricxuqi 2010-05-25
  • 打赏
  • 举报
回复
啥已经结贴了。我靠!白忙了。
patricxuqi 2010-05-25
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
using namespace std;
class String
{
char * str;
int len;
static int num_string;
static const int CINLIM = 80;
public:
String(const char * s);
String();
String(const String &);
~String();
int length()const;

String & operator=(const String &);
String & operator=(const char *);
char & operator[](int i);
const char & operator[](int i)const;

friend bool operator<(const String &st, const String &st2);
friend bool operator>(const String &st, const String &st2);
friend bool operator==(const String &st,const String &st2);

friend ostream & operator<<(ostream & os, const String &st);
friend istream & operator>>(istream & is, const String &st);

static int HowMany();
};

int String::num_string = 0;
int String::HowMany()
{ return num_string;}

String::String(const char * s)
{
len = strlen(s);
str = new char[len+1];
strcpy(str,s);
num_string++;
}
String::String()
{
len = 4 ;
str = new char[1];
str[0] = '\0';
num_string++;
}
String::String(const String & st)
{
len = st.len;
str = new char[len+1];
strcpy(str,st.str);
num_string++;
}

String::~String()
{
num_string--;
delete [] str;
}

String & String::operator=(const String & st)
{
if (this == &st) return *this;
delete []str;
len = st.len;
str = new char[len+1];
strcpy(str,st.str);
return *this;
}
String & String::operator=(const char * s)
{
delete [] str;
len = strlen(s);
str = new char[len+1];
strcpy(str,s);
return *this;
}
char & String::operator[](int i)
{
return str[i];
}
const char & String::operator[](int i)const
{ return str[i];}

bool operator<(const String &st,const String &st1)
{
return (strcmp(st.str,st1.str) < 0);}
bool operator>(const String & st,const String &st1)
{
return st1.str < st.str;}
bool operator==(const String &st,const String &st1)
{
return (strcmp(st.str,st1.str) == 0);}

ostream & operator<<(ostream & os,const String &st)//去掉const
{
os << st.str;
return os;
}
istream & operator>>(istream & is,const String &st)//去掉const ,返回类型改为istream
{
char temp[80];
is.get(temp,80);
if(is)
strcpy(st.str, (const char*)temp);//这里做一些修改。
while(is && is.get() != '\n')
continue;
return is;
}
int main(void)
{
String a;
cin >> a;
String b = "wo ai ni";
cout << a << b << endl;;
if (a > b ) cout << "sssssssssssssssssssssss" << endl;
b = a;
cout << a << b << endl;
if (a == b) cout << "xxxxxxxxxxxxxxxxxxxxxxxx" << endl;
system("pause");
return 0;
}

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧