C++ 使用类来计算文件中的行数
c++ using class to count lines in file
我正在尝试将我的行计数函数移动到一个类中,但是,我遇到了一些错误,我不知道如何使其工作。
class lines {
string name;
int number_of_lines;
string line;
public:
void set_value (string n);
ifstream myfile(name); //C2061: syntax error : identifier 'name'
while (getline(myfile, line)) //Multiple markers at this line - C2059: syntax error : 'while', - Syntax error
{ // C2334: unexpected token(s) preceding '{'; skipping apparent function body
++number_of_lines;
}
int row() {return number_of_lines;}
};
void lines::set_value (string n) {
number_of_lines=0;
name = n;
}
我将错误作为注释添加到它们显示的行中。
更改代码
string line;
public:
void set_value (string n);
ifstream myfile(name); //C2061: syntax error : identifier 'name'
while (getline(myfile, line)) //Multiple markers at this line - C2059: syntax error : 'while', - Syntax error
{ // C2334: unexpected token(s) preceding '{'; skipping apparent function body
++number_of_lines;
}
自:
public:
void set_value (string name)
{
ifstream myfile(name);
string line;
while (getline(myfile, line))
{
++number_of_lines;
}
myfile.close();
}
无论您在
必须位于某个函数内部的C++中执行的任何操作/计算,这里您都在必须在某个函数内的类中使用以下语句。
ifstream myfile(name); //C2061: syntax error : identifier 'name'
while (getline(myfile, line)) //Multiple markers at this line - C2059: syntax error : 'while', - Syntax error
{ // C2334: unexpected token(s) preceding '{'; skipping apparent function body
++number_of_lines;
}
相关文章:
- 如何计算文件中的"columns"数?
- C++数据文件、数组和计算赋值
- 如何计算文件中的生物数量?
- 计算二进制文件中的位数
- 通过从文件中读取值来计算移动平均线
- 在目标计算机上访问 POSIX 信号灯时出现可执行文件崩溃(SEGV_MAPERR)
- 我正在尝试计算文本文件中有多少销售人员 c++
- 在C++中对文件中的值单独执行计算
- 从文本文件中的每一行读取数字,并计算数字重复的次数
- RPN计算器使用头文件进行计算操作和堆栈;用于堆栈的矢量
- 从文件中找出字符'a',然后计算字符出现的行数
- 使用结构数组计算文本文件中单词的出现次数C++
- 快速计算 CSV 文件 C++ 中的行数
- 创建程序以从给定的.txt文件中查找文本,替换并计算单词
- 通过将文本文件读取为字符串/向量来计算加权/未加权 GPA
- 在 64 位 Linux armv8 计算机上编译 32 位二进制文件时遇到问题
- 阅读一个大文件来计算重复K次的单词数
- 在不同的计算机上运行.exe文件.(视觉工作室)
- 使用Proc文件计算CPU使用率%
- 从文本文件计算