Me again with same problem.

J

JoeC

It is me again with the same problem with my game. I can get data from
one object to another.
I have my player play graphic stored in a vector. I would like to get
that data out of tha object and display my graphics in the part that
displays the graphics. I have made significant changes in my program
but I can't get it to work.

class space{
char gchar;
graphic *gr;
graphic *grDefault;
graphic *cgr;
player * play;
bool seen;

graphic& space::graphicOut(){

if(play){
vector<BYTE>t;
play->data(t);
cgr = new graphic(t);
return *cgr;
}
//if(play){return *cgr;}
if(seen){return *gr;}
else {return *grDefault;}
}

void data(vector<BYTE>& d){
for(int lp = 0; lp != 32; lp++){
//d.push_back(gData[lp]);
}
}

I have been trying many thing to make this work I down't understand why
I can't return the graphics vector or the individual datas to retrun
out of this object.
 
A

Alf P. Steinbach

* JoeC:
It is me again with the same problem with my game. I can get data from
one object to another.
I have my player play graphic stored in a vector. I would like to get
that data out of tha object and display my graphics in the part that
displays the graphics. I have made significant changes in my program
but I can't get it to work.

class space{
char gchar;
graphic *gr;
graphic *grDefault;
graphic *cgr;
player * play;
bool seen;

graphic& space::graphicOut(){

if(play){
vector<BYTE>t;
play->data(t);
cgr = new graphic(t);
return *cgr;
}
//if(play){return *cgr;}
if(seen){return *gr;}
else {return *grDefault;}
}

void data(vector<BYTE>& d){
for(int lp = 0; lp != 32; lp++){
//d.push_back(gData[lp]);
}
}

I have been trying many thing to make this work I down't understand why
I can't return the graphics vector or the individual datas to retrun
out of this object.

Your code is hard to follow, that's probably what's makes it hard to create.

Split the problems into smaller, more well-defined problems.

When a problem is well-defined it is much easier to solve.
 
J

JoeC

Sorry, I got fustrated and careless in my attempts to make this work.

I made some changes that should work but my program crashes.

class player{
string name;
vector<BYTE>gData;
void create();

public:
player();
BYTE data(const int);
void dummy(){MessageBox(NULL, "Dummy" , "Notice", MB_OK); }
};

BYTE player::data(const int n){return gData[n];}

class space{

player * play;
.....

graphic& space::graphicOut(){

if(play){
vector<BYTE>t;
for(int lp = 0; lp != 32; lp++){
t.push_back(play->data(lp));
}
cgr = new graphic(t);
return *cgr;
}
//if(play){return *cgr;}
if(seen){return *gr;}
else {return *grDefault;}
}

What I am trying to do is read the vector from my player into a new
vector in my graphicOut function. My problem is my program keeps
crashing when I try to get data from my player object.
 
J

Jim Langston

JoeC said:
Sorry, I got fustrated and careless in my attempts to make this work.

I made some changes that should work but my program crashes.

class player{
string name;
vector<BYTE>gData;
void create();

public:
player();
BYTE data(const int);
void dummy(){MessageBox(NULL, "Dummy" , "Notice", MB_OK); }
};

BYTE player::data(const int n){return gData[n];}

class space{

player * play;
....

graphic& space::graphicOut(){

if(play){
vector<BYTE>t;
for(int lp = 0; lp != 32; lp++){
t.push_back(play->data(lp));
}

what is play->data(lp) supposed to be giving you? It would be nice if you
actually showed us what BYTE data( const int); was doing, as that's where
your problem might be.
Also, what is BYTE defined as? I could presume unsigned char, but I might
be presuming wrong.

How do you know how much data the vector? Are there actually 32 values?
Are you sure? How do you know? How do we know?

Why do you need to copy it anyway? Why don't you just return a pointer or a
reference to it?
 
A

Alf P. Steinbach

* JoeC:
Sorry, I got fustrated and careless in my attempts to make this work.

I made some changes that should work but my program crashes.

class player{
string name;
vector<BYTE>gData;
void create();

public:
player();
BYTE data(const int);
void dummy(){MessageBox(NULL, "Dummy" , "Notice", MB_OK); }
};

BYTE player::data(const int n){return gData[n];}

class space{

player * play;
....

graphic& space::graphicOut(){

if(play){
vector<BYTE>t;
for(int lp = 0; lp != 32; lp++){
t.push_back(play->data(lp));
}
cgr = new graphic(t);
return *cgr;
}
//if(play){return *cgr;}
if(seen){return *gr;}
else {return *grDefault;}
}

What I am trying to do is read the vector from my player into a new
vector in my graphicOut function. My problem is my program keeps
crashing when I try to get data from my player object.

There are many possible technical reasons, including (1) 'play' is a
bogus pointer, (2) play->gData doesn't currently hold 32 items or more,
(3) the 'graphic' constructor fails.

Starting with (1), your original post indicated that class 'space' does
not have any constructor defined by you (although as shown in that post
the class would have all members private and be unusable, so it could
not be real code: please consult the FAQ about how to post examples).
The default constructor supplied by the compiler doesn't initialize the
non-class type members, and all your members are non-class type. Thus,
the probability of a bogus 'play' pointer is quite high.

To reduce the probability of that sort of thing happening, make all the
class data members private, if they aren't already, and make sure to
initialize them all in every constructor you define. Also keep in mind
that when you have pointer members, you need to either use
smart-pointers or take manually charge of copying or disable copying.
The simplest is to disable copying: declare a private copy constructor
and a private assignment operator, and simply don't define them.
 
J

JoeC

what is play->data(lp) supposed to be giving you? It would be nice if
you
actually showed us what BYTE data( const int); was doing, as that's
where
your problem might be.
Also, what is BYTE defined as? I could presume unsigned char, but I
might
be presuming wrong.

BYTE pgr[] = {0xfc,0x3d,0xf8,0x1d,0xf8,0x1d,0xfc,0x3d,
0xfe,0x7d,0xf0,0x18,0xe8,0x0d,0x8c,0x31,
0x0c,0x3f,0x0c,0x3f,0x0b,0xdf,0x9b,0xdf,
0xfb,0xdf,0xfb,0xdf,0xfb,0xdf,0xf3,0xcf};

for(int lp = 0; lp != 32; lp++)
gData.push_back(pgr[lp]);
}

How do you know how much data the vector? Are there actually 32
values?
Are you sure? How do you know? How do we know?

Yes, all my data has 32 numbers. A BYTE is what I need to create data,
it is built into the windows.h library.

Why do you need to copy it anyway? Why don't you just return a pointer
or a
reference to it?

I don't think I have tried that how would that work with a vector?
 
J

JoeC

class space{
char gchar;
graphic *gr;
graphic *grDefault;
graphic *cgr;
player * play;
bool seen;

public:
space();
~space();
void graphicIn(char g);
graphic& graphicOut();
void playIn(player*);
bool isPlay();
void see(){seen = true;}
bool been(){return seen;}
void playOut();
bool canMove();
bool winspace();
};

space::space(){
play = 0;
grDefault = new graphic();;
seen = false;
}

later I put the player in:

void space::playIn(player *p){
play = p;
seen = true;
}

Here is my space constructor, how can I make it better?

The point of using pointers is that I want one player to be moved from
space to space, I don't want each space to have a player but the
potential to hold one. This concept actually worked with a simikkiar
program where I returned a character in stead of a an object. It
worked fine.

..To reduce the probability of that sort of thing happening, make all
the
..class data members private, if they aren't already, and make sure to
..initialize them all in every constructor you define.

Which class, the player.

..Also keep in mind
..that when you have pointer members, you need to either use
..smart-pointers or take manually charge of copying or disable copying.
..The simplest is to disable copying: declare a private copy constructor
and a private assignment operator, and simply don't define them.


so play = p, I need a copy constructor or do I need an assignment (=)?
This could be my problem.

Here is player:

I would appreeciate tips, if you have time.

class player{
string name;
vector<BYTE>gData;
void create();

public:
player();
player(const player&) ; <-to add
BYTE& data();
void dummy(){MessageBox(NULL, "Dummy" , "Notice", MB_OK); }
};

player::player(){
name = "";
create();
}

void player::create(){

BYTE pgr[] = {0xfc,0x3d,0xf8,0x1d,0xf8,0x1d,0xfc,0x3d,
0xfe,0x7d,0xf0,0x18,0xe8,0x0d,0x8c,0x31,
0x0c,0x3f,0x0c,0x3f,0x0b,0xdf,0x9b,0xdf,
0xfb,0xdf,0xfb,0xdf,0xfb,0xdf,0xf3,0xcf};

for(int lp = 0; lp != 32; lp++)
gData.push_back(pgr[lp]);
}

BYTE player::data(const int n){return gData[n];}

I am going to work on the ideas you gave me I think you have come
pretty close to telling me what my problem is. That does make sence.

Originally I had the player create a graphic class and then just return
it and I have been fiddeling with this program for a while. But the
purpsoe of what I do is learning.

Thanks, I hope I have time to inoperate the changes soon.


My cable went out but here is what I added:

player::player(const player& pl){
name = pl.name;
gData = pl.gData;
}

player& player::eek:perator =(const player& pl){
if(&pl != this){
name = pl.name;
gData = pl.gData;
}
return *this;
}

It still crashes at this point:

graphic& space::graphicOut(){

if(play){
vector<BYTE>t;
for(int lp = 0; lp != 32; lp++){
//t.push_back(play->data(lp));
}
cgr = new graphic(t);
return *cgr;
}
//if(play){return *cgr;}
if(seen){return *gr;}
else {return *grDefault;}
}

I am just trying to get it to work and later I want to make it better.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top