如何更正称为"mystring"的类代码错误 [英] how to correct the Class code error called “mystring”

查看:54
本文介绍了如何更正称为"mystring"的类代码错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<pre lang="msil">// mystring.h -- class definition<br />
#include <iostream><br />
#include<string><br />
using namespace std;<br />
class String<br />
{<br />
private:<br />
    char * str;             // pointer to string<br />
    int len;                // length of string<br />
    static int num_strings;  // number of objects<br />
    static const int CINLIM = 80;   // cin input limit<br />
public:<br />
    // constructors and other methods<br />
    String(const char * s);       // constructor<br />
    String();               // default constructor<br />
    String(const String &);   // copy constructor<br />
    ~String();              // destructor<br />
    int length () const { return len; }<br />
    // overloaded operator methods<br />
    String & operator=(const String &);<br />
    String & operator=(const char *);<br />
    char & operator[](int i);<br />
    const char & operator[](int i) const;<br />
    // overloaded operator friends<br />
    friend bool operator<(const String &st, const String &st2);<br />
    friend bool operator>(const String &st1, const String &st2);<br />
    friend bool operator==(const String &st, const String &st2);<br />
    friend ostream & operator<<(ostream & os, const String & st);<br />
    friend istream & operator>>(istream & is, String & st);<br />
    // static function<br />
    static int HowMany();<br />
};<br />
// mystring.cpp -- String class methods<br />
// 初始化静态类成员num_strings<br />
int String::num_strings = 0;<br />
// static method<br />
int String::HowMany()<br />
{<br />
    return num_strings;<br />
}<br />
// class methods,要求动态分配字符串内存空间<br />
String::String(const char * s)<br />
{<br />
    len = strlen(s);<br />
    str = new char[len];<br />
    str = static_cast<char*>(s);<br />
}<br />
String::String()<br />
{<br />
    len = 4;<br />
    str = new char[1];<br />
    str[0] = ''\0'';<br />
    num_strings++;<br />
}<br />
String::String(const String & st)<br />
{<br />
    *this(st.str);<br />
}<br />
String::~String()                     // necessary destructor<br />
{<br />
    delete str;<br />
}<br />
// overloaded operator methods<br />
// assign a String to a String<br />
String & String::operator=(const String & st)<br />
{<br />
    if (this == &st)<br />
        return st;<br />
    delete [] str;<br />
    len = st.len;<br />
    return st;<br />
}<br />
// assign a C string to a String<br />
String & String::operator=(const char * s)<br />
{<br />
    delete [] str;<br />
    len = std::strlen(s);<br />
    str = new char[len + 1];<br />
    std::strcpy(str, s);<br />
    return *this;<br />
}<br />
// read-write char access for non-const String<br />
char & String::operator[](int i)<br />
{<br />
    return str[i];<br />
}<br />
// read-only char access for const String<br />
const char & String::operator[](int i) const<br />
{<br />
    return static_cast<const char>(str[i]);  //此处与上一空内容一样<br />
}<br />
// overloaded operator friends<br />
bool operator<(const String &st1, const String &st2)<br />
{<br />
    return (std::strcmp(st1.str, st2.str) < 0);<br />
}<br />
bool operator>(const String &st1, const String &st2)<br />
{<br />
    return st2.str < st1.str;<br />
}<br />
bool operator==(const String &st1, const String &st2)<br />
{<br />
    return st1.str == st2.str;<br />
}<br />
// simple String output<br />
ostream & operator<<(ostream & os, const String & st)<br />
{<br />
    os <<  st.str <<" " << st.len << " " << st.num_strings << std::endl;<br />
}<br />
// quick and dirty String input<br />
istream & operator>>(istream & is, String & st)<br />
{<br />
    is >> st.len >> st.str;<br />
}</pre><br />


谢谢您的帮助!


thank fore your help !

推荐答案

String & String::operator=(const String & st)
{
    if (this == &st)
        return st;
    delete [] str;
    len = st.len;

    // TODO: allocate and assign this->str here

    return *this; // Return this

}



您不能将const对象作为非const返回. (反正也不是没有强制转换.)



You cannot return a const object as non-const. (Not without a cast anyway.)


String & String::operator=(const String & st)
{
    if (this == &st)
        return st;
    delete [] str;
    len = st.len;
    return st;
}


此处出现多个错误:

1.当需要非常量引用时,语句return st尝试返回常量引用.

2.不仅如此,您还返回了错误的对象.通常的共识是方法operator=()始终返回对分配给该对象的引用.只需使用return *this;即可.

3.删除str,但不要再次分配它.您应该在此处复制st.str.

而且...


Multiple errors here:

1. the statements return st try to return a const reference when a non-const reference was required.

2. Not only that, you are also returning the wrong object. The general consensus is that the method operator=() always returns a reference to the object that is being assigned to. Just use return *this; instead.

3. you delete str, but do not assign it again. You should copy st.str here.

Moreover ...

String::String(const char * s)
{
    len = strlen(s);
    str = new char[len];
    str = static_cast<char*>(s);
}


同样,这里有多个错误:

1.您没有检查s是否为Null指针.如果您呼叫strlen,它将崩溃.

2.第三行将覆盖从第2行指向新分配的内存的指针,因此该内存将丢失-您创建了内存泄漏.

3. static_cast是此处错误的类型转换;如果您的目标是抛弃const,请使用const_cast

4.也就是说,您也不应使用const_cast,例如,使用强制转换通常仅表示您做错了事.大多数时候,它所做的只是抑制编译器错误或警告,而不是修复错误的代码.其次,您不应在此处复制指针,而应将s的内容复制到新创建的字符串e中. G.与strcpy()

