首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

求大神帮忙找异常

2012-05-21 
求大神帮忙找错误啊#include iostreamusing namespace stdclass string{public :friend ostream operat

求大神帮忙找错误啊
#include <iostream>
using namespace std;
class string
{
public :
friend ostream operator<<(ostream & , string t);
  string (const char *str=NULL);
string (const string &other);
string& operator=(const string &other) ;
string operator+(const string &other) const;
protected:  
char *m_data;
};

  string::string(const char *str)
{
if(!str)m_data=0;
else
{
m_data=new char[strlen(str)+1];
  strcpy(m_data,str);
}
}
  string::string(const string &other)
{
if(!other.m_data) m_data=0;
else 
{
m_data =new char [strlen(other.m_data)+1];
strcpy (m_data , other.m_data);
}
}
string & string :: operator =(const string &other)
{
if(this!=&other)
{
delete[]m_data;
if(!other.m_data) m_data=0;
else
{m_data=new char[strlen(other.m_data)+1];
strcpy(m_data, other.m_data);
}
}
return *this;
string string::operator+(const string &other)const
{
string new string;
if(!other.m_data)
newstring=*this;
else if(!m_data)
newstring=other;
else
{
newstring.m_data=new char[strlen(m_data)+strlen(other.m_data)+1)];
strcpy(new string.m_data,m-data);
strcat (new string.m_data,other.m_data);
}
  return new string;
}
void main()
{
char a[100],b[100];
string str1;
cout<<"请输入:"<<endl;
cin>>a;
str1=a;
string str2;
cout<<"请输入:"<<endl;
cin>>s;
str2=b;
string str3=str1+str2;
cout<<"输出:"<<endl;
cout<<a<<endl;
cout<<b<<endl;
cout<<str3.m_data<<"\n"<<endl;

}

[解决办法]

C/C++ code
#include <iostream>#define endl "\r\n"class string{public :    friend std::ostream operator<<(std::ostream & , string t);    string (const char *str=NULL);    string (const string &other);    string& operator=(const string &other) ;    string operator+(const string &other) const;public:       char *m_data;};string::string(const char *str){    if(!str)m_data=0;     else    {        m_data=new char[strlen(str)+1];        strcpy(m_data,str);    }}string::string(const string &other){    if(!other.m_data) m_data=0;    else      {        m_data =new char [strlen(other.m_data)+1];        strcpy (m_data , other.m_data);    }}string & string :: operator =(const string &other){    if(this!=&other)    {        delete[]m_data;        if(!other.m_data) m_data=0;        else        {m_data=new char[strlen(other.m_data)+1];        strcpy(m_data, other.m_data);        }     }    return *this;}string string::operator+(const string &other)const{    string newstring;    if(!other.m_data)        newstring=*this;    else if(!m_data)        newstring=other;    else    {        newstring.m_data=new char[strlen(m_data)+strlen(other.m_data)+1];        strcpy(newstring.m_data,m_data);        strcat (newstring.m_data,other.m_data);    }    return newstring;}void main(){    char a[100],b[100];    string str1;    std::cout<<"请输入:"<<endl;    std::cin>>a;    str1=a;    string str2;    std::cout<<"请输入:"<<endl;    std::cin>>b;    str2=b;    string str3=str1+str2;    std::cout<<"输出:"<<endl;    std::cout<<a<<endl;    std::cout<<b<<endl;    std::cout<<str3.m_data<<"\n"<<endl;    }
------解决方案--------------------


名字重复了。。。。string是个类名。。。换个名字
[解决办法]
冒个泡 一般自定义类型都会采用Myxxx这种命名

热点排行