不能从文件到指针向量的对象复制

Cant copy from a file to vector of pointers to objects

本文关键字:对象 复制 向量 指针 文件 不能      更新时间:2023-10-16

所以我有一个名为CStudentEmploy包含3个变量的类。

我还有另一个课堂 CAnalizeData:CStudentEmploy

包含指针的向量 vector<CStudentEmploy*>m_vData;

我也有istream运算符:

friend istream& operator >> (istream& str,CStudentEmploy& obj)
    {
        str >> obj.m_strName >> obj.m_strFacNum >> obj.m_iMinutes;
        return str;
    }

我想以这样的方式从文件中填充此向量:

CAnalizeData(const string &strFileName) {
        ifstream ifile(strFileName.data());
        copy(istream_iterator<CStudentEmploy*>(ifile), istream_iterator<CStudentEmploy*>(), back_inserter(m_vData));
    }

如果我试图填充对象的向量,则可以使用这种方式。

我遇到的错误是:

错误c2679二进制'>>':找不到操作员,它采用了类型'_ty'的右手操作数(或没有可接受的转换)

我知道迭代器存在问题,但不能真正解决它。谢谢

这是完整的代码:

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <fstream>
#include <algorithm>
#include <istream>
using namespace std;
class CStudentEmploy {
private:
    string m_strName;
    string m_strFacNum;
    int m_iMinutes;
public:
    CStudentEmploy() {
        m_strName = "Empty";
        m_strFacNum = "Empty";
        m_iMinutes = 0;
    }
    CStudentEmploy(string strname,string strfacnum,int minutes) {
        m_strName = strname;
        m_strFacNum = strfacnum;
        m_iMinutes = minutes;
    }
    CStudentEmploy(const CStudentEmploy &obj) {
        m_strName = obj.m_strName;
        m_strFacNum =obj.m_strFacNum;
        m_iMinutes =obj.m_iMinutes;
    }
    int get_m_iMinutes() {
        return m_iMinutes;
    }
    CStudentEmploy operator =(const CStudentEmploy &obj) {
        this->m_strName = obj.m_strName;
        this->m_strFacNum = obj.m_strFacNum;
        this->m_iMinutes = obj.m_iMinutes;
        return *this;
    }
    bool operator <(const CStudentEmploy &obj)const {
        return m_iMinutes<obj.m_iMinutes;
    }
    CStudentEmploy operator +(const CStudentEmploy &obj) {
        this->m_iMinutes += obj.m_iMinutes;
        return *this;
    }
    friend ostream& operator << (ostream& str, const CStudentEmploy &obj)
    {
        str << "nIme: " << obj.m_strName<< "nF Nomer: " << obj.m_strFacNum << "nMinuti:" << obj.m_iMinutes << endl;
        return str;
    }
    friend istream& operator >> (istream& str,CStudentEmploy& obj)
    {
        str >> obj.m_strName >> obj.m_strFacNum >> obj.m_iMinutes;
        return str;
    }
};
class CAnalizeData:CStudentEmploy {
private:
    vector<CStudentEmploy*>m_vData;
public:
    CAnalizeData(const string &strFileName) {
        ifstream ifile(strFileName.data());
        copy(istream_iterator<CStudentEmploy*>(ifile), istream_iterator<CStudentEmploy*>(), back_inserter(m_vData));
    }
    void Write() {
        vector<CStudentEmploy*>::iterator it = m_vData.begin();
        while (it != m_vData.end())
        {
            cout << *it++;
        }
    }
    void Sort() {
        sort(m_vData.begin(), m_vData.end());
    }
    double calcMean() {
        double avg = 0;
        vector<CStudentEmploy*>::iterator it = m_vData.begin();
        for (it = m_vData.begin(); it != m_vData.end(); it++) {
            avg += (*it)->get_m_iMinutes();
        }
        cout << "Average minutes is:";
        return avg / m_vData.size();
    }
};
int main() {
    CAnalizeData AB("Test.txt");
    AB.Sort();
    AB.Write();
    cout << AB.calcMean();
    cout << endl;   system("pause");
    return 0;
}

也许与 std::transform

CAnalizeData(const string &strFileName) {
    ifstream ifile(strFileName.data());
    transform(istream_iterator<CStudentEmploy>(ifile),
      istream_iterator<CStudentEmploy>(), back_inserter(m_vData),
      [](const CStudentEmploy &e) { return new CStudentEmploy(e); });
}

在那里使用new,因为我认为该对象会在堆栈上创建。