C++ error: expected primary-expression before ‘}’ token
我试图调用一个名为
error: expected primary-expression before ‘}’ token
这是我的完整代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | #include <cstdio> #include <iostream> #include <fstream> #include <cstdlib> #include <string> using namespace std; void correction(ifstream& original,ofstream& corrected); int main () { ifstream original; ofstream corrected; char filename[16]; cout<<"Enter filename: "; cin>>filename; original.open(filename); corrected.open("corrected.txt"); if (original.fail( )) { cout <<"Input file opening failed. "; exit(1); } if (corrected.fail( )) { cout <<"Input file opening failed. "; exit(1); } else { correction(original,corrected); } return 0; } void correction(ifstream& original,ofstream& corrected) { char symbol; while(! original.eof()) { original.get(symbol); if (symbol== 'c') { corrected.put(symbol); original.get(symbol); if (symbol=='i') { corrected.put(symbol); original.get(symbol); if (symbol=='n') { corrected.put(symbol); original.get(symbol); while (symbol==' ') { original.get(symbol); } for (symbol=='<') { corrected.put('>'); original.get(symbol); } corrected.put(symbol); } } if (symbol=='o') { corrected.put(symbol); original.get(symbol); if (symbol=='u') { corrected.put(symbol); original.get(symbol); if (symbol=='t') { corrected.put(symbol); original.get(symbol); while (symbol==' ') { original.get(symbol); } for (symbol=='>') { corrected.put('<'); original.get(symbol); } corrected.put(symbol); } } } } else corrected.put(symbol); } return; } |
当我试图编译此代码时,会得到以下错误:
operators.cpp:59:23: error: expected ‘;’ before ‘)’ token
for (symbol=='<') ^ operators.cpp:65:5: error: expected primary-expression before ‘}’ token } ^ operators.cpp:65:5: error: expected ‘)’ before ‘}’ token > >operators.cpp:65:5: error: expected primary-expression before ‘}’
token
operators.cpp:65:5: error: expected ‘;’ before ‘}’ token
operators.cpp:83:20: error: expected ‘;’ before ‘)’ token
for (symbol=='>')
^ operators.cpp:89:9: error: expected primary-expression before ‘}’ token
}
^ operators.cpp:89:9: error: expected ‘)’ before ‘}’ token operators.cpp:89:9: error: expected primary-expression before ‘}’
token operators.cpp:89:9: error: expected ‘;’ before ‘}’ token< /块引用>
我对编程有点陌生,花了几个小时试图修复它,但我不明白问题是什么。
任何帮助都非常感谢!
for (symbol=='<') 和for (symbol=='>') for循环语法都是错误的。在这个场景中,您可以使用for循环,如下所示。
1
2
3
4
5
6
7
8
9
10
11 for (; symbol == '<';)
{
corrected.put('>');
original.get(symbol);
}
for (; symbol == '>';)
{
corrected.put('<');
original.get(symbol);
}或
1
2
3
4
5
6
7
8
9
10
11 for (; symbol == '<';original.get(symbol))
{
corrected.put('>');
}
for (; symbol == '>';original.get(symbol))
{
corrected.put('<');
}在仔细看了@liup的答案后,补充道:
哎哟,我的错,这首歌可能真的是要用一个循环!然后,可以使用while循环,如下所示:
1 while (symbol=='<')和
1 while (symbol=='>')老回答:
在这里:
1 if (symbol=='<')这里:
1 if (symbol=='>')而不是
for (symbol=='<') 和for (symbol=='>') 。错误源于这样一个事实:在一个
for 循环中,即使这三个子句都是空的,也必须拥有它们,因此必须使用两个分号; 。在两个不正确的for 语句中,您只提供了一个分号。有关参考,请参见CPPreference中的循环描述。