我这段代码是怎么回事呢?能帮我看看吗各位

dengfei007 2009-04-26 03:53:55

#include <iostream>
#include <string>
using namespace std;
class String//注意这里String中的'S'是大写
{
private:
char * p;
int size;

public:

String();//建立一个空串
String(int sz);
String(char *str);//用C++串来初始化,注意char *str这里实际上就是str是一个串即是一个串对象
String(String &ob);//用串对象来初始化
~String(){delete []p;};
int strsize() {return strlen(p);}//注意这里的p是字符串对象
friend ostream & operator <<(ostream & stream,String & ob);
String operator +(String & ob);
String operator +(char *s);
friend String operator +(char * s,String & ob);

};
String::String()
{
size=1;//用来存放结束标志NULL
p=new char[size];
if(p==NULL)
{cout<<"分配内存出错"<<endl;
exit(1);
}
p=NULL;
}
String::String(int sz)
{
size=sz;
p=new char[sz];
if(p==NULL)
{
cout<<"分配内存错误!"<<endl;
exit(1);
}

}
String::String(char *str)
{
size=strlen(str)+1;
p=new char[size];
if(p==NULL)
{
cout<<"分配内存错误"<<endl;
exit(1);
}
strcpy(p,str);
}
String::String(String & ob)
{
size=ob.size;
p=new char[size];
if(p==NULL)
{
cout<<"分配内存出错!"<<endl;
exit(1);
}
strcpy(p,ob.p);
}
ostream & operator <<(ostream & stream,String & ob)
{
stream<<ob.p;
return stream;
}
String String ::operator +(String & ob)
{
//两个串对象的连接
int len;
len=strlen(p)+strlen(ob.p)+1;
String temp(len);
strcpy(temp.p,p);
strcat(temp.p,ob.p);
return temp;
}
String String::operator +(char * s)
{
//一个串对象与一个带引号串的连接
int len;
len=strlen(s)+strlen(p)+1;
String temp(len);
strcpy(temp.p,p);
strcat(temp.p,s);
return temp;

}
String operator +(char * s,String & ob)
{
int len;

len=strlen(s)+strlen(ob.p)+1;
String temp(len);
strcpy(temp.p,s);
strcat(temp.p,ob.p);
return temp;

}
...全文
88 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
bbcby 2009-04-26
  • 打赏
  • 举报
回复
可以改为:
#include <iostream.h>
#include <string.h>
class String//注意这里String中的'S'是大写
{
private:
char *p;
int size;
public:
String();//建立一个空串
String(int sz);
String(char *str);//用C++串来初始化,注意char *str这里实际上就是str是一个串即是一个串对象
String(String &ob);//用串对象来初始化
~String(){delete []p;};
int strsize() {return strlen(p);}//注意这里的p是字符串对象
friend ostream& operator <<(ostream& stream,String & ob);
String operator +(String & ob);
String operator +(char *s);
friend String operator+(char*s,String&ob);
};
String::String()
{
size=1;//用来存放结束标志NULL
p=new char[size];
if(p==NULL)
{
cout <<"分配内存出错" <<endl;
//exit(1);
}
p=NULL;
}
String::String(int sz)
{
size=sz;
p=new char[sz];
if(p==NULL)
{
cout <<"分配内存错误!" <<endl;
//exit(1);
}
}
String::String(char *str)
{
size=strlen(str)+1;
p=new char[size];
if(p==NULL)
{
cout <<"分配内存错误" <<endl;
//exit(1);
}
strcpy(p,str);
}
String::String(String & ob)
{
size=ob.size;
p=new char[size];
if(p==NULL)
{
cout <<"分配内存出错!" <<endl;
//exit(1);
}
strcpy(p,ob.p);
} ;
ostream& operator <<(ostream&stream,String&ob)
{
stream<<ob.p;
return stream;
}
String String ::operator +(String& ob)
{
//两个串对象的连接
int len;
len=strlen(p)+strlen(ob.p)+1;
String temp(len);
strcpy(temp.p,p);
strcat(temp.p,ob.p);
return temp;
}
String String::operator +(char * s)
{
//一个串对象与一个带引号串的连接
int len;
len=strlen(s)+strlen(p)+1;
String temp(len);
strcpy(temp.p,p);
strcat(temp.p,s);
return temp;
}
String operator +(char * s,String & ob)
{
int len;
len=strlen(s)+strlen(ob.p)+1;
String temp(len);
strcpy(temp.p,s);
strcat(temp.p,ob.p);
return temp;
}
int main()
{
char *c="visual";
String a(3);
String b("12345");
cout<<b<<endl;
cout<<b+c<<endl;
return 0;
}
你的头文件有点问题,导致了不能访问私有成员的错误
dengfei007 2009-04-26
  • 打赏
  • 举报
回复
那具体应该怎么改啊
liliangbao 2009-04-26
  • 打赏
  • 举报
回复
修改并测试,看一看
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

