Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

c++ - 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
andydocumentsvisual studio 2012projects
ew project
ew projectmain.cpp(56): error C2228: left of '.READ' must have class/struct/union
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

Update

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) {}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...