首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在C++中收到错误"member function member member or member address taken in function“(必须调用成员函数或在函数中采用其地址

在C++中收到错误"member function member member or member address taken in function“(必须调用成员函数或在函数中采用其地址
EN

Stack Overflow用户
提问于 2012-12-10 10:02:53
回答 2查看 2.9K关注 0票数 1

我正在尝试编写一个程序,用一个结构数组来做各种事情,但我在函数方面遇到了问题。我一直收到“成员函数必须被调用或者它的地址在函数中被读取( E2235 *,int)”的错误,我不太确定是什么导致了这个错误。我的代码如下:

代码语言:javascript
复制
#include <iostream.h>
#include <fstream>
struct student{
    string first;
    string last;
    string id;
    double GPA;
    int age;
};
void menu();
void read(student* data, int x);
int main(){

bool quit=false;
char choice;
student data[20];
cout<<"Welcome!"<<endl;
//the program is menu driven, so a switch statement allows for many choices
while(!quit){
    menu();
    cin>>choice;
    switch(choice){
        case '1': read(data, 20); break;
        case '2': break;
        case '3': break;
        case '4': break;
        case '5': break;
        case '6': break;
        case '7': break;
        case '8': cout<<endl<<"Goodbye!\n"; quit=true; break;
        default: cout<<"I think you're doing it wrong. Please try again.";
        }
    }
} 

//menu output so the user knows what they're doing
void menu(){
    cout<<endl;
    cout<<"What would you like to do?"<<endl;
    cout<<"(1) - Read data into the database"<<endl;
    cout<<"(2) - Add a student"<<endl;
    cout<<"(3) - Remove a student"<<endl;
    cout<<"(4) - List all students"<<endl;
    cout<<"(5) - Compute the average GPA and SD of the database"<<endl;
    cout<<"(6) - View Student Information"<<endl;
    cout<<"(7) - Save the database"<<endl;
    cout<<"(8) - Exit"<<endl;
}

//the problem function
void read(student* data, int x){
    ifstream infile;
    infile.open("StudentStruct.txt");
    int k=0;
    string first, last, id;
    double GPA;
    int age;
    while(!infile.eof&&k<x){
        infile>>first;
        data[k].first;
        infile>>last;
        data[k].last;
        infile>>id;
        data[k].id;
        infile>>GPA;
        data[k].GPA;
        infile>>id;
        data[k].age;
        k++;
    }
    cout<<"The database now contains "<<k<<" data entries from 'StudentStruct.txt'"<<endl;
}

可能导致此错误的原因是什么?我应该如何修复它?

EN

回答 2

Stack Overflow用户

发布于 2012-12-10 11:45:13

我会把问题函数分成几个独立的部分。只需从文件中读取一条“记录”即可。另一个将使用它来读取所有记录。第一个,按照惯例,被命名为operator>> (也称为“流提取器”):

代码语言:javascript
复制
std::istream &operator>>(std::istream &infile, student &d) { 
    infile >> d.first;
    infile >> d.last;
    infile >> d.id;
    infile >> d.GPA;
    infile >> d.age;
    return infile;
}

第二个函数使用该函数从文件中读取所有数据。因此,您不希望使用while (!whatever.eof()) --这几乎肯定会失败。有几种选择。一种是这样的:

代码语言:javascript
复制
while (infile >> some_student)
    students.push_back(some_student);

另一种可能是使用istream_iterator,它几乎可以自动化整个循环:

代码语言:javascript
复制
std::vector<student> students((std::istream_iterator<student>(infile)),
                                std::istream_iterator<student>());

它定义了students向量,并从一对istream_iterator中初始化它,它隐式地循环遍历文件,读取所有数据(使用上面的流提取器。

如上所述,我也会使用std::vector<student> (如果可能)而不是student *,这样它就可以在需要时调整大小以容纳数据。使用这些函数,该函数如下所示:

代码语言:javascript
复制
std::vector<student> read() { 
    std::ifstream infile("StudentStruct.txt");
    std::vector<student> students((std::istream_iterator<student>(infile)),
                                    std::istream_iterator<student>());
    return students;
}
票数 2
EN

Stack Overflow用户

发布于 2012-12-10 11:19:26

我认为这是一个措辞笨拙的错误消息,与带有infile.eofwhile条件中的代码相关。您没有使用函数调用约定(即infile.eof()),因此编译器不知道您试图对该代码做什么:调用成员函数或获取其地址。所以它会抛出一个警告。

从文件中读取学生数据的方式还有另一个问题:

代码语言:javascript
复制
string first, last, id;
double GPA;
int age;

while(!infile.eof&&k<x){
    // Here you read a string into the local variable 'first'
    infile>>first;

    // And here you do... what? referencing the 'first' member variable of the k'th student.
    data[k].first;

    // Rinse, lather repeat
    infile>>last;
    data[k].last;
    infile>>id;
    data[k].id;
    infile>>GPA;
    data[k].GPA;
    infile>>id;
    data[k].age;
    k++;
}

仔细看看你在做什么:你的data永远不会有学生。您将值读入局部变量,然后丢弃结果,从不使用它们。综合克里斯关于取消使用infile.eof()的建议,试着这样做:

代码语言:javascript
复制
while((k < x) && (infile >> data[k].first)) 
{
    infile >> data[k].last;
    infile >> data[k].id;
    infile >> data[k].GPA;
    infile >> data[k].age;

    k++;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13794023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档