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

thank fore your help !
Posted
Comments
Olivier Levrey 10-May-11 8:00am    
What is the problem? Compilation? Runtime?
We can't guess what is wrong...
boyhailong 10-May-11 8:18am    
many Implementation of the class has errors
Rhuros 10-May-11 8:13am    
A bit more info on the error would be good...
boyhailong 10-May-11 8:20am    
one is :
String & String::operator=(const String & st)
{
if (this == &st)
return st;
delete [] str;
len = st.len;
return st;
}
error C2440: “return”: 无法从“const String”转换为“String &”
Olivier Levrey 10-May-11 8:26am    
Could you please translate into english before posting?

C++
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

}


You cannot return a const object as non-const. (Not without a cast anyway.)
 
Share this answer
 
String & String::operator=(const String & st)
{
    if (this == &st)
        return st;
    delete [] str;
    len = st.len;
    return st;
}

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);
}

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.
 
Share this answer
 
Comments
boyhailong 11-May-11 2:18am    
what you said is beneficial ,thanks!
ok, i change some of the errors , and some tests are ok!
MSIL
// mystring.h -- class definition
#include <iostream>
#include<string>
using namespace std;
class String
{
private:
    char * str;             // pointer to string
    int len;                // length of string
    static int num_strings;  // number of objects
    static const int CINLIM = 80;   // cin input limit
public:
    // constructors and other methods
    String(const char * s);       // constructor
    String();               // default constructor
    String(const String &);   // copy constructor
    ~String();              // destructor
    int length () const { return len; }
    // overloaded operator methods
    String & operator=(const String &);
    String & operator=(const char *);
    char & operator[](int i);
    const char & operator[](int i) const;
    // overloaded operator friends
    friend bool operator<(const String &st, const String &st2);
    friend bool operator>(const String &st1, const String &st2);
    friend bool operator==(const String &st, const String &st2);
    friend ostream & operator<<(ostream & os, const String & st);
    friend istream & operator>>(istream & is, String & st);
    // static function
    static int HowMany();
};
// mystring.cpp -- String class methods
// 初始化静态类成员num_strings
int String::num_strings = 0;
// static method
int String::HowMany()
{
    return num_strings;
}
// class methods,要求动态分配字符串内存空间
String::String(const char * s)
{
    len = strlen(s);
    if(!s) str = 0;
    else
    {
        str = new char[len+1];
        strcpy(str,s);
    }
}
String::String()
{
    len = 4;
    str = new char[1];
    str[0] = '\0';
    num_strings++;
}
String::String(const String & st):len(st.len)
{
    if(!st.str) str = 0;
    else
    {
        str = new char[len+1];
        strcpy(str,st.str);
    }
}
String::~String()                     // necessary destructor
{
    delete str;
}
// overloaded operator methods
// assign a String to a String
String & String::operator=(const String & st)
{
    if (this == &st)
        return *this;
    delete [] str;
    len = st.len;
    if(!st.str) str = 0;
    else
    {
        str = new char[len + 1];
        std::strcpy(str, st.str);
    }
    return *this;
}
// assign a C string to a String
String & String::operator=(const char * s)
{
    delete [] str;
    len = std::strlen(s);
    if(!s) str = 0;
    else
    {
        str = new char[len + 1];
        std::strcpy(str, s);
    }
    return *this;
}
// read-write char access for non-const String
char & String::operator[](int i)
{
    if(i >=0 && i <= strlen(str))
       return str[i];
}
// read-only char access for const String
const char & String::operator[](int i) const
{
    return static_cast<const char>(str[i]);  //此处与上一空内容一样
}
// overloaded operator friends
bool operator<(const String &st1, const String &st2)
{
    return (std::strcmp(st1.str, st2.str) < 0);
}
bool operator>(const String &st1, const String &st2)
{
    return (std::strcmp(st1.str, st2.str) > 0);
}
bool operator==(const String &st1, const String &st2)
{
    if(strlen(st1.str) != strlen(st2.str))
        return false;
    return strcmp(st1.str,st2.str) ? false:true;
}
// simple String output
ostream & operator<<(ostream & os, const String & st)
{
    os <<  st.str <<" " << st.len << " " << st.num_strings << std::endl;
    return os;
}
// quick and dirty String input
istream & operator>>(istream & is, String & st)
{
    is >> st.len >> st.str;
    return is;
}
int main()
{
   String s1 = "abd";
   String s2 = s1;
   cout << s2[1];
}
 
Share this answer
 
but i don't know the use of
num_strings
clearly ,who can tell me ?
 
Share this answer
 
Comments
Legor 11-May-11 4:02am    
Dont add your comments as solutions. Update your question instead.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900