类函数的C ++提取运算符

时间:2014-05-29 04:48:05

标签: c++ file-io io operator-overloading iostream

我不确定运算符重载是否是我正在寻找的,但我需要知道在C ++中实现以下内容的最佳方法;

我有一个类Employee(为简单起见),只有一个ID号atm。请假设输入文件有一个int数字和一些字符(仅显示1行),例如:

1234 Charles Hammond

到目前为止,这是代码。我试图使用提取运算符从输入文件中获取整数和其他数据到我的类函数(SetID);

class Employee
{
    int employeeID;

public:
     void SetID(int);
}

void Employee::SetID(int empID)
{
    employeeID = empID;
}

int main(void)
{
    int lineCounter = 4;
    Employee emp;

    //Create filestream objects and open
    ifstream input;
    ofstream output;
    input.open("input.txt");
    output.open("output.txt");


    for (int i = 0; i < lineCounter; i++)
    {
        input >> emp.SetID(input.get()); //illegal? Best way to do this
    }


    //Close program
    output.close();
    input.close();
    system("pause");
    return 0;
}

我只是想从输入文件中获取ID并将其存储在类成员“employeeID”中以便稍后用于计算。

2 个答案:

答案 0 :(得分:2)

一个选项是重载>>运算符并使其成为Employee类中的友元函数。

类似的东西:

istream& operator>>( istream& in, Employee& emp )
{
    in >> emp.employeeID;
    return in;
}

在您的员工类中:

friend istream& operator>> (istream& in, Employee& emp);

答案 1 :(得分:2)

有很多方法可以做到这一点,每个方法都有优点和缺点。您正在阅读的数据格式表明您每行有一条“记录”,在这种情况下应以某种方式强制执行。以下是通过从输入文件中读取一行数据,然后通过字符串流发送该行以进行进一步处理:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

class Employee
{
    // friend this operator, as we'll give it access to our
    //  private data members of our class.
    friend std::istream& operator >>(std::istream& inp, Employee& obj);

    int employeeID;

public:
    void setID(int id) { employeeID = id; }
    int getID() const { return employeeID; }
};

// extracts a single employee from a single input line taken from
//  the passed input stream. the code below mandates one, and only
//  one employee per line.
std::istream& operator >>(std::istream& inp, Employee& obj)
{
    // used as a full-line-buffer to enforce single-record-per-line
    std::string line;
    if (std::getline(inp, line))
    {
        // think of it as an in-memory stream primed with our line
        // (because that's exactly what it is).
        std::istringstream iss(line);

        // TODO: eventually you'll want this to parse *all* values from
        //  the input line, not just the id, storing each in a separate
        //  member of the Employee object being loaded. for now we get
        //  only the id and discard the rest of the line.
        if (!(iss >> obj.employeeID))
        {
            // a failure to read from the line string stream should flag
            //  the input stream we read the line from as failed. we also
            //  output the invalid line to std::cerr.
            inp.setstate(std::ios::failbit);
            std::cerr << "Invalid format: " << line << std::endl;
        }
    }
    return inp;
}


int main()
{
    // open input and output files
    std::ifstream input("input.txt");

    // read at-most four employee lines from our file. If there are
    //  less than that or a read-error is encountered, we will break
    //  early.
    Employee emp;
    for (int i=0; i<4 && input >> emp; ++i)
    {
        // do something with this thing
        std::cout << "Read Employee: " << emp.getID() << '\n';
    }

    system("pause");
    return 0;
}