![]() |
#2
zqmillet2011-01-27 11:33
|
每次运行的时候程序都会崩溃,我逐行测试后发现是在测试代码的最后一句return 1; 后崩溃的
因为用无参数构造函数就不会崩溃,我感觉是析构函数出现了问题,麻烦大家帮我看看

/*
* 无参数构造函数
*/
Formula::Formula(void)
{
formulaString = new char [1];
formulaString = '\0';
return;
}
* 无参数构造函数
*/
Formula::Formula(void)
{
formulaString = new char [1];
formulaString = '\0';
return;
}

/*
* 有参数构造函数
*/
Formula::Formula(const char * str)
{
if (str == NULL)
{
formulaString = new char [1];
formulaString = '\0';
}
else
{
formulaString = new char [strlen(str)];
strcpy(formulaString,str);
}
return;
}
* 有参数构造函数
*/
Formula::Formula(const char * str)
{
if (str == NULL)
{
formulaString = new char [1];
formulaString = '\0';
}
else
{
formulaString = new char [strlen(str)];
strcpy(formulaString,str);
}
return;
}

/*
* 析构函数
*/
Formula::~Formula(void)
{
delete [] formulaString;
return;
}
* 析构函数
*/
Formula::~Formula(void)
{
delete [] formulaString;
return;
}
这是声明,就一个成员变量

class Formula
{
public:
Formula(void);
Formula(const Formula & f);
Formula(const char * str);
Formula(const double num);
~Formula(void);
public:
Formula & operator = (const char * str);
Formula & operator = (const Formula & f);
Formula & operator = (const double num);
public:
int GetLength (void) const;
public:
void Trim (void);
private:
char * formulaString;
};
{
public:
Formula(void);
Formula(const Formula & f);
Formula(const char * str);
Formula(const double num);
~Formula(void);
public:
Formula & operator = (const char * str);
Formula & operator = (const Formula & f);
Formula & operator = (const double num);
public:
int GetLength (void) const;
public:
void Trim (void);
private:
char * formulaString;
};
这是测试代码

int main (void)
{
Formula f(" vfjwjfwe");
// f.Trim ();
return 1;
}
{
Formula f(" vfjwjfwe");
// f.Trim ();
return 1;
}