从 txt 文件中读取变量并将它们作为数组保存到 class 对象中 c++

Reading variables from txt file and saving them into the class objects as arrays c++

我正在尝试从一个看起来像这样的 txt 文件中获取变量;

1 Prince Heins 25
2 Lady Bridgette 29
3 Tony Ann 223
4 Lucy Phoenix 35

这是我的代码,但我无法将变量从 txt 文件获取到 myArray。我使用 getline 创建了行数,行数等于 linecount 。然后我创建了许多等于行数的对象。下一步是逐行从 txt 文件中获取变量,我尝试使用 while(pbin) 这是我的读取函数。我试图通过使用包含我的字符串和整数的 set 函数将变量发送到 class 但我收到编译错误:[Error] no match for 'operator>>' (操作数类型为 'std::ifstream {aka std::basic_ifstream}' 和 'void') 我检查了示例,但其中 none 解决了我的问题。 无法解决问题并解决它。如果有人能提供帮助,我会很高兴。

#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <istream>
#include <iomanip>
#include <cstring>

using namespace std;

class contact{
private:

    int listno;
    string name;
    string surname;
    string phonenumber;
        
public:
    contact(){
        this->name="Unknown";
        this->surname="Unknown";
        this->phonenumber="Unknown";
        this->listno=0;
    }
    contact (string name,string surname,string phonenumber){
        this->name=name;
        this->surname=surname;
        this->phonenumber=phonenumber;
        this->listno=0;
    }
    contact(int listno,string name,string surname,string phonenumber){
        
        this->name=name;
        this->surname=surname;
        this->listno=listno;
        this->phonenumber=phonenumber;
    }
    void setListno(int listno){
        this->listno=listno;
    }
    
    int getListno(){
        return listno;
    }
    
    void setName(string name){
        this->name=name;
    }
    
    string getName(){
        return name;
    }
    
    void setSurname(string surname){
        this->surname=surname;
    }
    
    string getSurname(){
        return surname;
    }
    
    void setNumber(string phonenumber){
        this->phonenumber=phonenumber;
    }
    
    string getNumber(){
        return phonenumber;
    }
    
    
    void showInfos(){
        cout << "Listno: " << this->listno << endl;
        cout << "Name: " << this->name << endl;
        cout << "Surname: " << this->surname << endl;
        cout << "Number: " << this->phonenumber<<endl;
        
    }
    

};



int main(){
    int mListno;
    string mName;
    string mSurname;
    string mNumber;
    ifstream pbin("phoneData2.txt");
    string line;
    long linecount;
    contact* myArray = new contact[linecount];
    for(linecount=0;getline(pbin,line);linecount++);
    if(pbin.is_open()){
        int i;
        for(i=0;i<linecount;i++){
            while(
            pbin >> myArray[i].setListno(mListno);
            pbin >> myArray[i].setName(mName);
            pbin >> myArray[i].setSurname(mSurname);
            pbin >> myArray[i].setNumber(mNumber);
            )

        }
    }
    pbin.close();
    
    return 0;
}

已编辑:

#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <istream>
#include <iomanip>
#include <cstring>

using namespace std;

class contact{
private:

    int listno;
    string name;
    string surname;
    string phonenumber;
        
public:
    contact(){
        this->name="Unknown";
        this->surname="Unknown";
        this->phonenumber="Unknown";
        this->listno=0;
    }
    contact (string name,string surname,string phonenumber){
        this->name=name;
        this->surname=surname;
        this->phonenumber=phonenumber;
        this->listno=0;
    }
    contact(int listno,string name,string surname,string phonenumber){
        
        this->name=name;
        this->surname=surname;
        this->listno=listno;
        this->phonenumber=phonenumber;
    }
    void setListno(int listno){
        this->listno=listno;
    }
    
    int getListno(){
        return listno;
    }
    
    void setName(string name,int count){
        this->name=name[count];
    }
    
    string getName(){
        return name;
    }
    
    void setSurname(string surname){
        this->surname=surname;
    }
    
    string getSurname(){
        return surname;
    }
    
    void setNumber(string phonenumber){
        this->phonenumber=phonenumber;
    }
    
    string getNumber(){
        return phonenumber;
    }
    
    
    void showInfos(){
        cout << "Listno: " << this->listno << endl;
        cout << "Name: " << this->name << endl;
        cout << "Surname: " << this->surname << endl;
        cout << "Number: " << this->phonenumber<<endl;
        
    }
    
    friend istream & operator>> (istream & in, contact & con){
        in >> con.listno >> con.name >> con.surname >> con.phonenumber;
        return in;
    }

};





int main(){
    ifstream pbin("phoneData2.txt");
    string line;
    long linecount=0;
    contact* myArray = new contact[linecount];
    for(linecount=0;getline(pbin,line);linecount++);
    pbin.seekg(0);
    if(pbin.is_open()){
        int i;
        for(i=0;i<linecount;i++){
            if(! (pbin >> myArray[i]))
            {
                cout << "The contact is not found." ;
            }
        }
    }
    pbin.close();
    
    
    return 0;
}

添加到 contact >> 过载

    friend std::istream & operator>> (std::istream & in, contact & con)
    {
        in >> con.listno >> con.name >> con.surname >> con.phonenumber;
        return in;
    }

然后在 main 中,for 循环变为

        for(i=0;i<linecount;i++){
            if (! (pbin >> myArray[i]))
            { // failed to read a contact
                // do something here to clean up the mess
            }
        }

为了纠正其他几个错误,

int main(){
    ifstream pbin("phoneData2.txt");
    string line;
    long linecount = 0; // otherwise you don't know what number linecount starts at
    contact* myArray = new contact[linecount];
    for(linecount=0;getline(pbin,line);linecount++);
    pbin.clear(); // Clear the error flag from hitting the end of the file
                  // Needed when building to older C++ Standards
    pbin.seekg(0); // rewind the file
    if(pbin.is_open()){
        int i;
        for(i=0;i<linecount;i++){
            if (! (pbin >> myArray[i]))
            {
                // do something here to clean up the mess
            }

        }
    }
    pbin.close();

    return 0;
}