作者sunneo (艾斯寇德)
站内Programming
标题Re: 希望C++传送「长度错误」等文字回去
时间Mon Sep 17 00:38:39 2007
※ 引述《[email protected] ( )》之铭言:
: 请问一下,我在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
1F:→ sunneo:..原来那边收不到这边的讯息220.132.228.138 09/17 23:06
※ 编辑: sunneo 来自: 220.132.228.138 (09/17 23:07)