C++ 成员必须具有类/结构/联合
我试图编写一个C++类定义,叫做“代码>学生.H/COD>”,它将从用户定义的输入文件中读取等级,并将等级写入输出文件中。 由用户定义。这是我到目前为止,但我得到这个错误,我不知道如何修复它。我想知道是否有人能帮我解决这个问题:C++ 成员必须具有类/结构/联合,c++,syntax,C++,Syntax,我试图编写一个C++类定义,叫做“代码>学生.H/COD>”,它将从用户定义的输入文件中读取等级,并将等级写入输出文件中。 由用户定义。这是我到目前为止,但我得到这个错误,我不知道如何修复它。我想知道是否有人能帮我解决这个问题: #include <iostream> #include <stdlib.h> #include <stdio.h> #include <string> #include <fstream> using nam
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <fstream>
using namespace std;
class student {
private:
int id;
int n; // no of- grades
int A[200]; // array to hold the grades
public:
student(void); // constructor
void READ(void); // to read from a file to be prompted by user;
void PRINT(void); // to write into an output file
void showStudent(); //show the three attributes
void REVERSE_PRINT(void); // to write into output file in reverse order;
double GPA(void); // interface to get the GPA
double FAIL_NUMBER(void); //interface to get the number of fails
};
void student::READ()
{
ifstream inFile;
ofstream outFile;
string fileName;
cout << "Input the name of your file" << endl;
cin >> fileName;
inFile.open(fileName.c_str());
if (inFile.fail()) {
cout << fileName << "does not exist!" << endl;
}
else
{
int x;
inFile >> x;
while (inFile.good())
{
for(int i=0;i<200;i++)
{
A[i]=x;
}
inFile >> x;
}
inFile.close();
}
}
int main()
{
student a();
a.READ(); //Line 56
}
这就是所谓的: 这实际上是一个函数声明,您需要的是:
student a;
或者在C++11中,您可以使用:
问题是C++语法中有歧义,所以任何可以解释为函数声明的东西都是。本手册的6.8节
歧义解决中介绍了这一点
这是使用第二个编译器可以提供帮助的情况之一,clang
实际上可以立即发现问题(),给出的警告如下:
warning: empty parentheses interpreted as a function declaration [-Wvexing-parse]
student a();
^~
note: remove parentheses to declare a variable
student a();
^~
<>我在我的答案中覆盖了所有的C++编译器,在多个编译器中运行代码是有指导意义的。
更新
根据您的评论,如果您没有提供的实现,您将收到一个错误,这是一种可能的实现方法,但您需要确定适当的默认值:
student::student() : id(-1), n(0) {}
这就是所谓的: 这实际上是一个函数声明,您需要的是:
student a;
或者在C++11中,您可以使用:
问题是C++语法中有歧义,所以任何可以解释为函数声明的东西都是。本手册的6.8节
歧义解决中介绍了这一点
这是使用第二个编译器可以提供帮助的情况之一,clang
实际上可以立即发现问题(),给出的警告如下:
warning: empty parentheses interpreted as a function declaration [-Wvexing-parse]
student a();
^~
note: remove parentheses to declare a variable
student a();
^~
<>我在我的答案中覆盖了所有的C++编译器,在多个编译器中运行代码是有指导意义的。
更新
根据您的评论,如果您没有提供的实现,您将收到一个错误,这是一种可能的实现方法,但您需要确定适当的默认值:
student::student() : id(-1), n(0) {}
student a()
-remove(),应该是学生a
你不需要在商店里到处都使用(void)
像READ()这样的大写方法名是有点不寻常的。此外,您从不设置学生id或分数。student a()
-remove(),应该是学生a
你不需要在商店里到处都使用(void)
像READ()这样的大写方法名是有点不寻常的。另外,你从不设置学生的id或分数。不,这不是最烦人的解析。@H2CO3实际上,我通过电子邮件问斯科特,他是否认为这属于MVP,他说他认为属于MVP。我知道很多人都有不同的想法,但在重新阅读了《有效STL》中的那一部分后,我离开时感觉这实际上是MVP的另一个案例,所以我发了电子邮件并询问。令人遗憾的是,这个词的创造者(他就是那个,对吧?)在措辞上前后矛盾,造成混乱。@H2CO3据我所知,网上很少有人试图澄清什么属于MVP,有效STL是唯一权威的公共来源。如果你知道任何有权威性的在线材料,请告诉我。不幸的是,我不(我不是特别的C++人,C有明显的这些怪癖)。我想我应该问问斯科特,他为什么前后矛盾,也许还应该请他停止这样做。。。但听起来总决赛的时间太长了:DNo,这不是最烦人的解析。@H2CO3实际上我在电子邮件中问斯科特,他是否认为这属于MVP,他说他认为是的。我知道很多人都有不同的想法,但在重新阅读了《有效STL》中的那一部分后,我离开时感觉这实际上是MVP的另一个案例,所以我发了电子邮件并询问。令人遗憾的是,这个词的创造者(他就是那个,对吧?)在措辞上前后矛盾,造成混乱。@H2CO3据我所知,网上很少有人试图澄清什么属于MVP,有效STL是唯一权威的公共来源。如果你知道任何有权威性的在线材料,请告诉我。不幸的是,我不(我不是特别的C++人,C有明显的这些怪癖)。我想我应该问问斯科特,他为什么前后矛盾,也许还应该请他停止这样做。。。但这听起来好像在决赛时间太多了:D