CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2001
    Posts
    745

    storing an object in a pointer-Please help

    I have a code which goes like this.I want to store an object in a poinetr of the corr: class & want to retrieve it later.

    I tied this code,but it is not working




    #include<fstream>
    using namespace std;

    typedef short T_16bit;
    typedef int T_32bit;

    const char cCc ={'\x5A'};
    const char Length [] = {'\x00','\x00'};
    const char IDM[] = {'\xD3','\xAB','\xCA','\x00','\x00','\x00'} ;


    class C_ptx

    {
    public:
    C_ptx()
    {
    m_ptx.open("First",ios:ut|ios::binary);
    }

    void Createstream()
    {
    m_ptx.write(&cCc,sizeof(cCc));
    // m_ptx.write(Length,sizeof(Length));
    m_ptx.write(IDM,sizeof(IDM));

    }
    ofstream& returnstream()
    {
    return m_ptx;

    }

    private:
    ofstream m_ptx;

    };



    class space
    {

    public:
    void store(C_ptx &object)
    {
    *m_ptx = object;
    }

    C_ptx* retrieve()
    {
    return m_ptx;
    }

    private:

    C_ptx *m_ptx;

    };

    void main()
    {
    C_ptx *p = new C_ptx;
    p->Createstream ();
    space s1;
    //When I call here it shows unhandled exception
    s1.store(*p);

    }

    Kohinoor

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: storing an object in a pointer-Please help

    Change the signature of space::store to

    void store(C_ptx *object)




    He who breaks a thing to find out what it is, has left the path of wisdom - Gandalf
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: storing an object in a pointer-Please help


    include<fstream>
    using namespace std;

    typedef short T_16bit;
    typedef int T_32bit;

    const char cCc ={'\x5A'}; // do we want the braces here?
    const char Length [] = {'\x00','\x00'};
    const char IDM[] = {'\xD3','\xAB','\xCA','\x00','\x00','\x00'} ;


    class C_ptx
    {
    public:
    C_ptx()
    {
    m_ptx.open("First",ios:ut|ios::binary);
    // might want to validate with is_open
    }

    void Createstream()
    {
    m_ptx.write(&cCc,sizeof(cCc)); // correct if '\x5A' above
    // m_ptx.write(Length,sizeof(Length));
    m_ptx.write(IDM,sizeof(IDM));

    }
    ofstream& returnstream()
    {
    return m_ptx;

    }

    private:
    ofstream m_ptx;

    };

    class space
    {

    public:
    void store(C_ptx &object)
    {
    *m_ptx = object; // illegal as pointer is not allocated
    // will try to copy object into unallocated space. Are you
    // sure you don't want m_ptx = &object;
    }

    C_ptx* retrieve()
    {
    return m_ptx;
    }

    private:

    C_ptx *m_ptx; // pointer is never allocated

    };

    void main()
    {
    C_ptx *p = new C_ptx;
    p->Createstream ();
    space s1;
    //When I call here it shows unhandled exception
    s1.store(*p); // it would

    }





    The best things come to those who rate

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured