类中的流语法
fstream syntax in a class
本文关键字:语法 更新时间:2023-10-16
这是我的学生班。该函数static int readRecord(fstream &, Student s[]);
:
#ifndef STUDENT_H
#define STUDENT_H
#include <fstream>
class Student
{
public:
Student();
Student(char* n, int g[]);
char* getName();
void setName(char* n);
void setGrade(int g[]);
double getGradesAverage();
static int readRecord(fstream &, Student s[]);
static void display(Student s[], int students);
static void sort(Student s[], int students);
private:
char name[30];
int grades[5];
};
#endif
在我的 cpp 中,我有这个:
int Student::readRecord(fstream & in, Student s[])
{
int j = 0;
if (!in)
{
cout << "cannot open the file or file does not existsn";
exit(0);
}
else
{
char name[30];
int g[5];
char ch;
while (in)
{
in >> name;
for (int i = 0; i<5; i++)
{
in >> g[i];
if (i != 4)
in >> ch;
}
s[j].setName(name);
s[j].setGrade(g);
j++;
}
}
return j - 1;
}
我收到此编译器错误:
错误C2061:语法错误:标识符"fstream"
和
错误 C2511: 'int 学生::读取记录(标准::fstream &,学生 [])" : 在"学生"中找不到重载成员函数
#ifndef STUDENT_H
#define STUDENT_H
#include <fstream>
class Student
{
public:
Student();
Student(char* n, int g[]);
char* getName();
void setName(char* n);
void setGrade(int g[]);
double getGradesAverage();
static int readRecord(std::fstream &file, Student s[]);
static void display(Student s[], int students);
static void sort(Student s[], int students);
private:
char name[30];
int grades[5];
};
如果您不使用using namespace std
,则必须使用std::fstream
。
更好的选择是做using std::fstream
而不是using namespace std
。
我看到的另一个问题是你忘了为fstream
对象声明一个名称。
static int readRecord(std::fstream &file, Student s[]);
// ^^^ ^^^^^
另外,由于您使用的是C++因此您不应该再将 char 数组用于字符串,而应该std::string
.
我通常会用std::vector
替换所有数组的东西。
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 助记符和指向成员语法的指针
- 有人能分解一下这个c++模板的语法吗
- C++避免重复声明的语法是什么
- QMetaObject invokeMethod的基于函数指针的语法
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 单独定义模板化嵌套类方法的正确语法
- 共享指针和具有自定义删除程序的唯一指针之间的语法差异背后的任何原因
- 错误 C2760:语法错误:映射迭代器上意外的标记"标识符",预期的";"
- 为什么我会收到错误 C2143 语法错误:缺少"*"之前的';'?
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- 使用基类指针调用基类的值构造函数的语法是什么?
- 很好的语法来获取对向量/数组数据的大小引用?
- C++语法运算符功能?
- C++使用 rand 定义函数语法
- 什么文件可以修改 atom 的C++语法?
- 创建模板嵌套类实例的语法?
- C++语法差异:二维和一维数组(指针算术)
- 将信息输入到下面显示的结构向量中的正确语法/格式是什么