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");
}