任何人都可以找到错误吗? [英] Can anyone find the errors?

查看:62
本文介绍了任何人都可以找到错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< cstring>

#include< iostream>

使用命名空间std;


class X

{

public:

X();

X(char *);

~X();

无效打印();


私人:

char * _str;

};


Y级:公共X

{

公开:

Y();

Y(字符*);

~Y();

void print();


私人:

char * _str;

};


X :: X()

{

static char * str =" X's str empty";

_str = new char [strlen(str)];

strcpy(_str,str);

cout<< X(),''" << str<< "''\ n";

}


X :: X(字符* str)

{

_str = new char [strlen(str)];

strcpy(str,str);

cout<< X(char *),''" << str<< "''\ n";

}


X ::〜X()

{

cout<< ~X(),deallocating''" << _str<< "''\ n";

删除_str;

}


void X :: print()

{

cout<<英寸×::打印():\t" << _str<< "''\ n";

}


Y :: Y()

{

static char * str =" Y'是空的" ;;

_str = new char [strlen(str)];

strcpy(_str,str );

cout<< Y(),''" << str<< "''\ n";

}


Y :: Y(字符* str)

{

_str = new char [strlen(str)];

strcpy(_str,str);

cout<< Y(char *),''" << str<< "''\ n";

}


Y :: ~Y()

{

cout<< ~Y()deallocating''" << _str<< "''\ n";

删除_str;

}


void Y :: print()

{

cout<< " Y ::打印():\t" << _str<< "''\ n";

}


int main()

