-
load a class problem
well im am trying to load a class, it does compile,but doesnt work :(
here's my code:
Code:
//prototype
void loadS(Student(*)[10]);
class Student
{
... //some stuff, also void display()
}
// in "int main()"
Student student[10];
loadS(student&); // loading it
void loadS(Student* loading[])
{
ifstream load;
load.open("Data.dat",ios::out | ios::binary);
// theres some data in Data.dat
load.read((char*)&loading, sizeof(Student));
load.close();
loading[0]->display(); //just a display test
loading[1]->display();
return;
}
//info in data.dat should be in "student" now
It is supposed to load the class with 10 objects, wich is in data.dat, in (Student)student.
this is the error:
Make.obj : error LNK2001: unresolved external symbol "void __cdecl load(class Student (*)[10])" (?loadS@@YAXPAY09VStudent@@@Z)
-
Use a single pointer to pass an array to a function (this is one way):
Code:
void foo(int *array, int length)
{
array[0] ...
array[1] ...
...
array[length - 1] ...
}
...
int n[10];
foo(n,10);
gg
-
:confused: :confused: :confused: :confused:
uuuh... and then?
-
As mentioned, pass the array of students as a single pointer, not a double pointer. An array is basically a pointer.
You say it compiles? Well, this doesn't look right to me:
loadS(student&);
You mean
loadS(&student);
???
If you switch to a single pointer, leave the & out.
Also, if you want to read 10 students, either make a loop or read 10 times the data (see below).
Finally, make sure the open() succeeds before attempting a read. This can be done using load.fail().
Code:
//prototype
void loadS(Student(*)[10]);
class Student
{
... //some stuff, also void display()
}
// in "int main()"
Student student[10];
loadS(student); // loading it
void loadS(Student* loading)
{
ifstream load;
load.open("Data.dat",ios:ut | ios::binary);
if(load.fail()) return;
// theres some data in Data.dat
load.read((char*)&loading, 10 * sizeof(Student));
load.close();
}
//info in data.dat should be in "student" now
-
Remove this "&" as well:
Code:
load.read((char*)&loading, 10 * sizeof(Student));
gg
-
hmm, still getting the error messagges.. is it because I use static integers in my class? (I have initialized them though)
-
This is wrong:
Code:
void loadS(Student(*)[10]);
Get rid of it completely or use the correct prototype. (See post by Magos)
Then go back and review pointers in your C/C++ book.
gg
-
Re: load a class problem
NOTICE! You need a semicolon to end the class.
Code:
//...
class Student
{
... //some stuff, also void display()
}; // <<<<<<<<< SEMICOLON!!!!!!
//...