So many errors....

This is a discussion on So many errors.... within the C++ Programming forums, part of the General Programming Boards category; Hi all, I can't figure out why I'm getting the below errors. How is temp() in my overloaded operator+ function ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    121

    So many errors....

    Hi all,

    I can't figure out why I'm getting the below errors. How is temp() in my overloaded operator+ function not of type newString()? Why are there two parameter lists()() after their comments on that error line? Why did return temp; produce an invalid init of non-const ref error? Also, I don't see any errors in my overloaded operator>> function. Can someone point out my mistakes? Thanks!!!!

    -Patrick

    C:\Dev-Cpp\main.cpp In member function `newString& newString:perator+(const newString&)':
    96 C:\Dev-Cpp\main.cpp request for member `strPtr' in `temp', which is of non-class type `newString ()()'
    97 C:\Dev-Cpp\main.cpp invalid initialization of non-const reference of type 'newString&' from a temporary of type 'newString (*)()'
    C:\Dev-Cpp\main.cpp In member function `newString& newString:perator+=(const newString&)':
    104 C:\Dev-Cpp\main.cpp invalid initialization of non-const reference of type 'newString&' from a temporary of type 'newString (*)()'
    163 C:\Dev-Cpp\main.cpp ambiguous overload for 'operator>>' in 'std::cin >> str1'
    167 C:\Dev-Cpp\main.cpp [Warning] the address of `newString str1()', will always evaluate as `true'
    Code:
     #include <cstdlib>
    #include <iostream>
    #include <cassert>
    #include <cstring>
    #include <iomanip>
    
    using namespace std;
    
    class newString
    {
          
          friend ostream& operator<<(ostream&, const newString&);
          friend istream& operator>>(istream&, newString&);
          public:
                 const newString& operator=(const newString&);
                 newString(const char*);
                 newString();
                 newString(const newString&);
                 ~newString();
                 char& operator[] (int);
                 const char& operator[](int) const;
                 newString& operator+(const newString&);
                 newString& operator+=(const newString&);
                 bool operator==(const newString&) const;
                 bool operator!=(const newString&) const;
                 bool operator<=(const newString&) const;
                 bool operator<(const newString&) const;
                 bool operator>=(const newString&) const;
                 bool operator>(const newString&) const;
                
                 int length(const newString&);
                 
          private:
                  char *strPtr;
                  int strLength;
    };
    
    const newString& newString::operator=(const newString& rightString)
    {
          if(this != &rightString)
          {
                  delete []strPtr;
                  strLength = rightString.strLength+1;
                  strPtr = new char[strLength+1];
                  assert(strPtr != NULL);
                  strcpy(strPtr, rightString.strPtr);
          }
          return *this;
    }
                                               
    newString::newString(const char *myChar)
    {
                               strLength = strlen(myChar);
                               strPtr = new char[strLength+1];
                               
                               assert(strPtr != NULL);
                               strcpy(strPtr, myChar);
    }
    
    newString::newString()
    {
                          strLength = 0; 
                          strPtr = new char[1];
                          assert(strPtr != NULL);
                          strcpy(strPtr, "");
    }
    
    newString::newString(const newString& rightString)
    {
                                    strLength = rightString.strLength;
                                    strPtr = new char[strLength+1];
                                    assert(strPtr != NULL);
                                    strcpy(strPtr, rightString.strPtr);
    }
    
    newString::~newString()
    {
                           delete []strPtr;
    }
    
    char& newString::operator[] (int index)
    {
          assert(0 <= index && index < strLength);
          return strPtr[index];
    }
    
    const char& newString::operator[] (int index) const
    {
          assert(0 <= index && index < strLength);
          return strPtr[index];
    }
    
    newString& newString::operator+(const newString& rightString)
    {
               newString temp();
               temp.strPtr = strcat(strPtr, rightString.strPtr);
               return temp;
    }           
    
    newString& newString::operator+=(const newString& rightString)
    {
               newString temp();
               strcat(strPtr, rightString.strPtr);
               return temp;
    }           
    
    
    bool newString::operator==(const newString& rightString) const
    {
         return (strcmp(strPtr, rightString.strPtr) == 0);
    }
    
    bool newString::operator!=(const newString& rightString) const
    {
         return (strcmp(strPtr, rightString.strPtr) != 0);
    }
    
    bool newString::operator<=(const newString& rightString) const
    {
         return (strcmp(strPtr, rightString.strPtr) <= 0);
    }
    
    bool newString::operator<(const newString& rightString) const
    {
         return (strcmp(strPtr, rightString.strPtr) < 0);
    }
    
    bool newString::operator>=(const newString& rightString) const
    {
         return (strcmp(strPtr, rightString.strPtr) >= 0);
    }
    
    bool newString::operator>(const newString& myString) const
    {
         return (strcmp(strPtr, myString.strPtr) > 0);
    }
    
    int newString::length(const newString&)
    {
        return strLength;
    }
    
    ostream& operator<<(ostream& out, const newString& myString)
    {
             out << myString.strPtr;
             return out;
    }
    
    istream& operator>>(istream& in, newString& myString)
    {
             char temp[255];
             
             in >>setw(255) >> temp;
             myString = temp;
             return in;
    }
    
    int main()
    {
        newString str1(), str2(), str3();
       
        cout << "Enter string1: ";
        cin >> str1;
        cout << "Enter string2: ";
        cin >> str2;
        
        cout << "The string in string1 is " << str1 << ", and the value in string2 is " << str2 << endl;
        cout << "string1 + string2 produces ";
        str3 = str2+str1;
        cout << str3 << endl;
        
        system("PAUSE");
        
    }

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    >newString str1(), str2(), str3();

    Whenever you declare an instance of a class, do not add the parenthesis. It seems like this should call the default constructor, but the compiler actually sees a function declaration, so do this:
    Code:
    newString str1, str2, str3;
    This should fix your errors, but it looks like you've got some other problems as well, so pay attention to the warnings
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Quote Originally Posted by JaWiB View Post
    >newString str1(), str2(), str3();

    Whenever you declare an instance of a class, do not add the parenthesis. It seems like this should call the default constructor, but the compiler actually sees a function declaration, so do this:
    Code:
    newString str1, str2, str3;
    This should fix your errors, but it looks like you've got some other problems as well, so pay attention to the warnings
    Thanks, that did help... so, if I have a blank constructor, I'm not to include parenthesis when I declare an object; that will call the constructor with the empty parameter list? Also, I removed the parenthesis from the declaration newString temp(); , but am still getting:
    95 C:\Dev-Cpp\main.cpp [Warning] reference to local variable `temp' returned

    What does that mean?

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Now that it works, the following int main() code produces a garbage value, just some strange characters. Can anyone point me to my mistake?
    Code:
    newString str1, str2, str3;
       
        cout << "Enter string1: ";
        cin >> str1;
        cout << "Enter string2: ";
        cin >> str2;
        
        cout << "The string in string1 is " << str1 << ", and the value in string2 is " << str2 << endl;
        cout << "string1 + string2 produces ";
        str3 = str2+str1;
        cout << str3 << endl;

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    so, if I have a blank constructor, I'm not to include parenthesis when I declare an object; that will call the constructor with the empty parameter list?
    Yes.

    Also, I removed the parenthesis from the declaration newString temp(); , but am still getting:
    95 C:\Dev-Cpp\main.cpp [Warning] reference to local variable `temp' returned
    A reference must refer to something that exists. However, your local variable no longer exists once the function returns control. So, what you should do is to change operator+ to return a newString instead of a newString&, and have operator+= modify the current object, upon which you can return *this.

    EDIT:
    There is more, actually. You should probably make operator+ a free function instead:
    Code:
    newString operator+(const newString& lhs, const newString& rhs)
    {
        return newString(lhs) += rhs;
    }
    Last edited by laserlight; 04-25-2007 at 07:14 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Thanks! Excuse the dumb questions; I'm on my second data structs book, because the first one confused me so much, but... when would I ever have newString& as a return type? Is there a case where I would return a reference to a newString object like that?

    -Patrick

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Sorry, one more thing... just so I'm clear on this, why is, for example, below, the newString object passed by reference, and not by value if it's not being modified directly?
    Code:
     
    bool operator==(const newString&) const;

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    903
    Passing by reference is faster and eats less memory, if I recall, because no copy is made.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    when would I ever have newString& as a return type? Is there a case where I would return a reference to a newString object like that?
    It is useful for operator chaining. As a typical example, the overloaded operator<< for ostreams returns an ostream& so that we can write code like:
    Code:
    std::cout << a << b << c;
    Another reason would be to reduce copies in operator chaining, which is why your copy assignment operator returns a const reference to the current object instead of returning it by value (though typically it returns a reference instead of a const reference, even though there are advantages to returning a const reference).

    why is, for example, below, the newString object passed by reference, and not by value if it's not being modified directly?
    Desolation is right, but in particular it is passed as a const reference, so one would not expect it to be modified in the funcion. Either way (by const reference or by value), the caller would be assured that whatever was passed would not be modified with respect to it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jun 2006
    Posts
    121

    Cool

    Thanks for the explanations and patience, guys! My new text is much better but still skips a lot of details,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM
  4. Help me with these errors... :-(
    By major_small in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2003, 08:18 PM
  5. errors in class(urgent)
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2001, 05:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21