[語法] String實做debug

看板C_and_CPP作者 (zenix)時間15年前 (2009/05/21 20:47), 編輯推噓0(0013)
留言13則, 4人參與, 最新討論串1/1
//排的有點醜,請包涵 //DevC++編譯不過 //其指出的錯誤已加在code內 #include<iostream> #include<string.h> using namespace std; class String { protected: char *str; int length; int capacity; public: String() { str=new char[100]; memset(str,0,sizeof(char)*100); length=0,capacity=100; } String(const char *a)//fixed:plus "const" { length=strlen(a),capacity=100; if(length>=capacity) { str=new char[length+1]; memset(str,0,sizeof(char)*(length+1)); capacity=length+1; strcpy(str,a); //for(int i=0;i<=length;i++)str[i]=a[i]; } else { str=new char[100]; strcpy(str,a); //for(int i=0;i<=length;i++)str[i]=a[i]; str[length]=0; } } String(const String &a)//fixed:plus "const" { length=a.length; capacity=100; if(a.length>=capacity) { //delete str; str=new char[length+1]; capacity=length+1; memset(str,0,sizeof(char)*(length+1)); strcpy(str,a.str); } else { str=new char[100]; strcpy(str,a.str); str[length]=0; } } ~String() { delete []str; length=0; capacity=0; } friend bool operator==(const String &a,const char *b) { return strcmp(a.str,b)==0; } friend bool operator==(const String &a,const String &b) { return strcmp(a.str,b.str)==0; } friend bool operator==(const char *a,const String &b) { return strcmp(a,b.str)==0; } friend bool operator!=(const String &a,const char *b) { return (!(a==b)); } friend bool operator!=(const String &a,const String &b) { return (!(a==b)); } friend bool operator!=(const char *a,const String &b) { return (!(a==b)); } friend ostream& operator<<(ostream& stream,const String& a) { return (stream<<a.str); } friend istream& operator>>(istream& stream,const String& a) { return stream>>a.str; } /*-------------------Error from here-----------------------------*/ const String& operator=(const String& a) /*Error:must be a nonstatic function*/ /*Error:must take exactly two arguments*/ //已訂正 { length=a.length; /*invalid use of non-static data member*/ if(length>=this.capacity)/*invalid use again*/ { delete str; /*invalid use of 'this' in none member func.*/ /*non-static data member again*/ str=new char[length+1]; capacity=length+1; strcpy(str,a.str); //以下關於str、length、capacity幾乎都有invalid use的問題 //friend 訂正完後沒事了 } else { strcpy(str,a.str); } } const String& operator=(const char* a) /*Error:must be a nonstatic function Error:must take exactly two arguments*/ //已訂正 { length=strlen(a); if(length>=capacity) { delete str; str=new char[length+1]; capacity=length+1; strcpy(str,a); } else { strcpy(str,a); } } friend const String operator+(const String& a,String& b) { char *k=new char[a.length+b.length+1]; strcpy(k,a.str); strcat(k,b.str); String Sum(k); return Sum; } friend const String operator+(const char* a,String& b) { char *k=new char[strlen(a)+b.length+1]; strcpy(k,a); strcat(k,b.str); String Sum(k); return Sum; } friend const String operator+(const String& a,char* b) { char *k=new char[a.length+strlen(b)+1]; strcpy(k,a.str); strcat(k,b); String Sum(k); return Sum; } }; /* 請問要怎麼改才對呢? 另外,有時後會看到operator+(單引數) 但Dev C++會顯示錯誤 又是怎麼回事(已解) */ /* 再問一個問題 就是使用字串加法的時候 編譯器會顯示no matching function for call to 'String::String(const String)' candidates are: String::String(String&) String::String(char*) 是指怎樣的錯? (建構式要加上const,否則operator在配置暫存的String時會出錯) */ - -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.165.105.228

05/21 20:50, , 1F
首先就是memset那邊可能不該寫sizeof(str)
05/21 20:50, 1F

05/21 20:52, , 2F
那邊我確定可以這樣用,相當於完全清空 測試也沒事
05/21 20:52, 2F

05/21 20:56, , 3F
sizeof(str)是4或8
05/21 20:56, 3F

05/21 20:59, , 4F
該是member的就member,不要全部無限上綱成friend
05/21 20:59, 4F

05/21 21:03, , 5F
啊!...我知道什麼意思了 非常感謝
05/21 21:03, 5F
※ 編輯: zenixls2 來自: 218.165.105.228 (05/21 21:30)

05/21 21:26, , 6F
印象中this應該是個指標 要寫this->
05/21 21:26, 6F

05/21 21:28, , 7F
然後可能就像四樓說的 有些可以不用friend
05/21 21:28, 7F

05/21 21:29, , 8F
感覺一個參數的operator=不用加friend 因為都是String
05/21 21:29, 8F

05/21 21:29, , 9F
(不知道有沒有記錯 太久沒用c++的friend了)
05/21 21:29, 9F

05/21 21:38, , 10F
friend訂正過了 感謝
05/21 21:38, 10F
※ 編輯: zenixls2 來自: 218.165.105.228 (05/21 21:48)

05/21 22:18, , 11F
有人能解我最後一個疑惑嗎?
05/21 22:18, 11F

05/21 22:44, , 12F
String(const String&)
05/21 22:44, 12F

05/21 22:45, , 13F
String(const char*)
05/21 22:45, 13F
※ 編輯: zenixls2 來自: 218.165.100.123 (05/22 21:04)
文章代碼(AID): #1A5Krf_0 (C_and_CPP)