成员必须具有班级/结构/工会 [英] Member must have class/struct/union

查看:104
本文介绍了成员必须具有班级/结构/工会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写名为 student.h C ++ 类定义,该定义将从用户定义的输入文件中读取成绩,并将成绩写入用户定义的输出文件
中。到目前为止,这是我所遇到的,但是我遇到此错误,而且我不知道如何解决。我想知道是否有人可以帮助我解决这个问题:

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

}

这是我在我编译代码:

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


推荐答案

这就是所谓的解析度最高的

student a();
         ^^

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

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

student a;

或在 C ++ 11 中,您可以使用统一初始化

student a{};

问题在于 C ++ 语法中有歧义可以解释为函数声明。在6.8 歧义解决部分中对此进行了介绍。 /wg21/docs/papers/2012/n3485.pdf rel = nofollow noreferrer>草稿C ++标准。

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.

这是其中一种情况使用第二个编译器可以提供帮助, clang 实际上可以立即发现问题( 实时示例 ),给出的警告如下:

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();
          ^~

我在回答在线C ++编译器和评估器,我发现在多个编译器中运行代码具有启发性。

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

这篇关于成员必须具有班级/结构/工会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