帮忙看看错在哪吧!!!高分求教啦!!!

MashiMaro2002 2002-08-01 04:10:38
刚开始学,试着写了一个实现字符串连接的小程序,运行结果却有乱码,不知道错哪了,请各位大虾帮忙指点一二,高分求教哦!!!谢了!!!
#include <iostream.h>
#include <string.h>

class String{
public:
String(char* pN="no name")
{
pName=new char[strlen(pN)+1];
if(pName!=0)
strcpy(pName,pN);
}
String(String& s)
{
pName=new char[strlen(s.pName)+1];
if(pName!=0)
strcpy(pName,s.pName);
}
~String()
{
if(pName)
{
delete pName;
pName=0;
}
}
friend String operator +(String& s1,String& s2);
void display()
{
cout<<pName<<endl;
}
protected:
char* pName;
};

String operator +(String& s1,String& s2)
{
char* newStr=new char[strlen(s1.pName)+strlen(s2.pName)+1];
strcpy(newStr,s1.pName);
strcat(newStr,s2.pName);
return String(newStr);
}

void main()
{
String s1("aaa"),s2("bbb"),s3;
cout<<"String1 is:";
s1.display();
cout<<"String2 is:";
s2.display();
s3=s1+s2;
cout<<"String1 + String2 is:";
s3.display();
}
...全文
137 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
MashiMaro2002 2002-08-03
  • 打赏
  • 举报
回复
谢谢各位大虾!!!确有收获呀!!!
^_^
punpuny 2002-08-02
  • 打赏
  • 举报
回复
否则对于形如
CString s1("aaa");
CString s2;
CString &(
GoogleGeek 2002-08-02
  • 打赏
  • 举报
回复
To:punpuny()
谢谢!我的写法不规范!确实应该返回String&!
实际上你当初的理解对于该问题也是正确的!----你的本意应该是:同一块内存被delete两次!
punpuny 2002-08-01
  • 打赏
  • 举报
回复
psusong(我心飞扬)不好意思,我弄错了。
你的code基本正确,在你的代码中=的重载应该返回String&而不是String,这个看一下CString的做法就知道了。
GoogleGeek 2002-08-01
  • 打赏
  • 举报
回复
To:punpuny()
问题出在s3=s1+s2这句上面,你在+这个操作符重载函数中返回的是一个临时变量,出了函数体后就会析构,这样s3中的内容当然是无效的了。
-----------------------------------------
老大!谁告诉你除了函数体变量就立即被析构掉?搞笑!
compiler 确保在使用完了变量后,才会对变量进行析构!如果象你说得那样,不只会有多少的传值调用的函数会失败!
GoogleGeek 2002-08-01
  • 打赏
  • 举报
回复
出现乱码,是应为你使用了不该使用的内存!
你忘记了C++中的拷贝构造函数的调用时机(机制)!
只有在定义一个类的对象并且以同类的对象作为该对象的初值时,才会调用该类的拷贝构造函数!,否则,如果没有提供相应的运算符重载的话,将直接调用compiler生成的每有什么意义的bitwisecopy function!
运行下面的程序 !Everything is OK!
----------------------------------------
// console.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream.h>
#include <string.h>

class String{
public:
String(char* pN="no name")
{
pName=new char[strlen(pN)+1];
if(pName!=0)
strcpy(pName,pN);
}
String(const String& s)
{
pName=new char[strlen(s.pName)+1];
if(pName!=0)
strcpy(pName,s.pName);
}
~String()
{
if(pName)
{
delete[] pName;
pName=0;
}
}
//must add the function:operator =
String operator =(String& str)
{
//first delete the pName;
delete[] pName;
int len=strlen(str.pName)+1;
pName=new char[len];
strcpy(pName,str.pName);
return *this;
}
friend String operator +(String& s1,String& s2);
void display()
{
cout<<pName<<endl;
}
protected:
char* pName;
};

String operator +(String& s1,String& s2)
{
char* newStr=new char[strlen(s1.pName)+strlen(s2.pName)+1];
strcpy(newStr,s1.pName);
strcat(newStr,s2.pName);
return String(newStr);
}

void main()
{
String s1("aaa"),s2("bbb"),s3;
cout<<"String1 is:";
s1.display();
cout<<"String2 is:";
s2.display();
//call the String String::operator =(String& str),
//not the function:String::String(const String &s)
s3=s1+s2;
cout<<"Now call the overloading operators:operator =(String& str),! "<<endl;
cout<<"String1 + String2 is:";
s3.display();
//Only when you define a object ,call the function:String::(String &s)
cout<<"Now call the function:String::(const String& str)!"<<endl;
String s=s1+s2;
cout<<"String1 + String2 is:";
s.display();
}
punpuny 2002-08-01
  • 打赏
  • 举报
回复
问题出在s3=s1+s2这句上面,你在+这个操作符重载函数中返回的是一个临时变量,出了函数体后就会析构,这样s3中的内容当然是无效的了。
Xeroo 2002-08-01
  • 打赏
  • 举报
回复
你没有重载=
所以当s3=s1+s2时会出现错误
还有就是不能把char *强制转换成String可能会出错
不过不敢肯定~~~~~~权当参考
wanglei888 2002-08-01
  • 打赏
  • 举报
回复
在构造函数中:
String(char* pN="no name")
{
pName=new char[strlen(pN)+1];
if(pName!=0)
strcpy(pName,pN);
}
你判断if (pName != 0) 这一句,不会按你的意愿执行,因为pName没有被正确初始化,这样改:
String(char* pN="no name") : pName(0)
{
pName=new char[strlen(pN)+1];
if(pName!=0)
strcpy(pName,pN);
}
其他部分我没有发现问题。
GoogleGeek 2002-08-01
  • 打赏
  • 举报
回复
test it !

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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