有关string中输出运算符<<重载的问题。

stone112979 2002-02-18 10:51:35
#include <iostream.h>
#include <string.h>
#include <stdlib.h>

class string
{
private:
char *str;
int length;
public:
string ();
string (char *);
~string () {delete []str;}
string& operator= (const string &);
friend ostream& operator<< (ostream&,const string &);
};

string::string ()
{
*str =NULL;
length = 0;
}

string::string (char *s)
{
length = strlen(s);
str = new char [length + 1];
strcpy (str, s);
}

string& string::operator= (const string& s)
{
delete [] str;
length = s.length;
str = new char [length + 1];
strcpy (str, s.str);
return *this;
}

ostream& operator<< (ostream &out,const string &s)
{
out << s.str;
return out;
}


void main ()
{
string s1("sandy");
string s2("stony");

string& rs = s1;
string *ps = &s1;
cout << rs << endl;
cout << *ps << endl;

/* rs = s2; // rs still refers to s1,but s1's value is now "stony"
cout << rs << endl;
cout << s1 << endl;
*/
ps = &s2; // ps now points to s2,s1 is unchanged
cout << *ps << endl;
cout << s1 << endl;
}
这样是编译是正确的

如果是#include <iostream>
#include <string>
using namespace std;
这样就有错,不知道是为什么?其中stdlib.h是什么库文件?
using namespace std; 到底是怎么用的?

还有一个程序我怎么改,编译还是不能通过。

#include <iostream.h>
#include <string.h>
#include <stdlib.h>

class String
{
public:
String (const char *str);
String (const String &other);
~String ();
String& operator= (const String &other);
ostream& operator<< (ostream &out, const String &str);
private:
char *m_data;
};

String::String (const char *str)
{
if(str==NULL)
{
m_data = new char[1];
*m_data = '\0';
}
else
{
int length = strlen(str);
m_data = new char[length+1];
strcpy(m_data, str);
}
}

String::~String ()
{
delete []m_data;
}

String::String (const String &other)
{
int length = strlen (other.m_data);
m_data = new char [length+1];
strcpy (m_data, other.m_data);
}

String& String::operator = (const String &other)
{
if (this == &other)
return *this;
delete []m_data;
int length = strlen (other.m_data);
m_data = new char [length+1];
strcpy (m_data, other.m_data);
return *this;
}

ostream& operator<< (ostream &out, const String &str)
{
out << str.m_data << endl;
return out;
}

void main ()
{
String a = "happy";
String b = " birther!";
String c(a);
b = c;
}


...全文
251 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
flcheng 2002-02-19
  • 打赏
  • 举报
回复
不带<.h>是stl的定义
这涉及到命名空间问题
using namespace std;
意思即使用std中的声明,否者要使用std::等
stdlib.h是个函数集合,
理解它的用处对编程是很有帮助的,
举例子,
(不恰当的)
你声明了一个列表类,放在list.h中,那么操纵它的动作就放在listlib.h中,当然,最好这里的函数可以操纵不仅仅是列表,还有队列,树等等.
flcheng 2002-02-19
  • 打赏
  • 举报
回复
问题2
<<重载,
你是打算友元类声明,还是类成员函数声明,下面是成员函数的声明的实现,但很荒唐
:
#include <iostream.h>
#include <string.h>
#include <stdlib.h>

class String
{
public:
String (const char *str);
String (const String &other);
~String ();
String& operator= (const String &other);
ostream& operator<< (ostream &out);
private:
char *m_data;
};

String::String (const char *str)
{
if(str==NULL)
{
m_data = new char[1];
*m_data = '\0';
}
else
{
int length = strlen(str);
m_data = new char[length+1];
strcpy(m_data, str);
}
}

String::~String ()
{
delete []m_data;
}

String::String (const String &other)
{
int length = strlen (other.m_data);
m_data = new char [length+1];
strcpy (m_data, other.m_data);
}

String& String::operator = (const String &other)
{
if (this == &other)
return *this;
delete []m_data;
int length = strlen (other.m_data);
m_data = new char [length+1];
strcpy (m_data, other.m_data);
return *this;
}

ostream& String::operator<< (ostream &out)
{
out << m_data << endl;
return out;
}

void main ()
{
String a = "happy";
String b = " birther!";
String c(a);
b = c;
a.operator << (cout);
b.operator << (cout);
c.operator << (cout);
}
mathe 2002-02-19
  • 打赏
  • 举报
回复
#include <string>
就是使用了STL中定义的string类,
如果你自己再次定义string,那当然是重复定义了。
你不需要自己来定义string类了,直接使用STL中的类就可以了。
stone112979 2002-02-19
  • 打赏
  • 举报
回复
可是我还是不懂如果.h可以的话,那么为什么不可以用using namespace std;呢?
snipersu 2002-02-18
  • 打赏
  • 举报
回复
#include <iostream>#include <string>:
你用的如果是VC++的话就不支持,只能是#include<*.h>
其中stdlib.h是 其它说明 的库文件(一些不归类的函数定义在这个库中)
如果你在ostream&operatior...前加上"friend"即:friend ostream& operator<< (ostream &out, const String &str);这样就能通过编译。
可能因为是有两个对象的操作,所以应用friend进行重载。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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