为什么在这段代码中会出现编译错误? 我必须为C++中的类编写这个程序,从代码中删除所有注释,并将代码读出到新文件或输出中。经过长时间的工作,我不断收到编译错误,我只是不知道我做错了什么。如果能帮上点忙,我将不胜感激 #include <iostream> #include <fstream> #include <string> #include <cassert> #include <cstdio> using namespace std; void remove_comments (ifstream& , ofstream&); //function declaration int main(void) { int i; string inputFileName; string outputFileName; string s; ifstream fileIn; ofstream fileOut; char ch; do { cout<<"Enter the input file name:"; cin>>inputFileName; } while (fileIn.fail() ); cout<<"Enter the output file name: "; cin>>outputFileName; fileIn.open(inputFileName.data()); assert(fileIn.is_open() ); remove_comments ( fileIn , fileOut); fileIn.close(); fileOut.close(); return 0; } void remove_comments( ifstream& fileIn , ofstream& fileOut) { string line; bool flag = false; while (! fileIn.eof() ) { getline(fileIn, line); if (line.find("/*") < line.length() ) flag = true; if (! flag) { for (int i=0; i < line.length(); i++) { if(i<line.length()) if ((line.at(i) == '/') && (line.at(i + 1) == '/')) break; else fileOut << line[i]; } fileOut<<endl; } if(flag) { if(line.find("*/") < line.length() ) flag = false; } }

为什么在这段代码中会出现编译错误? 我必须为C++中的类编写这个程序,从代码中删除所有注释,并将代码读出到新文件或输出中。经过长时间的工作,我不断收到编译错误,我只是不知道我做错了什么。如果能帮上点忙,我将不胜感激 #include <iostream> #include <fstream> #include <string> #include <cassert> #include <cstdio> using namespace std; void remove_comments (ifstream& , ofstream&); //function declaration int main(void) { int i; string inputFileName; string outputFileName; string s; ifstream fileIn; ofstream fileOut; char ch; do { cout<<"Enter the input file name:"; cin>>inputFileName; } while (fileIn.fail() ); cout<<"Enter the output file name: "; cin>>outputFileName; fileIn.open(inputFileName.data()); assert(fileIn.is_open() ); remove_comments ( fileIn , fileOut); fileIn.close(); fileOut.close(); return 0; } void remove_comments( ifstream& fileIn , ofstream& fileOut) { string line; bool flag = false; while (! fileIn.eof() ) { getline(fileIn, line); if (line.find("/*") < line.length() ) flag = true; if (! flag) { for (int i=0; i < line.length(); i++) { if(i<line.length()) if ((line.at(i) == '/') && (line.at(i + 1) == '/')) break; else fileOut << line[i]; } fileOut<<endl; } if(flag) { if(line.find("*/") < line.length() ) flag = false; } },c++,C++,你没有写参数名,应该是 remove_comments( ifstream& fileIn, ofstream& fileOut) 您还遗漏了末尾的几个括号在main()中声明的变量对其他函数不可用。与所有函数类似。这称为变量范围 您的变量fileIninremove\u comments在函数中没有声明,这正是编译器所抱怨的。也许,正如@Ahmed Magdy Guda所说,您忘记在函数声明和定义中输入参数。您到底收到了什么错误?错误消息是什么?您的程序处理了吗?错误消息准确

你没有写参数名,应该是

remove_comments( ifstream& fileIn, ofstream& fileOut)

您还遗漏了末尾的几个括号

main()
中声明的变量对其他函数不可用。与所有函数类似。这称为变量范围


您的变量
fileIn
in
remove\u comments
在函数中没有声明,这正是编译器所抱怨的。也许,正如@Ahmed Magdy Guda所说,您忘记在函数声明和定义中输入参数。

您到底收到了什么错误?错误消息是什么?您的程序处理了吗?错误消息准确地解释了错误所在。在第53行,您尝试使用
fileIn
,但没有这样的变量。请注意,人们希望程序是可编译的,除非您在问题中指出无法编译。如果您有编译错误,请始终在问题中包含编译错误消息;我们不应该猜测你看到了什么问题。识别您正在使用的编译器和操作系统通常也是一个好主意。另外,有一天你将了解人们在程序中可以做的技巧,例如
put(“/*这不是注释*/”),但在进行此类改进之前,您需要担心更大的问题。
remove_comments( ifstream& , ofstream&) 
remove_comments( ifstream& fileIn, ofstream& fileOut)