-
Question
ok im trying to read in data from a file with this info
Ray L. Jones 3942 6 4 1974 3 2 2001
and i have a class like this
Code:
class Instructor {
public:
Instructor(); // constructor
~Instructor();
private:
string firstName;
string lastName;
string middleInitial;
string employeeID;
}; // end class Instuctor
and my code in main looks like this. The line that says
inFile >> *(I[i_index]);
is not executing , it gives me an error and i think i would need to use operator overloading on a function for that line to be valid but im not sure.
Code:
Instructor *I[MAX_INSTRUCTORS] = {NULL_POINTER};
for(int i_index = LOOP_BASE; i_index < MAX_INSTRUCTORS; i_index++)
{
I[i_index] = new Instructor();
inFile >> *(I[i_index]);
//What must I do to my class Instructor to make the line of code above work?
//cout << "The new instructor is " << *(I[i_index]) << ".\n";
}
-
Try
inFile.read(reinterpret_cast<char *>(I[i_index]) sizeof(Instructor));
Although I think you're going to need to use a binary file with NULL seperating your strings.
-
I can't do that i really need to make that particular line of code work, does anyone have any ideas.
-
I used this for my string class. You can easily change it to make it work for your Instructor class.
Code:
//In your .h file
friend ostream& operator<<(ostream&,const String&);
friend istream& operator>>(istream&,String&);
//In your .cpp file
ostream& operator<<(ostream& o, const String& newStr)
{
return o << newStr.lpzstr;
}
istream& operator>>(istream& i,String& obj)
{
char buffer[1024];
i.getline(buffer,1023,'\n');
delete [] obj.lpzstr;
obj.lpzstr = new char[strlen(buffer)+1];
strcpy(obj.lpzstr, buffer);
return i;
}
-
I see your code, but i don't see how that code would make this a vaild line of code
Code:
inFile >> *(I[i_index]);
How will that read from the file?
-
Hmm.... You were supposed to figure it out.
What, exactly, must happen when you do the following :
Code:
inFile >> *(I[i_index]);
-
its supposed to read in data from a file that looks like this
Ray L. Jones 3942 6 4 1974 3 2 2001
Alice K. Brown 8300 4 8 1969 2 1 1999
and store into a pointer of class Instructor which i have defined above and the line below the one that looks like this
Code:
inFile >> *(I[i_index]);
then the line that prints the code looks like this
Code:
cout << "The new instructor is " << *(I[i_index]) << ".\n";
which will print out something like this:
The new instructor is Ray L. Jones 3942
-
Why not add a function to your class to output the data and call that?
-
because i can't do that, i have to make it fit what is already in main.
-
ok i have overloaded the >> operator and in main i have a line of code like this Instructor *S[3] which is an array of 3 pointers to class Instructor and then also in main is the line:
inFile >> *(I[i_index]);
now the function that matches this is in my class Instructor which looks like this:
Code:
friend istream &operator>>(ifstream &, Instructor *);
private:
string firstName;
string lastName;
string middleInitial;
string employeeID;
but in my definition of the function it says firstName, lastName and so on are undeclared
Code:
stream &operator>>(ifstream &I, Instructor *S)
{
I>>firstName>>middleInitial>>lastName>>employeeID;
}
-
I'm doubting that you wrote this code yourself, or you're extremely sleepy. Think about it...
Code:
const ifstream &operator>>(ifstream &I, Instructor *S)
{
I >> S->firstName >> S->middleInitial >> S->lastName
>> S->employeeId;
return I;
}
-
This code is in main
Code:
for(int i_index = LOOP_BASE; i_index < MAX_INSTRUCTORS; i_index++)
{
I[i_index] = new Instructor();
inFile >> *(I[i_index]);
This is in class Instructor
Code:
class Instructor {
public:
Instructor(); // constructor
~Instructor();
friend istream &operator>>(ifstream &I, Instructor *S);
and this is in the member functions file
Code:
ifstream &operator>>(ifstream &I, Instructor *S)
{
I >> S->firstName >> S->middleInitial >> S->lastName
>> S->employeeID;
}
it gives me this error
Compiling...
Driver.cpp
C:\Documents and Settings\Massa\Driver.cpp(91) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class Instructor' (or there is no acceptable conversion)
Error executing cl.exe.
Driver.exe - 1 error(s), 0 warning(s)
-
Again wake up...and think a little...
Code:
inFile >> *(I[i_index]);
in the above line you're passing an Instructor object to the operator NOT its address. You either need to change it, or change the operator definition...
Code:
const ifstream &operator>>(ifstream &I, Instructor &S)
{
I >> S.firstName >> S.middleInitial >> S.lastName
>> S.employeeID;
}
-
dont forget to return the ifstream reference in the defintion...
-
-
Then when you have done that try doing it the better way.
Those operators need not be friends. If they need not then they should not be friends.They cannot be members either. If you cant work out how this is done i'll tell you tomorrow. btw. its real simple.