本文介绍了任何人都可以找到错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! #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; } 解决方案 [...更多代码...] [主题:任何人都可以找到错误吗?] 哪一个? 我猜,最大的错误是没有使用std :: string。 - Thomas http:// www。 netmeister.org/news/learn2quote.html 使用声明一般都不好。 需要析构函数的类可能还需要复制构造函数 和复制赋值运算符(三条规则)。如果你已经完成了任何会为你的班级调用复制语义的东西 你会遇到进一步的问题。 您是否也想将此功能设为虚拟?你的程序很难说 。 不推荐将const char数组转换为char数组。 WHOOP WHOOP ... strcpy复制strlen(str)+ 1个字符( null终止符不计入strlen。未定义的行为 来自写下字符串的结尾。 ERROR.iostream不一定定义ostream函数。 你需要< ostreamincluded。 和以前一样的错误。 > X ::〜X() { cout<< " ~X(),deallocating''"<< _str<<"''\ n" ;; delete _str; 更多未定义的行为。 必须使用delete []释放由new []分配的内存。 此外,如果你只需要b $ b,你就可以避免许多这些错误使用std :: string而不是使用char *制作多个 搞砸了。 X(char const); void print()const; Y(char const *); void print()const; 提示:你已经在X中有一个_str static char str [] =" X''s str empty" ;; _str = new char [sizeof str]; 这会复制strlen(str)+ 1个字符! strlen()不计算 终止零。 为什么不使用std :: string类? X :: X(char const * str) _str = new char [strlen(str)+ 1]; void X :: print()const $ b这里调用$ b X :: X()。如果你不想这样,你可以这样做: :X(Y'的空格) [snip]几乎相等的代码] 这将打印X :: print()\ tX的str empty \ n" http://www.parashift.com/c++-faq-lit ... functions.html 此行未定义行为: http://www.parashift.com/c++-faq-lit....html#faq-20.7 - 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;} 解决方案[...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.htmlUsing declarations are generally bad.Classes that require destructors probably also need copy constructorsand copy assignment operators (rule of three). If you had doneanything that would have invoked copy semantics for your classyou would have had further problems.Did you perhaps also want to make this function virtual? Hard to tellfrom your program.Deprecated conversion of const char array to char array.WHOOP WHOOP... strcpy copies strlen(str) + 1 characters (thenull terminator is not counted by strlen. Undefined behaviorfrom writing off the end of the string.ERROR. iostream does not necessarily define ostream functions.You need <ostreamincluded.same bugs as before.More undefined behavior.Memory allocated by new[] must be freed with delete[].Further, you''d have avoided many of these bugs if you''dhave just used std::string rather than making multiplescrew ups with char*.X(char const);void print() const;Y(char const *);void print() const;Hint: You already have a _str in Xstatic char str[] = "X''s str empty";_str = new char[sizeof str];This copies strlen(str) + 1 characters! strlen() does not count theterminating zero.Why don''t you use the std::string class?X::X(char const *str)_str = new char[strlen(str) + 1];void X::print() constX::X() is called here. If you do not want this, you could do:: X("Y''s str empty")[snip almost equal code]This shall print "X::print()\tX''s str empty\n" http://www.parashift.com/c++-faq-lit...functions.htmlThis line is undefined behaviour: http://www.parashift.com/c++-faq-lit....html#faq-20.7--rbh 这篇关于任何人都可以找到错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!