刚刚的,还是刚刚的问题.

还是刚刚的问题. - 故障解答 - 电脑教程网

还是刚刚的问题.

日期:2007-06-11   荐:
还是刚刚的问题.#include "stdafx.h"#include <iostream.h>#include <string.h>class string{public:char str[100];string(char *c){for(h=0;c[h]!='\0';h )str[h]=c[h];} string operator (string &s);void display(){cout<<"复制后的字符串为:"<<s1.str; private:int h;};string string::operator (string &s){return (string)(strcat(str,s.str));}void main(){char a="sdfsgfgssfgBBBBB",b="AAAAgfhsaggfgsfd"; string s1(a); string s2(b); s1 = s1 s2; s1.display();}我的这个程序有问题,应该怎么改呢?????帮我改改,谢谢!!!1#include <string.h>跟你定义的string命名冲突改了,但没有解决掉始终问题!!!!!!我主要是想问问,运算符重载那个地方的问题,应该怎样解决!!!!#include <iostream.h>#include <string.h>class Mystring{public:char str[100];Mystring(char *c){for(h=0;c[h]!='\0';h )str[h]=c[h];str[h]='\0';// } Mystring operator (Mystring &s);void display(){cout<<"复制后的字符串为:"<<str<<endl;} private:int h;};Mystring Mystring::operator (Mystring &s){return (Mystring)(strcat(str,s.str));}void main(){char a[]="sdfsgfgssfgBBBBB",b[]="AAAAgfhsaggfgsfd";Mystring s1(a);s1.display();Mystring s2(b);s2.display();s1 = s1 s2;s1.display();}重载的并没有错,只是你的构造函数,给str赋值时候加上字符串结束字符''你用的是成员函数重载运算符,那我的是用友元函数重载.这两者有什么区别吗?class String {char * str;public: String(char * s) { str=new char[strlen(s) 1]; strcpy(str,s); } ~String(char * s) { delete[] this->str; } String & operator (String & s) { char * tmp=new char[strlen(s) strlen(this->str) 1]; strcpy(tmp,this->str); strcat(tmp,s); delete[] this->str; this->str=tmp; return this; }friend ostream & operator << (String s,ostream &)}ostream & operator << (String s,ostream & oo) {oo<<s->str;return oo;}TO: zengwujun(月之海)"重载的并没有错,只是你的构造函数,给str赋值时候加上字符串结束字符''"是''还是'\0'? 二者有何区别?string(char *c){for(h=0;c[h]!='\0';h )str[h]=c[h];}==============string(char *c) { strncpy(str, c, strlen(c)); }还是不要用数组了,char *s; string(char *c) { s = new char[strlen(c) 1]; strcpy(s, c); }构造函数可以这样实现:string(char *c){ strcpy(str, c); }重载用一个参数--加数,而友元你得用两个参数--加数和被加数
标签: