gpt4 book ai didi

c++ - C++错误: expected primary-expression before ‘}’ token

转载 作者:行者123 更新时间:2023-12-02 11:13:59 25 4
gpt4 key购买 nike

我试图调用一个名为correction(original,corrected)的void函数,当我尝试对其进行编译时,出现了以下几种错误:

error: expected primary-expression before ‘}’ token



这是我完整的代码:
    #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:\n";
cin>>filename;
original.open(filename);
corrected.open("corrected.txt");
if (original.fail( ))
{
cout << "Input file opening failed.\n";
exit(1);
}
if (corrected.fail( ))
{
cout << "Input file opening failed.\n";
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



我是编程的新手,花了几个小时来修复它,但是我不明白问题是什么。

任何帮助都将不胜感激!

最佳答案

在仔细观察@liup的答案之后添加:

糟糕,我的糟糕,OP可能确实意味着要在此处使用循环!然后,您可以像这样使用while循环:

while (symbol=='<')


while (symbol=='>')

旧答案:

这里:
if (symbol=='<')

和这里:
if (symbol=='>')

代替 for (symbol=='<')for (symbol=='>')

该错误源于以下事实:在 for循环中,即使三个子句为空,也必须具有所有三个子句,因此必须使用两个分号 ;。您仅在两个不正确的 for语句的每一个中提供了一个未提供单个分号的位置。

供引用,请参阅 for loop description at cppreference

关于c++ - C++错误: expected primary-expression before ‘}’ token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35251100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com