还有一件事:不要使用标准的序列化运算符编写num_strings -它是静态变量,因此是类的属性,而不是此类的实例的属性.如果要写入该值,请在类外进行.

顺便说一句,并不是所有的构造函数都递增num_strings,所以该值可能不正确.


Again, multiple errors here:

1. You did not check whether s is a Null pointer. your call to strlen will crash if it is.

2. the third line will overwrite the pointer to the newly allocated memory from line 2, the memory will thus be lost - you created a memory leak.

3. static_cast is the wrong type of cast here; if your goal is to cast away const, then use const_cast

4. That said, you shouldn''t use const_cast either - for one, using a cast is usally just an indication that you''re doing something wrong. Most of the time, all it does is suppress compiler errors or warnings, not fix the erroneous code. Second, you should not copy the pointer here, but copy the contents of s to the newly created string, e. g. with strcpy()

One more thing: do not write num_strings with the standard serialization operators - it is a static variable, and as such a property of the class, not a property of the instances of this class. If you want to write that value, do it outside the class.

As an aside, not all of your constructors increment num_strings, so this value will likely not be correct.


好,我更改了一些错误,并且一些测试还可以! br/>
ok, i change some of the errors , and some tests are ok!
<pre lang="msil">// mystring.h -- class definition<br />
#include <iostream><br />
#include<string><br />
using namespace std;<br />
class String<br />
{<br />
private:<br />
    char * str;             // pointer to string<br />
    int len;                // length of string<br />
    static int num_strings;  // number of objects<br />
    static const int CINLIM = 80;   // cin input limit<br />
public:<br />
    // constructors and other methods<br />
    String(const char * s);       // constructor<br />
    String();               // default constructor<br />
    String(const String &);   // copy constructor<br />
    ~String();              // destructor<br />
    int length () const { return len; }<br />
    // overloaded operator methods<br />
    String & operator=(const String &);<br />
    String & operator=(const char *);<br />
    char & operator[](int i);<br />
    const char & operator[](int i) const;<br />
    // overloaded operator friends<br />
    friend bool operator<(const String &st, const String &st2);<br />
    friend bool operator>(const String &st1, const String &st2);<br />
    friend bool operator==(const String &st, const String &st2);<br />
    friend ostream & operator<<(ostream & os, const String & st);<br />
    friend istream & operator>>(istream & is, String & st);<br />
    // static function<br />
    static int HowMany();<br />
};<br />
// mystring.cpp -- String class methods<br />
// 初始化静态类成员num_strings<br />
int String::num_strings = 0;<br />
// static method<br />
int String::HowMany()<br />
{<br />
    return num_strings;<br />
}<br />
// class methods,要求动态分配字符串内存空间<br />
String::String(const char * s)<br />
{<br />
    len = strlen(s);<br />
    if(!s) str = 0;<br />
    else<br />
    {<br />
        str = new char[len+1];<br />
        strcpy(str,s);<br />
    }<br />
}<br />
String::String()<br />
{<br />
    len = 4;<br />
    str = new char[1];<br />
    str[0] = ''\0'';<br />
    num_strings++;<br />
}<br />
String::String(const String & st):len(st.len)<br />
{<br />
    if(!st.str) str = 0;<br />
    else<br />
    {<br />
        str = new char[len+1];<br />
        strcpy(str,st.str);<br />
    }<br />
}<br />
String::~String()                     // necessary destructor<br />
{<br />
    delete str;<br />
}<br />
// overloaded operator methods<br />
// assign a String to a String<br />
String & String::operator=(const String & st)<br />
{<br />
    if (this == &st)<br />
        return *this;<br />
    delete [] str;<br />
    len = st.len;<br />
    if(!st.str) str = 0;<br />
    else<br />
    {<br />
        str = new char[len + 1];<br />
        std::strcpy(str, st.str);<br />
    }<br />
    return *this;<br />
}<br />
// assign a C string to a String<br />
String & String::operator=(const char * s)<br />
{<br />
    delete [] str;<br />
    len = std::strlen(s);<br />
    if(!s) str = 0;<br />
    else<br />
    {<br />
        str = new char[len + 1];<br />
        std::strcpy(str, s);<br />
    }<br />
    return *this;<br />
}<br />
// read-write char access for non-const String<br />
char & String::operator[](int i)<br />
{<br />
    if(i >=0 && i <= strlen(str))<br />
       return str[i];<br />
}<br />
// read-only char access for const String<br />
const char & String::operator[](int i) const<br />
{<br />
    return static_cast<const char>(str[i]);  //此处与上一空内容一样<br />
}<br />
// overloaded operator friends<br />
bool operator<(const String &st1, const String &st2)<br />
{<br />
    return (std::strcmp(st1.str, st2.str) < 0);<br />
}<br />
bool operator>(const String &st1, const String &st2)<br />
{<br />
    return (std::strcmp(st1.str, st2.str) > 0);<br />
}<br />
bool operator==(const String &st1, const String &st2)<br />
{<br />
    if(strlen(st1.str) != strlen(st2.str))<br />
        return false;<br />
    return strcmp(st1.str,st2.str) ? false:true;<br />
}<br />
// simple String output<br />
ostream & operator<<(ostream & os, const String & st)<br />
{<br />
    os <<  st.str <<" " << st.len << " " << st.num_strings << std::endl;<br />
    return os;<br />
}<br />
// quick and dirty String input<br />
istream & operator>>(istream & is, String & st)<br />
{<br />
    is >> st.len >> st.str;<br />
    return is;<br />
}<br />
int main()<br />
{<br />
   String s1 = "abd";<br />
   String s2 = s1;<br />
   cout << s2[1];<br />
}</pre><br />


这篇关于如何更正称为"mystring"的类代码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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