C ++错误:在'之前预期的主要表达式('令牌

时间:2014-07-30 23:20:05

标签: c++ compiler-errors g++

所以我知道我的代码很乱。我有一个过期的作业分配操作员重载,我一直在失败,并试图让它工作。

它的要点是:根据以下规范定义一元数字一元。

该类应使用全1来表示数字,因此,例如,11111表示5,1111111111表示10,空字符串表示0.我们必须做<< >> + - ++& - (后和前)。

确定。到目前为止,我只能得到<< >>和+运算符来工作。所以我正在努力 - 现在我得到了这个错误:

100:17: error: expected primary-expression before '(' token
100:26: error: expected primary-expression before ')' token

我正在使用g ++。 这是我的代码。我用//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

标记了第100行
#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;
}

1 个答案:

答案 0 :(得分:0)

我认为你的意思是:

return Unary(-myInt&);

是:

return Unary(-myInt);

&符是语法错误。