运算符+重载

use*_*061 0 c++ operator-overloading concatenation add

好吧,我是操作员重载的新手,我必须在面向对象的程序中执行此功能,但我绝对需要帮助.以下是说明:

MyString对象应包含打印字符串的Print()方法

MyString对象应包含一个Length()方法,该方法报告字符串的长度

MyString对象应该包含一个默认构造函数,它将初始字符串设置为"Hello World"的值.

MyString对象应包含一个备用构造函数,允许设置字符串的初始值.

MyString对象应该重载以下运算符:

  • 应重载括号运算符以替换先前分配的Set和Get函数.请注意,两个实例都应在违反字符串数组bounaries时发出exit(1).

  • 赋值运算符(=)将源字符串复制到目标字符串中.请注意,目标的大小需要调整为与源相同.

  • 逻辑比较运算符(==)如果两个字符串的大小和内容相同则返回true.

  • 返回布尔否定为2的否定逻辑比较运算符(!=).

  • 连接运算符(+),用于连接两个字符串

  • 以下方式使用的加法/分配运算符(+ =):String1 + = String2作为String1 = String1 + String2运行

  • 加法(+)和赋值(=)运算符都需要能够进行级联操作.这意味着String3 = String1 + String2,或String1 = String2 = String3应该工作.

这是我在.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];

}

MyString::MyString(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];
 }

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

 int MyString::Length()
 {
       int counter(0);

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

       return (counter);
  }

**const MyString operator +(const MyString& one, const MyString& two)
{
       MyString String1;
       return String1;
 }**



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


 }

MyString& MyString::operator=(const MyString& rhs)
{

        Size = rhs.Size;
        counter = rhs.counter;

        delete [] String;
        String = new char[Size];


        for(int i = 0; i < counter+1 ; i++)
       {
               String[i] = rhs.String[i];
       }
       return *this;

 }

 bool MyString::operator==(const MyString& one)
 {
       if(one.Length() == two.Length())
       {
              for(int i = 0; i < one.Length()+1; i++)
              {
                      if(one[i] == two[i])
                            return true;
              }
       }
       else
             return false;
 }

 MyString& MyString::operator()(const int i)
 {

        if( String[i] == '\0')
        {
              exit(1);
        }
        else
       {

            return String[i];

       }
 }

void MyString::Print()
{
       for(int i=0; i < Size; i++)
               cout << String[i];
       cout << endl;

}
Run Code Online (Sandbox Code Playgroud)

这是我在主文件中的代码:

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

 MyString String1;             //Test of default constructor. Use "Hello world"
 MyString String2 ("Two strings are not equal");       //Test of alternate constructor
 MyString String3 ("Two strings are equal");
 MyString String4 (String1);

 cout << "*************Test of values*************" << endl;
 String1.Print ();
 String2.Print ();
 String3.Print ();

 cout << "*************Test of Length*************" << endl;
 cout << String1.Length () << " ";
 cout << String2.Length () << " ";
 cout << String3.Length () << endl;

 cout << "*************Test of Set*************" << endl;
 String1 (0, 'J');
 String1.Print ();

 cout << "*************Test of Copy*************" << endl;
 String1.Print ();
 cout << endl;
 String3.Print ();
 cout << endl;
 String3.Copy (String1);       //String1 should be copied into String3
 String1.Print ();
 cout << endl;
 String3.Print ();
 cout << endl;

 cout << "*************Test of Get*************" << endl;
 for (int i = 0; i < String1.Length (); i++)   //The last character should exit the    program
   {
      cout << String1 (i) << " ";
   }
  }
   cout << endl;

   if (String1 == String4)
   {
      String3.Print ();
   }
   else
   {
     String4.Print ();
   }

   if (String1 != String4)
   {
      String3.Print ();
   }
   else
   {
      String4.Print ();
    }

   String1 = String2 = String3;
   String1.Print ();
   String2.Print ();
   String3.Print ();


   String1 = String2 + String3 + String4;
   String1.Print ();

   String2 += String3;
   String2.Print ();

   return 0;

 }
Run Code Online (Sandbox Code Playgroud)

main.cpp文件无法更改,但另一个.cpp文件必须与该文件一起编译和运行.

jua*_*nza 7

您需要将操作符的声明放在标题中,但您遇到的问题是您operator+在字符串中使用的operator+是字符串

const MyString operator +(const MyString& one, const MyString& two) {
        MyString String1 = one + two; // this calls operator+, which calls operator+, which calls...
        return String1;
}
Run Code Online (Sandbox Code Playgroud)

此外,由于您返回的是MyStringby值,因此不应返回const:

MyString operator +(const MyString& one, const MyString& two) {
        MyString String1;
        // do something with the data of one and two and put it in String1
        return String1;
}
Run Code Online (Sandbox Code Playgroud)

最后,如果您的运营商需要访问非公开数据MyString,您应该将其声明为您MyString班级的朋友.

class MyString {

// as before

friend MyString operator+(const Mytring& rhs, const MyString& lhs);

};
Run Code Online (Sandbox Code Playgroud)