Click to See Complete Forum and Search --> : storing an object in a pointer-Please help


Kohinoor24
January 28th, 2002, 03:56 AM
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::out|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

Graham
January 28th, 2002, 08:08 AM
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

NMTop40
January 28th, 2002, 09:24 AM
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::out|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