无法让我的num_of_lines函数运行。我确实需要打开和关闭函数内部的文件,并且还必须对其他几个函数执行相同的操作。这样我就可以从文件的乞讨中获取信息(我不能使用任何其他允许我这样做的函数,因为我们还没有在课堂上学过它们)。我有两个错误:
hello.cpp:14:17: error: use of undeclared identifier 'file'; did you mean
'fill'?
ilines.open(file); //does file need to be a c-string
^~~~
fill
hello.cpp:14:12: error: no matching member function for call to 'open'
ilines.open(file); //does file need to be a c-string
不知道该怎么做,谢谢。
#include <iostream>
#include <fstream>
#include <cctype>
#define die(errmsg) {cerr << errmsg << endl; exit(1);}
using namespace std;
int num_of_lines(string name)
{
int cnt3 = 0;
string line;
ifstream ilines; //Do I need to check for successful opening and closing each time
ilines.open(file); //does file need to be a c-string
if(ilines.is_open())
{
while(getline(ilines, line))cnt3++;
}
ilines.close();
return(cnt3);
}
int main(int argc, char **argv)
{
int num_of_lines(string name);
string file;
file = argv[1];
if(argc == 1)die("usage: mywc your_file");
ifstream ifs;
ifs.open(file);
if(ifs.is_open())
{
ifs.close();
int a;
a = num_of_lines(file);
cout <<"Lines: " << a << endl;
}
else
{
cerr <<"Could not open: " << file << endl;
exit(1);
}
//ifs.close();
return(0);
}
答案 0 :(得分:2)
你有几个问题。
func textViewDidChange(textView: UITextView) {
if textView.text.endIndex > 20 {
setBorder(descriptionField, finished: true)
} else {
setBorder(descriptionField, finished: false)
}
}
中未定义变量file
。您可以使用num_of_lines
作为参数而不是`name。
file
您也可以使用
int num_of_lines(string file)
{
...
}
避免复制int num_of_lines(string const& file)
{
...
}
。
假设std::string
的类型为file
,您可能必须在打开的调用中使用std::string
,具体取决于您的编译器支持的C ++标准版本。
如果您有符合C ++ 11的编译器,则可以使用:
file.c_str()
如果您只有符合C ++ 03的编译器,则必须使用:
string file;
ifstream ilines;
ilines.open(file);