2016-04-09 10 views
-1

Ich bekam zwei Dateien, eine mit einer .h-Datei, die ich auch Methoden/Konstruktoren hinzufügen musste, und die andere eine .cpp-Datei, die den Rest für mich tun würde.C++ Class String-Implementierung

Ich fügte die Methoden und so gut wie ich konnte hinzu, und dann habe ich etwas Hilfe mit einigen, aber dann mit dem Rest hat es immer noch nicht funktioniert.

I get the following errors:

IntelliSense: no operator "==" matches these operand types are: const String1030 == String1030

error C2676: binary '==' : 'const String1030' does not define this operator or a conversion to a type acceptable to the predefined operator
error C2572: 'String1030::String1030' : redefinition of default parameter : parameter 1

mein Code:

#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 

Hier ist die andere Code:

#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; 

} 

Ich bin nicht sicher, was fehlt, sagte der Lehrer es in Ordnung, ausnehmend war brauchen um den '&' auf dem Teil zu entfernen, der Operator = sagt, aber es hat nicht funktioniert. Ich weiß nicht, was ich machen soll.

+0

Bitte nehmen Sie sich einen Blick auf http://stackoverflow.com/help/how-to-ask und http://stackoverflow.com/help/mcve –

+0

Darin heißt es auch ziemlich klar, wenn Sie Fragen haben ** "Wenn Sie Versteh das nicht, komm, sieh mich oder die TA. "** Ich würde dir empfehlen, genau das zu tun. –

+0

Ich denke, Sie sollten den Ratschlägen in den Code-Kommentaren folgen. ** Wenn Sie nichts davon verstehen, kommen Sie zu mir oder dem TA. ** * Ich * in diesem Kommentar bezieht sich nicht auf * die Benutzer von SO *. –

Antwort

2

Lesen Sie here über die Überlastung des Bedieners.

Ihre Fehler Gegeben:

IntelliSense: no operator "==" matches these operand types are: const String1030 == String1030

Und diese Linie

(right == *this) 

Es gibt keine Umsetzung der Gleichheitsoperator ist, so dass Sie ein implementieren müssen:

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

Wenn Sie schauen In der Zeile über der == Zeile heißt es

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

Sie versuchen derzeit, den Wert right mit dem Wert *this zu vergleichen. Was Sie wirklich wollen, ist der Zeiger zu right (mit der Adresse des Betreibers &) mit dem this Zeiger zu vergleichen.

Dann müssen Sie den Wertvergleichsoperator nicht implementieren (wenn Sie ihn nirgendwo anders benötigen).

+0

Das dachte ich auch und dann sagte mir der Lehrer, dass ich das & nicht brauchte, und so entfernte ich es ... Es funktioniert nicht mit oder ohne dass es so ist. – Frostypine