简体   繁体   中英

Member must have class/struct/union

I am trying to write a C++ class definition called student.h which will read the grades from the input file defined by the user, and write the grades into an output file defined by the user. This is what I have so far but I'm getting this error and I have no idea how to fix it. I was wondering if anyone could help me with this problem:

#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

}

This is the syntax that I get when I compile the code:

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 ==========

This is what is called the most vexing parse :

student a();
         ^^

this is really a function declaration what you need is this:

student a;

or in C++11 you can use uniform initialization :

student a{};

The problem is there is an ambiguity in the C++ grammar and so anything that can be interpreted as a function declaration will be. This is covered in section 6.8 Ambiguity resolution in the draft C++ standard .

This is one of the cases where using a second compiler can help, clang actually spots the problem right away ( live exmaple ), the warnings given are as follows:

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

 student a();
          ^~

note: remove parentheses to declare a variable

 student a();
          ^~

I cover pretty much all the online C++ compilers in my answer to Online C++ compiler and evaluator and I find instructive to run code in multiple compilers.

Based on your comment, you will receive an error if you don't provide an implementation of your default constructor , this is one possible way to implement it but you need to decide what proper default values would be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM