【问题标题】:Cant copy from a file to vector of pointers to objects无法从文件复制到指向对象的指针向量
【发布时间】:2019-03-08 17:40:12
【问题描述】:

所以我有一个名为 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;
}

【问题讨论】:

  • 请在完整的错误消息中包含minimal reproducible example(通常在包含它认为_Ty 是什么类型之后还有更多)。
  • 有一点需要注意,无论我的回答是否有效,您存储的指针永远不会得到deleted。更好的答案可能是不使用指针,特别是因为您的排序也无法正常工作,而且似乎没有任何理由拥有它们,因为对象不在其他任何地方。

标签: c++ pointers vector copy


【解决方案1】:

也许是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,因为我假设该对象会在堆栈上创建。

【讨论】:

  • 啊,我明白了。这就是需要完整错误的原因。 repl.it下的clang显示问题是operator&gt;&gt;(它想要一个对象,但代码试图给它一个指针)...编辑传入
猜你喜欢
  • 2013-07-05
  • 2021-06-19
  • 1970-01-01
  • 1970-01-01
  • 2011-10-01
  • 2011-02-11
  • 2011-08-04
  • 2011-12-19
  • 2017-06-12
相关资源
最近更新 更多