So I know my code is a mess. I have an overdue homework assignment for operator overloading and I have been failing miserably and trying to make it work.
The gist of it is: Define a unary number class Unary to the following specifications.
The class should represent numbers using all 1's, so, for example, 11111 represents 5 and 1111111111 represents 10 and the empty string represents 0. We have to do << >> + - ++ & --(post and pre).
Ok. So far I have only been able to get the << >> and + operator to work. So I am working on the - right now and I get this error:
100:17: error: expected primary-expression before '(' token
100:26: error: expected primary-expression before ')' token
I am using g++.
Here is my code. I've marked line 100 with a //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#include <iostream>
#include <cstdlib>
#include <sstream>
using namespace std;
class Unary {
friend ostream& operator<<(ostream& outputStream, const Unary& output);
friend istream& operator>>(istream& inputStream, Unary& input);
public:
Unary();
~Unary();
Unary(int&);
string toString();
Unary operator+(const Unary&);
Unary operator-();
Unary& operator++();
const Unary operator++(int);
// Unary& operator--();
private:
int x, myInt;
string myString;
};
int main() {
// variable for manipulating
Unary uNum;
Unary uNum2;
Unary uNumAns;
Unary uNumAns2;
cout << "Please enter a number by representing it with 1's: ";
cin >> uNum;
cout << "uNum has " << uNum << ". " << endl;
cout << "Please enter a number by representing it with 1's: ";
cin >> uNum2;
cout << "uNum2 has " << uNum2 << ". " << endl;
uNumAns = uNum + uNum2;
cout << "uNum (" << uNum << ") + uNum2 (" << uNum2 << ") = uNumAns ("
<< uNumAns << ") " << endl;
cout << "uNumAns is " << uNumAns << endl;
cout << "** After ++uNumAns, uNumAns is " << uNumAns << endl;
// cout << "uNumAns2 is " << uNumAns2 << endl;
// --uNumAns2;
// cout << "** After --uNumAns, uNumAns2 is " << uNumAns2 << endl;
// cout << "\nuNumAns before uNumAns++ is " << uNumAns;
// uNumAns++;
// cout << "uNumAns after uNumAns++ is " << uNumAns << endl;
return 0;
}
// default constructor
Unary::Unary() : myInt(0) {}
Unary::Unary(int& newInt) : myInt(newInt) {
myString = this->toString();
cout << " in Unary(int) : myInt is " << myInt << "& myString is "
<< myString << endl;
}
// deconstructor
Unary::~Unary() {}
ostream& operator<<(ostream& outputStream, const Unary& output) {
outputStream << output.myString;
return outputStream;
}
istream& operator>>(istream& inputStream, Unary& input) {
string str;
inputStream >> str;
input.myString = str;
return inputStream;
}
Unary& Unary::operator++() {
this->myString += 1;
return *this;
}
// Unary& Unary::operator--() {
// this->myString = myString - 1;
// return *this;
// }
const Unary Unary::operator++(int post) {
myInt = myString.length();
Unary* answer = new Unary(myInt);
myInt += 1;
return *answer;
}
Unary Unary::operator-() {
// int newx = lhs.x - rhs.x;
// Unary *answer = new Unary(newx);
myInt = myString.length();
return Unary(-myInt&); //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ THIS IS LINE 100
}
Unary Unary::operator+(const Unary& rhs) {
// myInt = this->myInt + rhs.toString();
Unary* answer = new Unary;
answer->myString = myString + rhs.myString;
return *answer;
}
string Unary::toString() {
string str = "";
myInt = myString.length();
cout << "\n (changing an int to a string) You entered " << myInt;
for (int i = 0; i < myInt; i++) {
str += "1";
}
myString = str;
cout << " ** myString is " << myString << " ** " << endl;
return str;
}