{

X * x [3];


x [0] =新X(" X'的数据索引0");

x [1 ] =新Y(Y'的数据索引1);


// Y y(y'的数据索引2);

// x [2] =新Y(y);


x [0] - > print();

x [ 1] - > print();

// x [2] - > print();


删除x [0];

删除x [1];

//删除x [2];


返回0;

}

解决方案

sp ********@gmail.com 写道:


#include< cstring>

#include < iostream>

使用命名空间std;


class X

{

public:

X();

X(字符*);

~X();

void print() ;

私人:

char * _str;

};



[...更多代码...]

[主题:任何人都可以找到错误吗?]


哪一个?


我猜,最大的错误是没有使用std :: string。


-

Thomas
http:// www。 netmeister.org/news/learn2quote.html


sp********@gmail.com 写道:


你想更具体一点吗?


#include< cstring>

#include< iostream>

using namespace std;



使用声明一般都不好。


>

class X

{

public:

X();

X(char *);

~X();



需要析构函数的类可能还需要复制构造函数

和复制赋值运算符(三条规则)。如果你已经完成了任何会为你的班级调用复制语义的东西

你会遇到进一步的问题。


void print();



您是否也想将此功能设为虚拟?你的程序很难说



>

private:

char * _str;

};


>

X :: X( )

{

static char * str =" X's str empty" ;;



不推荐将const char数组转换为char数组。


_str = new char [strlen(str) )];

strcpy(_str,str);



WHOOP WHOOP ... strcpy复制strlen(str)+ 1个字符(

null终止符不计入strlen。未定义的行为

来自写下字符串的结尾。


cout<<" X(),''"< < str<<"''\ n";



ERROR.iostream不一定定义ostream函数。

你需要< ostreamincluded。


X :: X(char * str)

{

_str = new char [strlen(str)];

strcpy(str,str);



和以前一样的错误。
< blockquote class =post_quotes>
>

X ::〜X()

{

cout<< " ~X(),deallocating''"<< _str<<"''\ n" ;;

delete _str;



更多未定义的行为。

必须使用delete []释放由new []分配的内存。

此外,如果你只需要b $ b,你就可以避免许多这些错误使用std :: string而不是使用char *制作多个

搞砸了。


sp ******** @ gmail.com 写道:


# include< cstring>

#include< iostream>

使用命名空间std;


class X

{

public:

X();

X(char *);



X(char const);


~X();

void print();



void print()const;


>

private:

char * _str;

};


class Y:public X

{

public:

Y();

Y(char *);



Y(char const *);


~Y();

void print();



void print()const;


>

private:

char * _str;



提示:你已经在X中有一个_str


};


X :: X()

{

static char * str =" X's str empty" ;;



static char str [] =" X''s str empty" ;;


_str = new char [strlen(str)];



_str = new char [sizeof str];


strcpy(_str,str);



这会复制strlen(str)+ 1个字符! strlen()不计算

终止零。


为什么不使用std :: string类?


cout<< X(),''" << str<< "''\ n";

}


X :: X(char * str)



X :: X(char const * str)


{

_str = new char [strlen(str)] ;



_str = new char [strlen(str)+ 1];


strcpy(str,str );

cout<< X(char *),''" << str<< "''\ n";

}


X ::〜X()

{

cout<< ~X(),deallocating''" << _str<< "''\ n";

删除_str;

}


void X :: print()



void X :: print()const


{

cout< <英寸×::打印():\t" << _str<< "''\ n";

}


Y :: Y()


$ b这里调用$ b X :: X()。如果你不想这样,你可以这样做:


:X(Y'的空格)


[snip]几乎相等的代码]


int main()

{

X * x [3];


x [0] =新X(X'的数据索引0);

x [1] =新Y(Y) s data-index 1");


// Y y(" y'的数据索引2");

// x [ 2] =新Y(y);


x [0] - > print();

x [1] - > print();



这将打印X :: print()\ tX的str empty \ n"

http://www.parashift.com/c++-faq-lit ... functions.html


// x [2] - > print();


删除x [0];

删除x [1];



此行未定义行为:

http://www.parashift.com/c++-faq-lit....html#faq-20.7


//删除x [2];


返回0;

}



-

rbh


#include <cstring>
#include <iostream>
using namespace std;

class X
{
public:
X();
X( char * );
~X();
void print();

private:
char *_str;
};

class Y : public X
{
public:
Y();
Y( char * );
~Y();
void print();

private:
char *_str;
};

X::X()
{
static char *str = "X''s str empty";
_str = new char[ strlen( str ) ];
strcpy(_str, str);
cout << "X(), ''" << str << "''\n";
}

X::X( char *str )
{
_str = new char[ strlen( str ) ];
strcpy(str, str );
cout << "X(char *), ''" << str << "''\n";
}

X::~X()
{
cout << "~X(), deallocating ''" << _str << "''\n";
delete _str;
}

void X::print()
{
cout << "X::print():\t''" << _str << "''\n";
}

Y::Y()
{
static char *str = "Y''s str empty";
_str = new char[ strlen( str ) ];
strcpy(_str, str);
cout << "Y(), ''" << str << "''\n";
}

Y::Y(char *str)
{
_str = new char[ strlen( str ) ];
strcpy( _str, str );
cout << "Y(char *), ''" << str << "''\n";
}

Y::~Y()
{
cout << "~Y() deallocating ''" << _str << "''\n";
delete _str;
}

void Y::print()
{
cout << "Y::print():\t''" << _str << "''\n";
}

int main()
{
X *x[3];

x[0] = new X( "X''s data-index 0" );
x[1] = new Y( "Y''s data-index 1" );

// Y y( "y''s data-index 2" );
// x[2] = new Y( y );

x[0]->print();
x[1]->print();
// x[2]->print();

delete x[0];
delete x[1];
// delete x[2];

return 0;
}

解决方案

sp********@gmail.com wrote:

#include <cstring>
#include <iostream>
using namespace std;

class X
{
public:
X();
X( char * );
~X();
void print();

private:
char *_str;
};

[...more code...]
[Subject: Can anyone find the errors?]

Which one?

I guess, the biggest error is not using std::string.

--
Thomas
http://www.netmeister.org/news/learn2quote.html


sp********@gmail.com wrote:

Do you want to be more specific?

#include <cstring>
#include <iostream>
using namespace std;

Using declarations are generally bad.

>
class X
{
public:
X();
X( char * );
~X();

Classes that require destructors probably also need copy constructors
and copy assignment operators (rule of three). If you had done
anything that would have invoked copy semantics for your class
you would have had further problems.

void print();

Did you perhaps also want to make this function virtual? Hard to tell
from your program.

>
private:
char *_str;
};

>
X::X()
{
static char *str = "X''s str empty";

Deprecated conversion of const char array to char array.

_str = new char[ strlen( str ) ];
strcpy(_str, str);

WHOOP WHOOP... strcpy copies strlen(str) + 1 characters (the
null terminator is not counted by strlen. Undefined behavior
from writing off the end of the string.

cout << "X(), ''" << str << "''\n";

ERROR. iostream does not necessarily define ostream functions.
You need <ostreamincluded.

X::X( char *str )
{
_str = new char[ strlen( str ) ];
strcpy(str, str );

same bugs as before.

>
X::~X()
{
cout << "~X(), deallocating ''" << _str << "''\n";
delete _str;

More undefined behavior.
Memory allocated by new[] must be freed with delete[].
Further, you''d have avoided many of these bugs if you''d
have just used std::string rather than making multiple
screw ups with char*.


sp********@gmail.com wrote:

#include <cstring>
#include <iostream>
using namespace std;

class X
{
public:
X();
X( char * );

X(char const);

~X();
void print();

void print() const;

>
private:
char *_str;
};

class Y : public X
{
public:
Y();
Y( char * );

Y(char const *);

~Y();
void print();

void print() const;

>
private:
char *_str;

Hint: You already have a _str in X

};

X::X()
{
static char *str = "X''s str empty";

static char str[] = "X''s str empty";

_str = new char[ strlen( str ) ];

_str = new char[sizeof str];

strcpy(_str, str);

This copies strlen(str) + 1 characters! strlen() does not count the
terminating zero.

Why don''t you use the std::string class?

cout << "X(), ''" << str << "''\n";
}

X::X( char *str )

X::X(char const *str)

{
_str = new char[ strlen( str ) ];

_str = new char[strlen(str) + 1];

strcpy(str, str );
cout << "X(char *), ''" << str << "''\n";
}

X::~X()
{
cout << "~X(), deallocating ''" << _str << "''\n";
delete _str;
}

void X::print()

void X::print() const

{
cout << "X::print():\t''" << _str << "''\n";
}

Y::Y()

X::X() is called here. If you do not want this, you could do:

: X("Y''s str empty")

[snip almost equal code]

int main()
{
X *x[3];

x[0] = new X( "X''s data-index 0" );
x[1] = new Y( "Y''s data-index 1" );

// Y y( "y''s data-index 2" );
// x[2] = new Y( y );

x[0]->print();
x[1]->print();

This shall print "X::print()\tX''s str empty\n"

http://www.parashift.com/c++-faq-lit...functions.html

// x[2]->print();

delete x[0];
delete x[1];

This line is undefined behaviour:

http://www.parashift.com/c++-faq-lit....html#faq-20.7

// delete x[2];

return 0;
}

--
rbh


这篇关于任何人都可以找到错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