Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法将指向对象的指针从文件复制到向量_C++_Pointers_Vector_Copy - Fatal编程技术网

C++ 无法将指向对象的指针从文件复制到向量

C++ 无法将指向对象的指针从文件复制到向量,c++,pointers,vector,copy,C++,Pointers,Vector,Copy,因此,我有一个名为CStudentEmploy的类,包含3个变量 我还有一节课 CAnalizeData:CStudentEmploy 它包含指针向量 vectorm\u vData 我还有istream操作员: friend istream& operator >> (istream& str,CStudentEmploy& obj) { str >> obj.m_strName >> obj.m_strFac

因此,我有一个名为
CStudentEmploy
的类,包含3个变量

我还有一节课
CAnalizeData:CStudentEmploy

它包含指针向量
vectorm\u 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));
    }
CAnalizeData(常量字符串和strFileName){
ifstream ifile(strFileName.data());
复制(istream_迭代器(ifile)、istream_迭代器()、back_插入器(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;
CStudentEmploy类{
私人:
字符串m_strName;
字符串m_strFacNum;
国际货币基金组织;
公众:
CStudentEmploy(){
m_strName=“空”;
m_strFacNum=“空”;
m_imuntes=0;
}
CStudentEmploy(字符串strname、字符串strfacnum、int minutes){
m_strName=strName;
m_strFacNum=strFacNum;
m_imuntes=分钟;
}
CStudentEmploy(const CStudentEmploy&obj){
m_strName=对象m_strName;
m_strFacNum=对象m_strFacNum;
m_imuntes=对象m_imuntes;
}
int get_m_imuntes(){
返回m_imuntes;
}
CStudentEmploy运算符=(const CStudentEmploy&obj){
this->m_strName=obj.m_strName;
此->m_Ustrfacnum=obj.m_strFacNum;
这->m_imuntes=对象m_imuntes;
归还*这个;
}

布尔运算符可能是带有
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); });
}
CAnalizeData(常量字符串和strFileName){
ifstream ifile(strFileName.data());
变换(istream_迭代器(ifile),
istream_iterator(),back_inserter(m_vData),
[](const-CStudentEmploy&e){返回新的CStudentEmploy(e);});
}

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

请在完整的错误消息中包含一个(通常在包含它认为像
\u Ty
的类型之后会有更多)。不管我的答案是否有效,有一点需要注意的是,您存储的指针永远不会得到
delete
d。更好的答案可能是不使用指针,特别是因为您的排序也无法正常工作,而且似乎没有任何理由使用指针,因为对象不存在于其他任何地方。啊,我明白了。这是这就是需要完整错误的原因。repl下的叮当声。它表明问题出在
操作符>>
(它需要一个对象,但代码试图给它一个指针)…编辑传入的