I'm so lost in my programming class. I'm trying to output vector[i] to a text file but can't seem to get it to compile. Am I missing something?
The empFile is created in another class
1 2 3 4 5 6 7 8 9 10 11 12 13
void Employee::WriteData(ofstream& empFile)
{
vector<Employee*> empVector;
empVector.push_back(new Employee(37, "Joe Brown", "123 Main St.", "123-6788", 45, 10.00));
empVector.push_back(new Employee(21, "Sam Jones", "45 East State", "661-9000", 30, 12.00));
empVector.push_back(new Employee(15, "Mary Smith", "12 High Street", "401-8900", 40, 15.00));
for (int i = 0; i < empVector.size(), i++;)
{
empVector[i] -> empFile.write()
}
empFile.close();
};
The empFile.write() doesn't seem to want to compile. It says Employee has no empFile member?
My teacher wants us to put this employee information in a text file with each element on it's own line so we can call the text file via another function and display it on screen. Any advice would be greatly appreciated!
Your vector contains pointer to Employee objects.
On line 8 of your example you are calling the non-existant empFile member of each element in your vector:
empVector[i] -> empFile.write()
You probably want to do something along the lines of this:
Thanks, NwN. I'm not sure how to set up the vector to split the data like you have with .id and .name. I might forgo the vector and create the objects manually. My problem is when I do that my text file is just getting zeros. I'm doing something wrong, obviously. If I create an object like what is below, can I use methods to write to the text file like this?
I tried running the program like this and it just writes 3 zeros on one line but I think it is because I'm not referencing the object "joe" in any of these functions. How do I alleviate this?
The problem is not in using a vector, that's fine. It's your Employee object that needs to provide the functionality. I would have to see the definition of Employee to go into more detail.
Classes are like blueprints. A paper describing an employee, is not an actual Employee. When you do Employee joe(...) you create an actual employee according to the blueprint you have defined.
So, supposing that those member functions you mentioned are defined in the Employee class, you would have to call them specifically on "joe" as such: