Help in writing a class to file and calling a constructor

This is a discussion on Help in writing a class to file and calling a constructor within the C++ Programming forums, part of the General Programming Boards category; Hello! I got one class named Hired and want to write the whole class into a file. But upon execution ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Norway
    Posts
    25

    Question Help in writing a class to file and calling a constructor (fairly simple)

    Hello!

    I got one class named Hired and want to write the whole class into a file. But upon execution the program stops and ends without even create the file.
    Also; how do I call the construtor that takes the INT paramter?.... Ex: Hired(int i = 1); does not seem to work, it still calls the Hired() that takes no parameters?!

    The CLASS
    Code:
    class Hired
    {
    private:
        int      nr;                       
        int      ant_barn;                 
    public:
        Hired (int n)
        {
            cout << "Hired nr: (100-999)" << endl;
            cin >> nr;
            cout << "Ant barn: 0-20" << endl;
            cin >> ant_barn;
        }
    
        Hired ()
        {
            cout << "\n\nWARNING 1: SHALL NOT RUN THIS CONSTRUCTOR\n\n";
        }
        void write_file(ofstream* out)
        {
            *out << nr;
            *out << ant_barn << endl;
        }
      
        void display()
        {
            cout << "Nr: " << nr;
            cout << "ant barn: " << ant_barn;
            cout << endl << endl;
        }
    GLOBALS
    Code:
    //Globals
    Hired* hired[100];     
    int lastused = 0;
    The main program.
    Code:
    int main()
    {
    //Filling in at least one "hired" with data:
        hired[lastused].Hired(lastused); //How do I call the Constructor that takes INT as a parameter in my class? SInce this line does not work!!
     I've also tried switching the "." with ->, did not work
        lastused = lastused + 1;
    
    //To file
        ofstream myfile("hired.dta", ios::out | ios::ate);
        for (i = 1; i < lastused; i++)
        {
            hired[i]->write_file(&myfile); //Failure here or/and line below... I've also tried "hired[i].write_file(&myfile);
            hired[i]->display();
        }
        myfile.close();
    
    
        //To memory
        Hired* a;
        ifstream myfile2("hired.dta", ios::in);
        if (myfile2)                            //Read data in to file
        {
            i = 0;
            myfile2.read((char*) &a, sizeof(Hired));
            while (!myfile2.eof() &&  lastused >= i)
            {
                myfile2.read((char*) &a, sizeof(Hired));
                hired[i] = a;
                ++i;
                hired[i]->display();
            }
        }
        cout << "DONE";
        return 0;
    }
    Marked the lines that I am stumbled at with red color.

    How do I call the constructor in my class, when my "hired" is a pointer with array of type Hired.

    PS: If I made the "hired" pointer array variable of no pointer type, this works, then I just switch "->" with ".". But I want to work with pointers now!
    PS2: CodeBlocks editor, compiler GCC
    Last edited by ManyTimes; 03-18-2010 at 05:27 PM.

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    38
    1.) You're trying to access a pointer that points to garbage.

    hired[lastused] = new Hired(int);

    2.) Don't try to call a constructor explicitally, it's called upon "construction." To call the constructor
    with arguments, initialize the class using that constructor:

    hired[lastused] = new Hired(50);

    3.) I'd move thouse couts and cins into your main loop.

    {
    std::cout << "Hired nr: ";
    std::cin >> nr;
    std::cout << "Ant barn: ";
    std::cin >> ant_barn;

    hired[lastused] = new Hired(nr, ant_barn);
    lastused++;
    hired[lastused]->DoStuff();
    }

    4.) When working with a pointer, use -> instead of .

    Hired h(50);
    h.write_file;

    OR

    Hired* h = new Hired(50);
    h->write_file;
    delete h;


    If you use new, you need to use delete somewhere.
    Last edited by nicoqwertyu; 03-18-2010 at 06:51 PM.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    Norway
    Posts
    25
    Works now! Lovely, love you! Knew it was simple, just...
    Last edited by ManyTimes; 03-18-2010 at 07:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-27-2009, 01:46 PM
  2. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 03:29 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  4. inet_aton()... Noob needs help with sockets :|
    By Maz in forum C++ Programming
    Replies: 3
    Last Post: 11-22-2005, 04:33 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM

Tags for this Thread


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