不能从文件到指针向量的对象复制
Cant copy from a file to vector of pointers to objects
所以我有一个名为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
,因为我认为该对象会在堆栈上创建。
相关文章:
- 为什么C++在将一个对象复制到另一个对象时需要对这两个对象进行低级常量限定
- 如何在向量列表初始化时避免对象复制以及如何延长临时的生存期
- 了解在 C++ 中分配容器时的对象复制
- C++类对象复制构造函数和运算符=
- 复制 elision/RVO 会导致从同一对象复制/移动吗?
- 如何在循环内将大对象复制到omp任务中?
- 为什么当我添加一个不同的对象(复制构造函数中的参数)时调用复制构造函数?
- 无法将类构造函数中新创建的对象复制到 C++ 中的向量成员
- 将C++对象复制到指向新指针的指针处
- 将一个 OpenCV OutputArrayOfArrays 对象复制到另一个对象
- 选择性地禁用隐式对象复制
- 不能从文件到指针向量的对象复制
- 从派生对象复制构造
- OpenCV Mat对象复制速度更快
- 将子对象复制到父对象
- 为什么普通的 c++ 编译器不优化对象复制?
- C :将一个对象复制到构造函数中的另一个对象
- 使用命名对象复制构造函数
- 不必要的对象复制 - C++ STL
- 如何将一个类的对象复制到另一个类中