load a class problem

This is a discussion on load a class problem within the C++ Programming forums, part of the General Programming Boards category; well im am trying to load a class, it does compile,but doesnt work here's my code: Code: //prototype void loadS(Student(*)[10]); ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    45

    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)

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    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

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    45

    uuuh... and then?

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    Remove this "&" as well:
    Code:
    load.read((char*)&loading, 10 * sizeof(Student));
    gg

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    45
    hmm, still getting the error messagges.. is it because I use static integers in my class? (I have initialized them though)

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    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

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    648

    Re: load a class problem

    NOTICE! You need a semicolon to end the class.

    Code:
    //...
    
    class Student
    {
      ...   //some stuff, also void display()
    };  // <<<<<<<<< SEMICOLON!!!!!!
    
    //...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  2. problem with class
    By Mr.Pink in forum C++ Programming
    Replies: 26
    Last Post: 07-10-2005, 10:24 PM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. static class problem.
    By Sebastiani in forum C++ Programming
    Replies: 3
    Last Post: 10-16-2002, 03:27 PM
  5. Replies: 3
    Last Post: 12-03-2001, 12:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21