各位看下这段代码出了什么问题,小弟刚入门~

yoshikis 2011-11-13 12:11:50
头文件代码:
#ifndef _sieve_
#define _sieve_
class CString
{
private:
char*p_str;
public:
CString(char* p_s);
void print();
~CString();

};
#endif


实现代码:
CString::CString(char* p_s){
p_str=new char[strlen(p_s)];
strcpy(p_str,p_s);
}
void CString::print(){
while(*p_str!='\0')
{
cout<<*p_str;
p_str++;
}
}

CString::~CString(){
delete [] p_str;}

void main()
{
char* pstr;
char p[20];
int i;
pstr=&p[0];
cout<<"Enter a string: ";
while(p[i]!='\0')//到这里就报错了。。
{
for(i=0;i<20;i++)
cin>>p[i];
}

CString str(pstr);
str.print();
}


...全文
278 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
ai_xue_xi 2011-11-14
  • 打赏
  • 举报
回复
#include<iostream>
using namespace std;

class CString
{
private:
char*p_str;
public:
CString(char* p_s);
void print();
~CString();

};



CString::CString(char* p_s){
p_str=new char[strlen(p_s)+1];
strcpy(p_str,p_s);
}
void CString::print(){
//while(*p_str!='\0')
//{
cout<<p_str;
// p_str++;
//}
}

CString::~CString(){
delete []p_str;}

void main()
{

char p[20];
char *pstr;
int i;
cout<<"Enter a string: ";
for(i=0;i<20;i++)
cin>>p[i];
pstr=&p[0];
CString str(pstr);
str.print();
}


输入的时候要输入20个字符 不然它会等待你继续输入 你看下这是不是你要的结果
fukang258369 2011-11-14
  • 打赏
  • 举报
回复
我去,回复呢?
fukang258369 2011-11-14
  • 打赏
  • 举报
回复
new和delete的问题。
LZS535261548 2011-11-13
  • 打赏
  • 举报
回复
你的i 没有进行定义 楼主加油加油
yoshikis 2011-11-13
  • 打赏
  • 举报
回复
能不能帮我试着调试下程序,看看怎样输出字符串?现在问题是输出不了啊。。==
BT六眼飞鱼 2011-11-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hnuqinhuan 的回复:]

i没有初始化
int i = 0;
[/Quote]
正解
yoshikis 2011-11-13
  • 打赏
  • 举报
回复
求高手指点~感激不尽
yoshikis 2011-11-13
  • 打赏
  • 举报
回复
试了下,程序能运行,但按了回车没法输出字符串,请问这是怎么回事?
無_1024 2011-11-13
  • 打赏
  • 举报
回复
i没有初始化
int i = 0;
TheLostMan丶 2011-11-13
  • 打赏
  • 举报
回复
啊 已经有正解了
金元520 2011-11-13
  • 打赏
  • 举报
回复
.....
怎么半天
还是没弄好
qscool1987 2011-11-13
  • 打赏
  • 举报
回复
#ifndef _sieve_
#define _sieve_
#include <iostream>
using namespace std;
class CString
{
private:
char*p_str;
public:
CString(char* p_s);
void print();
~CString();

};
#endif


//实现代码:
CString::CString(char* p_s)//---->>这里有个拷贝,所有你的数组里必须'\0'结尾
{
p_str=new char[strlen(p_s)+1];
strcpy(p_str,p_s);
p_str[strlen(p_s)] = '\0';
}
void CString::print()
{
cout<<p_str;//------->>直接这样就行,不然析构会出错
}

CString::~CString()
{
delete [] p_str;//------------>>这里会出错,如果上面不改会造成内存泄漏
}

