C ++类字符串实现

时间:2016-04-09 02:12:41

标签: c++

我得到了两个文件,一个是.h文件,我必须添加方法/构造函数,另一个是.cpp文件,它将为我完成剩下的工作。

我添加了方法,并且尽我所能,然后我得到了一些帮助,但其余的它仍然无法正常工作。

  

我收到以下错误:

     

IntelliSense:没有运算符“==”匹配这些操作数类型是:const String1030 == String1030

     

错误C2676:binary'==':'const String1030'没有定义此运算符或转换为预定义可接受的类型   运营商
     错误C2572:'String1030 :: String1030':重新定义默认参数:参数1

我的代码:

 #ifndef STRING1030_H
    #define STRING1030_H 
    #include<iostream>

    using std::ostream;
    using std::istream;
    using std::endl;
    using std::cerr;

    /*
     * The class String1030 is for the students to practice implementing
     * more class functions and making sure that their implementation
     * is reasonable.
     *
     * It requires some skill in using arrays and overloading operators.
     *
     * Note that the sentinel value that MUST be part of the storage for our
     * strings is '\0'.  That is not special, it is just a way to tell future
     * readers that we know what we are doing. We could just as well use the 
     * digit 0, but that can be very confusing.
     */


    class String1030
    {       
      public:
          // The constructor. The "0" is the digit 0 NOT a 
          // character. It is used to let us know that 
          // nothing is being passed to the default constructor.
        String1030(const char *buf=0);
          //This next is a "copy" constructor. Remember that
          //we have to create new storage and then copy
          //the array content. We must not just copy the pointer.
        String1030(const String1030& oldstring);
          // The destructor is needed because we are allocating memory.
          // We must deallocate it when the object goes out of
          // scope (is destroyed).
        ~String1030();       
        String1030& operator=(const String1030& right);

          // Allows us to change the element at a certain index.
          // refer to the IntList code.
        char& operator[](int index);

          // Returns the number of characters stored excluding the
          // the sentinel value.
        int getSize(void) const;
          // Resizes the storage. Must include 1 extra space for the
          // sentinel value.
        void setSize(int newsize);
          // Returns a pointer to array storing the string.
        const char *getString();
          // Replace the existing string with a new one. 
        void setString(const char *carray);      
      private:
        char *buffer;
        int mysize;

    };

    // Basic constructor.
    //
    String1030 :: String1030(const char *buff=0): mysize(0), buffer(0)
    {
      setSize(*buff);
    }

    //copy constructor
    String1030::String1030(const String1030& oldstring) : mysize(0), buffer(0)
    {
        if (oldstring.getSize() <= 0) {
            setSize(0);
        }
        else {
            setSize(oldstring.getSize());
            for (int i = 0; i< mysize; i++) {
                buffer[i] = oldstring.buffer[i];
            }
        }
    }

    // Destructor call
    String1030::~String1030()
    {
      setSize(0);
    }

    String1030& String1030:: operator = (const String1030& right){
            // must take 'address of' the argument to compare it to the
            // this pointer.
            if (right == *this) {
                cerr << "Warning:  attempt to copy IntList onto self" << endl;
            }
            else {
                if (right.getSize() +1 <= 0) {
                    setSize(0);
                }
                else {
                    setSize(right.getSize());
                    for (int i = 0; i< mysize +1 ; i++) {
                        buffer[i] = right.buffer[i];
                    }
                }
            }
            return *this;  // dereference the pointer to get the object
        }

    char& String1030::operator[](int index)
    {
        if (index<0 || index >= mysize + 1) {
            cerr << "Attempt to access element outside index bounds"
                << endl;
            exit(4); // Maybe not reasonable but what the heck.
        }
        else {
            return buffer[index];
        }
    }

    int String1030::getSize()  const
    {
        return mysize;
    }

    void String1030::setSize(int newsize)
    {
        if ( newsize <0) {
            cerr << "Warning:  attempt to set a size < 0" << endl;
            exit(1);  // is this reasonable?
        }
        else {
            if (buffer != 0) {
                delete[] buffer;
                buffer = 0;
                mysize = 0;
            }
            if (newsize != 0) {
                buffer = new char[newsize+1];
                if (buffer == 0) {
                    mysize = 0;
                    cerr << "Warning:  unable to allocate enough space for list" << endl;
                    exit(2); 
                }
                else {
                    mysize = newsize;
                }
            }
        }       
    }

    // Returns a pointer to array storing the string.
    const char* String1030:: getString()
    {
        return    buffer;
    }

    // Replace the existing string with a new one. 
    void  String1030::setString(const char *carray)
    {

    int len = 0;
    for ( int tmp = 0; carray[tmp] != 0; tmp ++){
        len =  len + 1;
    }

    setSize(len);

    for(int i=0; i < len +1; i++){
        buffer[i] = carray[i];
    }       
}

#endif

以下是其他代码:

#include<iostream>
#include "String1030.h"

using std::cout;
using std::cin;

int main()
{  
  // check out the use of the constructors 
  String1030 s("My string");
  String1030 t(s);
  String1030 x;
  char in_buf[256];

  cout << "S size(): " << s.getSize() << endl;
  cout << "T size(): " << t.getSize() << endl;
  cout << "X size(): " << x.getSize() << endl;

  for(int i=0;i<t.getSize();i++)
    cout << t[i];
  cout << endl;

  // check the ability to modify one element of the string
  s[2]='5';

  for(int i=0;i<s.getSize();i++)
    cout << s[i];
  cout << endl;
  // check the assignment operator
  x=s;
  cout << "X: " << x.getString() << endl;

  // check the size reset.
  x.setSize(30);
  cout << "Input a string: ";
  cin >> in_buf;
  x.setString(in_buf);
  cout <<  "\nX: " << x.getString() << endl;

  //more checks on resize
  //set to a negative value, nothing should change
  s.setSize(-8);
  cout << "S size(): " << s.getSize() << endl;

  //set to 0, should be 0
  s.setSize(0);
  cout << "S size(): " << s.getSize() << endl;

  //read into the 0 length array should NOT have an error
  //and should NOT transfer any characters. Output should not
  //have any errors either.
  cout << "Input a string: ";
  cin >> in_buf;
  s.setString(in_buf);
  cout << "S after cin>>: " << s.getString() << endl;

  //reset to something larger than 0
  s.setSize(10);
  cout << "S size(): " << s.getSize() << endl;

  //read should work now
  cout << "Input a string: ";
  cin >> in_buf;
  s.setString(in_buf);
  cout << "S after cin>>: " << s.getString() << endl;

  //now the assignment return value    
  x=t=s;

  cout << "T: " << t.getString() << endl;
  cout << "X: " << x.getString() << endl;  
  return 0;

}

我不确定遗失了什么,老师说除了需要删除'&amp;'之外没关系在说运营商的部分=但它不起作用。不知道该怎么办。

2 个答案:

答案 0 :(得分:2)

阅读有关操作员过载的here

鉴于您的错误:

  

IntelliSense:没有运算符“==”匹配这些操作数类型是:const String1030 == String1030

这一行

(right == *this)

没有实现等于运算符,因此您需要实现一个:

bool operator==(const String1030 & lhs, const String1030& rhs){ 
     /* do actual comparison */ 
}

答案 1 :(得分:0)

如果您查看==行上方的行

// must take 'address of' the argument to compare it to the
// this pointer."
if (right == *this) {

您目前正在尝试将right的值与*this的值进行比较。您真正想要做的是将指针right(使用运算符&的地址)与this指针进行比较。

if (&right == this) {

然后您不需要实现值比较运算符(如果您在其他任何地方都不需要它)。