-1

这是整个程序,请帮助我,我已经尝试了一切来找出内存到底发生了什么。问题是一切都运行得很好,但是输出时打印了一些额外的字符。

这是.h文件:

 class MyString
 {
    public:
            MyString();
            MyString(const char *message);
            MyString(const MyString &source);
            ~MyString();
            const void Print() const;
            const int Length() const;
            MyString& operator()(const int index, const char b);
            char& operator()(const int i);

            MyString& operator=(const MyString& rhs);
            bool operator==(const MyString& other) const;
            bool operator!=(const MyString& other) const;
            const MyString operator+(const MyString& rhs) const;
            MyString& operator+=(const MyString& rhs);
            friend ostream& operator<<(ostream& output, const MyString& rhs);
            const int Find(const MyString& other);
            MyString Substring(int start, int length);

    private:
            char *String;
            int Size;


 };

 istream& operator>>(istream& input, MyString& rhs);

.cpp 文件:

 MyString::MyString()
 {
    char temp[] = "Hello World";

    int counter(0);
    while(temp[counter] != '\0')
    {
            counter++;
    }
    Size = counter;
    String = new char [Size];
    for(int i=0; i < Size; i++)
            String[i] = temp[i];

 }

 //alternate constructor that allows for setting of the inital value of the string

  MyString::MyString(const char *message)
  {
    int counter(0);
    while(message[counter] != '\0')
    {
            counter++;
    }
    Size = counter;
    String = new char [Size];
    for(int i=0; i < Size; i++)
            String[i] = message[i];
 }

 //copy constructor
 MyString::MyString(const MyString &source)
 {

    int counter(0);
    while(source.String[counter] != '\0')
    {
       counter++;
    }
    Size = counter+1;
    String = new char[Size];
    for(int i = 0; i <= Size; i++)
            String[i] = source.String[i];


 }

 //Deconstructor
 MyString::~MyString()
 {
    delete [] String;
 }

 //Length() method that reports the length of the string
 const int MyString::Length() const
 {
    int counter(0);

    while(String[counter] != '\0')
    {
            counter ++;
    }
    return (counter);
 }

 /*Parenthesis operator should be overloaded to replace the Set and Get functions of  your previous assignment. Note that both instances should issue exit(1) upon violation of the string array bounaries.
 */

    MyString& MyString::operator()(const int index, const char b)
    {
    if(String[index] == '\0')
    {
            exit(1);
    }
    else
    {
            String[index] = b;
    }
 }

 char& MyString::operator()(const int i)
 {
    if(String[i] == '\0')
    {
            exit(1);
    }
    else
    {
            return String[i];
  }
 }
 /*Assignment operator (=) which will copy the source string into the destination string. Note that size of the destination needs to be adjusted to be the same as the source.
 */

 MyString& MyString::operator=(const MyString& rhs)
 {
    if(this != &rhs)
    {
            delete [] String;
            String = new char[rhs.Size];
            Size = rhs.Size;

            for(int i = 0; i < rhs.Size+1 ; i++)
            {
                    String[i] = rhs.String[i];
            }
    }

    return *this;
 }
 /*Logical comparison operator (==) that returns true iff the two strings are identical in size and contents.
 */

  bool MyString::operator==(const MyString& other)const
  {
    if(other.Size == this->Size)  {         
        for(int i = 0; i < this->Size+1; i++)
            {
                    if(&other == this)
                            return true;
            }
    }
    else
            return false;
 }

 //Negated logical comparison operator (!=) that returns boolean negation of 2

 bool MyString::operator!=(const MyString& other) const
 {
    return !(*this == other);
 }

 //Addition operator (+) that concatenates two strings

 const MyString MyString::operator+(const MyString& rhs) const
 {
    char* tmp = new char[Size + rhs.Size +1];


    for(int i = 0; i < Size; i++)
    {
            tmp[i] = String[i];
    }
    for(int i = 0; i < rhs.Size+1; i++)   {           
        tmp[i+Size] = rhs.String[i];
    }

    MyString result;

    delete [] result.String;
    result.String = tmp;
    result.Size = Size+rhs.Size;

    return result;
 }
 /*Addition/Assigment operator (+=) used in the following fashion: String1 += String2 to operate as String1 = String1 + String2
 */

 MyString& MyString::operator+=(const MyString& rhs)
  {
    char* tmp = new char[Size + rhs.Size + 1];

    for(int i = 0; i < Size; i++)        {
            tmp[i] = String[i];
    }
    for(int i = 0; i < rhs.Size+1; i++)
    {
            tmp[i+Size] = rhs.String[i];
    }

    delete [] String;
    String = tmp;
    Size += rhs.Size;

    return *this;
 }

 istream& operator>>(istream& input, MyString& rhs)
 {
    char* t;
    int size(256);
    t = new char[size];
    input.getline(t,size);

    rhs = MyString(t);
    delete [] t;

    return input;
 }

 ostream& operator<<(ostream& output, const MyString& rhs)
 {
    if(rhs.String != '\0')
    {
            output << rhs.String;
    }
    else
    {
            output<<"No String to output\n";
    }

    return output;
 }






 /*MyString::Find that finds a string in a larger string and returns the starting location of the substring. Note that your string location starts from 0 and ends at length -1. If the string is not found, a value of -1 will be returned
 */

 const int MyString::Find(const MyString& other)
 {

    int nfound = -1;

    if(other.Size > Size)
    {
            return nfound;
    }   
      int i = 0, j = 0;      
      for(i = 0; i < Size; i++)    {            
        for(j = 0; j < other.Size; j++)  {            
             if( ((i+j) >= Size) || (String[i+j] != other.String[j]) )

                    {
                            break;
                    }

            }

            if(j == other.Size)
            {

                    return i;

            }

      }


    return nfound;
 }
 /*MyString::Substring(start, length). This method returns a substring of the original string that contains the same characters as the original string starting at location start and is as long as length.
 */

 MyString MyString::Substring(int start, int length)
 {
    char* leo = new char[length+1];
    for(int i = start; i < start + length+1; ++i)
    {
            leo[i-start] = String[i];
    }

    MyString sub;
    delete [] sub.String;        sub.String = leo;        sub.Size = Size;
    return sub;
 }

  //Print() method that prints the string

 const void MyString::Print() const
 {

    for(int i=0; i < Size; i++)
    {
            cout<<String[i];
    }
    cout<<endl;
 }

