2

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;
}
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
Nat
  • 37
  • 2
  • 8
  • 4
    You should see an obvious problem with line 100. – chris Jul 30 '14 at 23:21
  • i fixed it. I think that was an accident when I was copying and pasting. I still get the errors. – Nat Jul 30 '14 at 23:24
  • This question appears to be out of topics as it is refers to hiring debugging staff. – 101010 Jul 30 '14 at 23:26
  • 2
    ...can you at least tell us WHICH line is 100 so we don't have to look through all that unnecessary code you posted? – scohe001 Jul 30 '14 at 23:26
  • sorry ... return Unary( -myInt&); is line 100 in the - operator overload function – Nat Jul 30 '14 at 23:29
  • 1
    @Nat Whoa, what on earth is that little ampersand doing there? Could that possibly be causing the problem? /sarcasm/ – scohe001 Jul 30 '14 at 23:32
  • 1
    Your `operator+` leaks memory every time it's called. – chris Jul 30 '14 at 23:32
  • alright all ... I appreciate the sarcasm ... trust me ... I did not want to post this sorry code here ... BUT I am tired and I wanted help with just one of the many errors I've gotten. @chris yeah I haven't dealt with that ... but it adds!! :P – Nat Jul 30 '14 at 23:42

1 Answers1

1

I think you meant for this:

return Unary(-myInt&);

to be:

return Unary(-myInt);

The ampersand is a syntax error there.

David G
  • 94,763
  • 41
  • 167
  • 253
  • -myInt doesn't work without the & because Unary requrites an int& – Nat Jul 30 '14 at 23:30
  • 1
    @Nat, That's not how references work. – chris Jul 30 '14 at 23:31
  • @Nat 0x499602D2 is correct, you do not need to specify `&`. Please go read up on passing by reference again. – OMGtechy Jul 30 '14 at 23:32
  • ok thanks. I am just swimming in a sea of C++ at the end of the semester and what made sense 3 weeks ago doesn't any more. Thanks all for your help. – Nat Jul 30 '14 at 23:34
  • I changed it to Unary Unary::operator-() { myInt = myString.length(); myInt = -myInt; Unary *answer = new Unary(myInt); return *answer; } and it compiles now. Thanks again for the help! – Nat Jul 30 '14 at 23:36
  • 1
    @Nat, That also leaks memory. First, your constructor shouldn't be taking the `int` by reference. Then, this just becomes `return myString.length();` – chris Jul 30 '14 at 23:38
  • 1
    @Nat It works but there's still an issue. `Unary* answer = new Unary(myInt)` allocates memory using `new`. If that `new` is not matched with a `delete`, you'll get a memory leak. It's better to just do `Unary answer(myInt)` instead. Moreover, since `Unary(int)` is non-[`explicit`](http://stackoverflow.com/questions/121162/what-does-the-explicit-keyword-in-c-mean), you can go ahead and do `Unary Unary::operator-() { myInt = myString.length(); return myInt; }` – David G Jul 30 '14 at 23:40
  • 2
    @Nat stop doing `new` . you don't need it anywhere in your code and it only causes problems. – M.M Jul 30 '14 at 23:47
  • Thanks y'all! That really helps. @MattMcNabb removing the news!! =) – Nat Jul 30 '14 at 23:50