2013-12-11 144 views
1

我想写称为student.h一个C++的类定义将用户定义的输入文件读取的档次,档次写入由定义的输出文件 用户。这是我迄今为止,但我得到这个错误,我不知道如何解决它。我在想,如果有人可以帮助我解决这个问题:会员必须有类/结构/联合

#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 

} 

这是我得到的语法,当我编译代码:

1>------ Build started: Project: New Project, Configuration: Debug Win32 ------ 
1> Main.cpp 
1>c:\users\randy\documents\visual studio 2012\projects\new project\new project\main.cpp(56): error C2228: left of '.READ' must have class/struct/union 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 
+2

'学生一();' - 删除(),它应该只是'学生答;' –

+0

你不需要'(无效)'遍铺 –

+0

这是一个有点不寻常具有像READ()这样的大写方法名称。你也从来没有设置学生的ID或成绩的数量。 – jarmod

回答

6

这就是所谓的most vexing parse

student a(); 
     ^^ 

这实际上是一个函数声明,你需要的是:

student a; 

C++ 11您可以使用uniform initialization

student a{}; 

的问题是没有在C++语法歧义等任何可以解释为函数声明会。这在6.8中涵盖,在draft C++ standard中的歧义分辨率

这是在哪里使用第二编译器可以帮助情形之一的,clang实际上斑点问题的时候了(live exmaple),给出的警告如下:

warning: empty parentheses interpreted as a function declaration [-Wvexing-parse] 

student a(); 
      ^~ 

note: remove parentheses to declare a variable 

student a(); 
      ^~ 

我涵盖几乎所有在我的答案Online C++ compiler and evaluator的在线C++编译器,我发现在多个编译器中运行代码很有指导意义。

更新

基于您的评论,你会收到一个错误,如果你不提供你default constructor的实现,这是实现它的一种可能的方式,但你需要决定什么适当的默认值将是:

student::student() : id(-1), n(0) {} 
+1

不,这不是最令人头疼的解析。 – 2013-12-11 20:54:12

+2

@ H2CO3其实我通过电子邮件询问斯科特以澄清他是否认为这属于MVP,并且他说他这样做。我知道很多人对此感到不适,但是在重新阅读该部分之后,我感觉这实际上是MVP的另一个案例,因此我通过电子邮件发送并询问。 –

+0

很遗憾,这个词的创造者(他是那个,对吧?)对他的措辞不一致,导致了混淆。 – 2013-12-11 20:56:27

相关问题