C++错误:令牌之前的预期主表达式'('
C++ error: expected primary-expression before '(' token
所以我知道我的代码是一团糟。我有一个关于操作符重载的作业,已经过期了,我一直在努力使它工作。
其要点是:定义一个一元数类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++。这是我的代码。我用//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#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);
相关文章:
- 在 C++ 中使用正则表达式错误时出现问题 括号表达式中的范围无效
- C++ 标记之前预期主表达式'('错误
- 正则表达式中带有卷轴大括号的正则表达式错误
- C++初学者错误:主要表达式错误
- 代数树表达式C++错误:未解析的外部符号"Public:_thiscall AlgebraicTreeExpression::~AlgebraicTreeExpression(void) ..."
- 使用多线程处理对象数组 - 无效使用 void 表达式错误
- 为什么 if constexpr 不会使这个核心常量表达式错误消失?
- 提升正则表达式错误:未初始化的提升::match_results
- C++比较表达式错误
- C++ "主表达式"错误
- C++ 正则表达式错误(未处理的异常)
- 'this'不能用于常量表达式错误 (C++)
- 代码块引发有关for_each的预期主表达式错误
- Boost Regex中的命名子表达式错误
- C++之前的主要表达式错误
- Ubuntu 上的正则表达式错误
- C++正则表达式错误
- 理解常量表达式错误
- 成员函数和预期的主表达式错误
- Xcode中应有表达式错误