Re: 希望C++傳送「長度錯誤」等文字回去

看板Programming作者 (艾斯寇德)時間17年前 (2007/09/17 00:38), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串2/5 (看更多)
※ 引述《jiannrong@kkcity.com.tw ( )》之銘言: : 請問一下,我在class內偵測傳送過來的資料當東西產生時,傳送錯誤的代碼回去, : 如果我希望他傳送的是「長度錯誤」等文字回去,那我該怎麼寫呢? : 謝謝! : 附上原始語法如下: : #include <iostream> : class CBox : { : private: : double length; : double breadth; : double height; : public : : double Volume(double a,double b,double c) : { : if (a<0) : return -1; : else if (b<0) : return -2; : else if (c<0) : return -3; : else : { : length=a; : breadth=b; : height=c; : return height*length*breadth; : } : } : }; : : using namespace std; : int main() : { : CBox box1; : CBox box2; : double boxVolume = 0.0; : cout << endl : << "Volume of box1 =" << box1.Volume(10,50,60); : cout << endl : << "Volume of box2 =" : << box2.Volume(10,-10,20); //雖然資料有錯 : 但是東西仍產生了 只是這是不良品 : cout <<endl; : system("PAUSE"); : return 0; : } 我想你需要加強的是排版 排版好了,程式碼更好看出錯誤吧... 如果你是要傳文字回去,你可以讓他的回傳值是一個 const char* 而這個錯誤訊息的字元陣列空間也許是外面的人給的, 或者是這個型別的一個data member 像是 class CMSGBuffer { protected: char *errmsg; size_t length; public: CMSGBuffer() { errmsg = (char*)calloc(1,sizeof(char)); } void resize(size_t length) { errmsg = (char*)realloc(errmsg,(this->length = length)* sizeof(char)); } ~CMSGBuffer(){ free(errmsg); } }; class B : private CMSGBuffer { public: const char* foo(int i) { char* msg; if(!i) msg = "Something wrong"; else msg = "Nothing"; resize(strlen(msg) + 1); strcpy(errmsg,msg); return errmsg; } }; -- IceCold::IceCode -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.132.228.138

09/17 23:06, , 1F
..原來那邊收不到這邊的訊息
09/17 23:06, 1F
※ 編輯: sunneo 來自: 220.132.228.138 (09/17 23:07)
文章代碼(AID): #16xLmHdC (Programming)
討論串 (同標題文章)
文章代碼(AID): #16xLmHdC (Programming)