Referencing

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Yagani
Posts: 17
Joined: May 19th, 2013, 3:31 pm

Referencing

Post by Yagani » June 2nd, 2013, 10:38 am

Hey Guys,

I've got a problem once again ._. In my solution I've created a separate header and cpp file. It has about five classes. In the most important class (Attis) I passed the graphics object as a reference. In that main class I pass the graphics object to all the other classes in the file. However, if I use the reference it gives me an unhandled exception reading at location ... . I tried to make it a reference to a reference and try to declare it globally (which was impossible because I have to declare it directly). Does anybody know a solution to this problem...? Opening my solution will clear things up I think.

Yagani
Attachments
Attis - kopie.rar
(485.69 KiB) Downloaded 141 times

User avatar
Nosferatu
Posts: 36
Joined: July 25th, 2012, 7:08 am
Location: Slovakia

Re: Referencing

Post by Nosferatu » June 2nd, 2013, 4:28 pm

In your Attis constructor
change cursor(gfx), ball(gfx),level(gfx) to cursor(gfxx),ball(gfxx),level(gfxx).
I have no idea why it works, but it does.
Maybe someone more experienced can explain this to us.

User avatar
viruskiller
Posts: 399
Joined: June 14th, 2012, 5:07 pm

Re: Referencing

Post by viruskiller » June 3rd, 2013, 2:44 am

dunno how or why this works, don;t have much time to look into it more, but lines like this

gfx(gfxx)
{
}
are confusing as hell, and as nosferatu pointed out if u use gfxx it works , that's because gfxx is the name of the reference u used for gfx.
i'd say u rename them all to something like rfgfx .

User avatar
viruskiller
Posts: 399
Joined: June 14th, 2012, 5:07 pm

Re: Referencing

Post by viruskiller » June 3rd, 2013, 3:01 am

well i just read the ehader file and u brought this to yourself, why using a reference of gfx, to initialize a copy of the gfx object? and use about the same name for both?

i'd say u leeave it working with pointers:)

User avatar
viruskiller
Posts: 399
Joined: June 14th, 2012, 5:07 pm

Re: Referencing

Post by viruskiller » June 3rd, 2013, 3:10 am

follow this for all classes
header

class Cursor
{
public:
Cursor(D3DGraphics& gfx);
~Cursor();
void DrawCursor();
void MoveLeft();
void MoveRight();
int GetxCor();
int GetyCor();
int GetLength();
private:
int cursorxCor;
int cursoryCor;
int cursorLength;
int cursorSpeed;
D3DGraphics * gfx;



.cpp file


Cursor::Cursor(D3DGraphics& gfx)
:
cursorLength(100),
cursorSpeed(8),
gfx(&gfx)

{
cursorxCor = (700 - cursorLength) / 2;
cursoryCor = 530;
}

Yagani
Posts: 17
Joined: May 19th, 2013, 3:31 pm

Re: Referencing

Post by Yagani » June 3rd, 2013, 4:14 pm

That works as planned. I've now also changed the variable names to a bit clearer ones lol :p

However, I have another question. How could I pass it on another time...? I use that when I pass the graphics object on from the Level class to the Block class. I wonder if there's another way to do it, because the Block class will be created quite often. Passing on the graphics object takes time, I assume. Wouldn't there be a more efficient, quicker way to accomplish the same?
Last edited by Yagani on June 3rd, 2013, 5:00 pm, edited 1 time in total.

Yagani
Posts: 17
Joined: May 19th, 2013, 3:31 pm

Re: Referencing

Post by Yagani » June 3rd, 2013, 4:19 pm

Got it working now, but it's only displaying a white window... I guess I'm doing something wrong :x

User avatar
viruskiller
Posts: 399
Joined: June 14th, 2012, 5:07 pm

Re: Referencing

Post by viruskiller » June 4th, 2013, 1:35 am

well looks like u enterd an infinite loop here:P
for(int y = blockyCor; y < y + blockHeight; y++)

and that's why the white screen

User avatar
viruskiller
Posts: 399
Joined: June 14th, 2012, 5:07 pm

Re: Referencing

Post by viruskiller » June 4th, 2013, 1:53 am

well i did some research and seems like i misunderstood references:P,i was thinkin of refrences as another form of pointers:) or a copy of the oject refered too:)

http://www.parashift.com/c++-faq-lite/o ... -refs.html

i guess u can go back to using references, but this time jsut don't use confusing names:P

also in the block code use xoff and y off, as it's confusing when u get to draw the lines or pixels..
if u have them both x and y then which x or y will it be used for drawing?

Yagani
Posts: 17
Joined: May 19th, 2013, 3:31 pm

Re: Referencing

Post by Yagani » June 4th, 2013, 5:40 am

And now my programs works flawless, yay! Thx :D

Post Reply