ifstream& operator>> 函数失败,该怎么处理

ifstream& operator>> 函数失败

friend ifstream& operator>>(ifstream& i, CSample& obj)
{
i>>obj.id;
i>>obj.price;
i>>obj.counts;
return i;
}





void InitList()
{
ifstream ifs;
ifs.open(fileName,ifstream::in);
CSample obj;

string str;


while(getline(ifs,str))
{

ifs>>obj;
mylist.push_back(obj);
}

ifs.close();
}




发现 ifs>>obj;后,obj没有存储数据,  

是失败的



------解决方案--------------------
在按照楼主的CSample改一下:
C/C++ code

#include <iostream>
#include <fstream>
using namespace std;

class CSample
{
private:
    int        id;
    double    price;
    int        counts;
public:
    inline CSample():id(-1), price(0.0), counts(0)
    {
    }
 
    inline CSample(int id, double price, int counts)
    {
        this->id = id;
        this->price = price;
        this->counts = counts;
    }
 
    inline virtual ~CSample()
    {
    }

    inline void print()
    {
        cout << id << " " << price << " " << counts << endl;
    }

    friend ofstream& operator << (ofstream&, const CSample&);
    friend ifstream& operator >> (ifstream&, CSample&); 
};
 
ofstream& operator << (ofstream& os, const CSample& c)
{
    os << c.id << " " << c.price << " " << c.counts << endl;
    return os;
}

ifstream& operator >> (ifstream& is, CSample& c)
{
    is >> c.id >> c.price >> c.counts;
    return is;
}

int main(void)
{
    CSample a(1, 1.1, 1);
    CSample b(2, 2.2, 2);

    CSample c;
    CSample d;
    
    // 往文件写入数据
    ofstream fos("E:/out.dat");
    if(!fos)
    {
        cout << "can not open file..." << endl;
        exit(1);
    }

    fos << a << b;
    fos.close();

    // 从文件读出数据
    ifstream fis("E:/out.dat");
    if(!fis)
    {
        cout << "can not open file..." << endl;
        exit(1);
    }
 
    fis >> c >> d;
    fis.close();

    // 显示读出数据
    c.print();
    d.print();
 
    return 0;
}