using namespace std;
class String//注意这里String中的'S'是大写
{
private:
char * p;
int size;

public:

String();//建立一个空串
String(int sz);
String(char *str);//用C++串来初始化,注意char *str这里实际上就是str是一个串即是一个串对象
String(const String &ob);//用串对象来初始化
~String(){delete []p;};
int strsize() {return strlen(p);}//注意这里的p是字符串对象
friend ostream & operator <<(ostream & stream,String & ob);
String operator +(String & ob);
String operator +(char *s);
friend String operator +(char * s,String & ob);

};
String::String()
{
size=1;//用来存放结束标志NULL
p=new char[size];
if(p==NULL)
{
cout <<"分配内存出错" <<endl;
exit(1);
}
*p='\0'; //初始化串
}
String::String(int sz)
{
size=sz;
p=new char[sz];
if(p==NULL)
{
cout <<"分配内存错误!" <<endl;
exit(1);
}
*p = '\0';//初始化串
}
String::String(char *str)
{
size=strlen(str)+1;
p=new char[size];
if(p==NULL)
{
cout <<"分配内存错误" <<endl;
exit(1);
}
strcpy(p,str);
}
String::String(const String & ob) //建议用const修饰,不然常量对象就不让使用
{
size=ob.size;
p=new char[size];
if(p==NULL)
{
cout <<"分配内存出错!" <<endl;
exit(1);
}
strcpy(p,ob.p);
}
ostream & operator <<(ostream & stream,String & ob)
{
stream <<ob.p<<endl;
return stream;
}
String String ::operator +(String & ob)
{
//两个串对象的连接
int len;
len=strlen(p)+strlen(ob.p)+1;
String temp(len);
strcpy(temp.p,p);
strcat(temp.p,ob.p);
return temp;
}
String String::operator +(char * s)
{
//一个串对象与一个带引号串的连接
int len;
len=strlen(s)+strlen(p)+1;
String temp(len);
strcpy(temp.p,p);
strcat(temp.p,s);
return temp;

}
String operator +(char * s,String & ob)
{
int len;

len=strlen(s)+strlen(ob.p)+1;
String temp(len);
strcpy(temp.p,s);
strcat(temp.p,ob.p);
return temp;

}

int main(int argc, char *argv[])
{

String str1;
String str2(5);
String str3("12345");
String str4(str3);
String str5 = str4 + "678";//拷贝构造用const的好处,不然是不对的,下面同理
String str6 = "0" + str4;
String str7 = str3 + str4;

cout<<"1= "<<str1<<endl;
cout<<"2= "<<str2<<endl;
cout<<"3= "<<str3<<endl;
cout<<"4= "<<str4<<endl;
cout<<"5= "<<str5<<endl;
cout<<"6= "<<str6<<endl;
cout<<"7= "<<str7<<endl;

system("PAUSE");
return 0;
}
deltamaster 2009-04-26
  • 打赏
  • 举报
回复
p在类中被声明为private类型,只有本类的成员函数和友元函数可以访问。
deltamaster 2009-04-26
  • 打赏
  • 举报
回复
错误说不能访问private成员
dengfei007 2009-04-26
  • 打赏
  • 举报
回复
补发问题
--------------------Configuration: 串类及实现 - Win32 Debug--------------------
Compiling...
串类及实现.cpp
E:\代码\临时\基本数据结构\串类及实现.cpp(69) : error C2248: 'p' : cannot access private member declared in class 'String'
E:\代码\临时\基本数据结构\串类及实现.cpp(7) : see declaration of 'p'
E:\代码\临时\基本数据结构\串类及实现.cpp(97) : error C2248: 'p' : cannot access private member declared in class 'String'
E:\代码\临时\基本数据结构\串类及实现.cpp(7) : see declaration of 'p'
E:\代码\临时\基本数据结构\串类及实现.cpp(99) : error C2248: 'p' : cannot access private member declared in class 'String'
E:\代码\临时\基本数据结构\串类及实现.cpp(7) : see declaration of 'p'
E:\代码\临时\基本数据结构\串类及实现.cpp(100) : error C2248: 'p' : cannot access private member declared in class 'String'
E:\代码\临时\基本数据结构\串类及实现.cpp(7) : see declaration of 'p'
E:\代码\临时\基本数据结构\串类及实现.cpp(100) : error C2248: 'p' : cannot access private member declared in class 'String'
E:\代码\临时\基本数据结构\串类及实现.cpp(7) : see declaration of 'p'
Error executing cl.exe.

串类及实现.obj - 5 error(s), 0 warning(s)
liliangbao 2009-04-26
  • 打赏
  • 举报
回复
String::String() 
{
size=1;//用来存放结束标志NULL
p=new char[size];
if(p==NULL)
{cout < <"分配内存出错" < <endl;
exit(1);
}
p=NULL; //上面开辟空间成功,你这怎么将其置空呢?可以修改为*p = '\0';
}
  • 打赏
  • 举报
回复
问题呢?
mengde007 2009-04-26
  • 打赏
  • 举报
回复
你的程序总是成对的出现strcpy(temp.p,s); strcat(temp.p,ob.p);前者覆盖了程序的p;后者却又在后面续上了;
把 strcpy(temp.p,s);去掉
baiwei156 2009-04-26
  • 打赏
  • 举报
回复
说说出现的问题
starry_zx 2009-04-26
  • 打赏
  • 举报
回复
什么 怎么回事
花生-sniper 2009-04-26
  • 打赏
  • 举报
回复
不错

65,211

社区成员

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

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