main.cpp 文件:

 int main (int argc, char **argv)
 {

   MyString String1; 

  const MyString ConstString("Target string");    //Test of alternate constructor

   MyString SearchString;  //Test of default constructor that should set "Hello World".

 MyString TargetString (String1); //Test of copy constructor


   cout << "Please enter two strings. ";
  cout << "Each string needs to be shorter than 256 characters or terminated by /\n." << endl;
 cout << "The first string will be searched to see whether it contains exactly the second string. " << endl;

  cin >> SearchString >> TargetString; // Test of cascaded string-extraction operator






   if(SearchString.Find(TargetString) == -1) {

    cout << TargetString << " is not in " << SearchString << endl;
    }

  else {

    cout << TargetString << " is in " << SearchString << endl;

    cout << "Details of the hit: " << endl;

    cout << "Starting position of the hit: " << SearchString.Find(TargetString) << endl;
    cout << "The matching substring is: " << SearchString.Substring(SearchString.Find(TargetString), TargetString.Length()-1)<<"\n";
    }
  return 0;

 }

运行程序你得到这个:

请输入两个字符串。每个字符串必须少于 256 个字符或以 / 结尾。将搜索第一个字符串以查看它是否正好包含第二个字符串。

第一

真实的

房地产世界不在第一

请帮忙!!

4

2 回答 2

0

@Sam 的回答是正确的。我将添加它以帮助您了解正在发生的事情。

C 和 C++ 字符串实际上是遵循字符串以 终止的约定的字符数组\0,有时称为 NUL(空),它是一个所有位都为 0 的字符。

您的代码的第一部分是正确的,因为它创建了一个字符数组。但是,您不应用字符串必须以 NUL 终止的约定。

然后,您将不遵循 NUL 终止约定的字符串传递给cout,它确实遵循该约定。换句话说,它遍历字符串,将每个字符打印到stdout,直到它发生\0在内存中的字符上。它终止实际上是相当幸运的。如果它输出的字符数组中没有 a \0,它将继续运行,直到到达不属于您的程序的内存地址并因分段错误而失败。

于 2012-07-17T00:31:46.610 回答
0

MyString::MyString(const char *message)尝试在构造函数的字符串末尾添加 '\0'

于 2012-07-16T15:52:54.090 回答