2013-12-11 64 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

相關問題