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

C++ error: expected primary-expression before '(' token

本文关键字:表达式 错误 令牌 C++      更新时间:2023-10-16

所以我知道我的代码是一团糟。我有一个关于操作符重载的作业,已经过期了,我一直在努力使它工作。

其要点是:定义一个一元数类unary,以满足以下规范。

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

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

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

我想你是这个意思:

return Unary(-myInt&);

:

return Unary(-myInt);