void main()
{
//char* pstr;
char p[20];
int i;
char ch;
//pstr=&p[0];
cout<<"Enter a string: ";
// while(p[i]!='\0')//到这里就报错了。。
//{
for(i=0;i<19;i++)
{
cin >> ch;
p[i] = ch;
if(!cin.good())//---->>检查输入流状态
break;
}
p[19] = '\0';//--------->>必须有这句
CString str(p);
str.print();
}
这样,运行下看看
yoshikis 2011-11-13
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 lijy520 的回复:]
引用 20 楼 lijy520 的回复:
CString::CString(char* p_s){
p_str=new char[strlen(p_s)];
strcpy(p_str,p_s);
}
void CString::print(){
char *p1=p_str;
while(*p1!='\0')
{
cout<<*p1;
p_str++;
}
}

CSt……
[/Quote]还是输不出、、、
金元520 2011-11-13
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 lijy520 的回复:]
CString::CString(char* p_s){
p_str=new char[strlen(p_s)];
strcpy(p_str,p_s);
}
void CString::print(){
char *p1=p_str;
while(*p1!='\0')
{
cout<<*p1;
p_str++;
}
}

CString::~CString(){
del……
[/Quote]


漏了个
p_str++; 改为==> p1++;
金元520 2011-11-13
  • 打赏
  • 举报
回复
CString::CString(char* p_s){
p_str=new char[strlen(p_s)];
strcpy(p_str,p_s);
}
void CString::print(){
char *p1=p_str;
while(*p1!='\0')
{
cout<<*p1;
p_str++;
}
}

CString::~CString(){
delete [] p_str;}

void main()
{
char* pstr;
char p[20];
int i=0;
pstr=&p[0];
cout<<"Enter a string: ";
while(p[i]!='\0')
{
for(i=0;i<19;i++)
cin>>p[i];
}
p[i]='\0';

CString str(pstr);
str.print();
}


试试看
yoshikis 2011-11-13
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 lijy520 的回复:]

刚回的怎么没发得出来
有几个问题
1.i初始化
2.获得字符串的时候,不好输入'\0',所以需要p[i]='\0'(前提i没有越界);
以给后面的strcpy和print中判断使用
3.print中别用 p_str++; 这样会改变本身 p_str 的指向,导致最后delete[] 有问题(泄露)
[/Quote]要怎么改呢?
金元520 2011-11-13
  • 打赏
  • 举报
回复

刚回的怎么没发得出来
有几个问题
1.i初始化
2.获得字符串的时候,不好输入'\0',所以需要p[i]='\0'(前提i没有越界);
以给后面的strcpy和print中判断使用
3.print中别用 p_str++; 这样会改变本身 p_str 的指向,导致最后delete[] 有问题(泄露)
yoshikis 2011-11-13
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 qscool1987 的回复:]
引用 11 楼 yoshikis 的回复:

引用 9 楼 qscool1987 的回复:
p_str=new char[strlen(p_s)];
改成p_str=new char[strlen(p_s) + 1];
因为strlen计算的是字符串的长度不包括\0’字符,你分配内存的时候必须把这个\0算上,string输出时都是遇到\0字符终止输出的
但为什么程序没有输出字符串,我现……
[/Quote]初始化了,也照你刚才的代码打进去了,还是输不出字符串。。
yoshikis 2011-11-13
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 qscool1987 的回复:]
手误,
void main()
{
//char* pstr;
//char p[20];
int i = 0;
//pstr=&p[0];
cout<<"Enter a char: ";
/*while(p[i]!='\0')//到这里就报错了。。
{
for(i=0;i<20;i++)
cin>>p[i];
}*/
whil……
[/Quote]还不行。。。
qscool1987 2011-11-13
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 yoshikis 的回复:]

引用 9 楼 qscool1987 的回复:
p_str=new char[strlen(p_s)];
改成p_str=new char[strlen(p_s) + 1];
因为strlen计算的是字符串的长度不包括\0’字符,你分配内存的时候必须把这个\0算上,string输出时都是遇到\0字符终止输出的
但为什么程序没有输出字符串,我现在的问题在这里
[/Quote]
上面都说了,i没初始化啊,所以他得值是乱七八糟的,
加载更多回复(8)

64,653

社区成员

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

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