What am I doing wrong ?

W

watkinsdev

Hi,

I have created a mesh class in visual studio 6.0 c++.

I can create a device, render objects and can edit the objects by for
instancnce selecting a cluster of vertices and processing the vertices
and can do this multiple times on a sinlge vertex cluster.

The problem I have been encoutering is that, if I select a second
vertex cluster and try to edit that , the program crashes.

I have re-written the application from passing pointers to the mesh
object to functions to an application that passes references but I
still get the same type of problem.

in my current version, I declare a reference, m_ref and then pass
this object to the following sequence of function calls :

create sphere ( my_mesh& p_mesh ) returning p_mesh.

select_vertex_cluster ( my_mesh& p_mesh ) returning p_mesh.

Edit_vertex_cluster (( my_mesh& p_mesh ) returning p_mesh.)

If I call the Edit_vertex_cluster , for a second time, it just cannot
find any data, that has been created previously within the object.

I am loosing quite a lot of hair with this one and I have inplimented
a copy constructor but , really I am so confused right now that I just
have no idea what I should be doing .

Can anyone see what type of thing I may be doing wrong here?

I really would appriciate some help with this.

Thanks in advance,

Robert W.
 
J

Jim Langston

Hi,

I have created a mesh class in visual studio 6.0 c++.

I can create a device, render objects and can edit the objects by for
instancnce selecting a cluster of vertices and processing the vertices
and can do this multiple times on a sinlge vertex cluster.

The problem I have been encoutering is that, if I select a second
vertex cluster and try to edit that , the program crashes.

I have re-written the application from passing pointers to the mesh
object to functions to an application that passes references but I
still get the same type of problem.

in my current version, I declare a reference, m_ref and then pass
this object to the following sequence of function calls :

create sphere ( my_mesh& p_mesh ) returning p_mesh.

select_vertex_cluster ( my_mesh& p_mesh ) returning p_mesh.

Edit_vertex_cluster (( my_mesh& p_mesh ) returning p_mesh.)

If I call the Edit_vertex_cluster , for a second time, it just cannot
find any data, that has been created previously within the object.

I am loosing quite a lot of hair with this one and I have inplimented
a copy constructor but , really I am so confused right now that I just
have no idea what I should be doing .

Can anyone see what type of thing I may be doing wrong here?

I really would appriciate some help with this.

Thanks in advance,

I would guess that you are attempting to reseat a reference, which you can
not do. Other than that, I would have to see the code.
 
W

watkinsdev

I would guess that you are attempting to reseat a reference, which you can
not do. Other than that, I would have to see the code.- Hide quoted text -

- Show quoted text -

Is a reference reset if a return value is passed back to it ?
 
R

Rolf Magnus

Sounds reasonable, if that was the only change you made.

You mean the object it refers to? Why are you creating a reference first?

There is not enough information to say what is wrong.
Is a reference reset if a return value is passed back to it ?

I have no idea what you mean by that. There is no way to "reset" a
reference. Everything you do after it's initialized is done to the object
it refers to.
 
W

watkinsdev

Sounds reasonable, if that was the only change you made.


You mean the object it refers to? Why are you creating a reference first?


There is not enough information to say what is wrong.



I have no idea what you mean by that. There is no way to "reset" a
reference. Everything you do after it's initialized is done to the object
it refers to.

Sorry, I am not wearing glasses, reset/reseat

Anyhow here's some code ,I apologie if it looks, un-together.

//////////////////////////////////////////////////////////////////////
// Setting up the references and pointers, Globaly
//////////////////////////////////////////////////////////////////////

m_model& m_ref = oooo;
m_model& b_ref = uuuu;
m_model* tmp;
m_model* z_mesh;

///////////////////////////////
// create sphere
///////////////////////////////

tmp = new m_model
;
D3DXCreateSphere(my3d_device,0.5, 32, 32,&tmp->m_Mesh, NULL);
m_ref = oooo;
m_ref = m_ref.copy_sphere(my3d_device,tmp, m_ref );
m_ref = m_ref.m_x_rotate(m_ref, 90.0, 1);
oooo.X = 0;
oooo.Y = 0;
oooo.Z = 0;
cluster_mesh[mesh_index] = m_ref;
mesh_index++;

///////////////////////////////////////
// Getting and setting a vertex cluster
///////////////////////////////////////

if( f_thru == true )
{
m_ref.vIndex = new WORD [4];
m_ref.Cull_Type = new int [4];
f_thru = false;
}
else
{
delete[] m_ref.vIndex;
m_ref.vIndex = new WORD [4];
delete[] m_ref.Cull_Type;
m_ref.Cull_Type = new int [4];
i_pos = 0;
}

switch(type_cursor)
{
case 1:

numberOfpoints = 4;

break;

. . .

}

m_ref.Get_cursor_Onject_data(m_ref, type_cursor, numberOfpoints);

/////////////////////////
//Edit mesh object
/////////////////////////
m_ref = m_ref.process_Sub_Trunc_Level_Extrude( m_ref,
my3d_device ,
scale_value, true, e_type );

cluster_mesh[0] = m_ref;
 
J

Jim Langston

Is a reference reset if a return value is passed back to it ?

No. I made that mistake once and couldn't figure out why everything was
working for the first set of objects in my code but not the other 2 with the
same code. I was doing something like this:

MyClass& thisClass = (*it);
thisClass.name = "Hello";
// ... etc..

++it;
thisClass = (*it);
thisClass name = "Goodbye";
// .. etc...

Well, this just didn't work as I expected. I wound up changing the first
class twice and I actually posted in this newsgroup asking why. And of
course as soon as someone told me it was a dohhh!

MyClass& thisClass = (*it);
seats the refernce. thisClass points to the instance that it was pointing
to (in my case an iterator). So when I did assignments after it, it made
changes to where it was pointing to and all was well.

But, when I incremented my iterator, then tried to reseat the reference with
thisClass = (*it);
what was actually happening was that the reference was STILL pointing to
where it was before, it doesn't change. So it became a simple assignment.
Like a = b. So now the first instance got copied over with where it was
pointing to now.

The only time you can seat a refernce is on the declaration/defination line:
sometype& varname = // this is seating the refernce
or in a structure/class initalization list

class MyClass
{
public:
MyClass( sometype& thisInstance ): MyRef( thisInstance ) {} // This is
where it gets seated
private:
sometype& MyRef;
};
 
W

watkinsdev

No. I made that mistake once and couldn't figure out why everything was
working for the first set of objects in my code but not the other 2 with the
same code. I was doing something like this:

MyClass& thisClass = (*it);
thisClass.name = "Hello";
// ... etc..

++it;
thisClass = (*it);
thisClass name = "Goodbye";
// .. etc...

Well, this just didn't work as I expected. I wound up changing the first
class twice and I actually posted in this newsgroup asking why. And of
course as soon as someone told me it was a dohhh!

MyClass& thisClass = (*it);
seats the refernce. thisClass points to the instance that it was pointing
to (in my case an iterator). So when I did assignments after it, it made
changes to where it was pointing to and all was well.

But, when I incremented my iterator, then tried to reseat the reference with
thisClass = (*it);
what was actually happening was that the reference was STILL pointing to
where it was before, it doesn't change. So it became a simple assignment.
Like a = b. So now the first instance got copied over with where it was
pointing to now.

The only time you can seat a refernce is on the declaration/defination line:
sometype& varname = // this is seating the refernce
or in a structure/class initalization list

class MyClass
{
public:
MyClass( sometype& thisInstance ): MyRef( thisInstance ) {} // This is
where it gets seated
private:
sometype& MyRef;



};- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

Thanks for that, would it be possible to direct me to some literature
relating to this as I guess I am still feeling, slightly 'goofy'.
 
J

Jim Langston

Sorry, I am not wearing glasses, reset/reseat

Anyhow here's some code ,I apologie if it looks, un-together.

//////////////////////////////////////////////////////////////////////
// Setting up the references and pointers, Globaly
//////////////////////////////////////////////////////////////////////

m_model& m_ref = oooo;

Okay, m_ref is pointing at the variable oooo;
m_model& b_ref = uuuu;

b_ref is pointing to the variable uuuu;
m_model* tmp;
m_model* z_mesh;
///////////////////////////////
// create sphere
///////////////////////////////

tmp = new m_model
;
D3DXCreateSphere(my3d_device,0.5, 32, 32,&tmp->m_Mesh, NULL);
m_ref = oooo;

And you are assigning m_ref to itself. m_ref is already pointing to oooo;
So this is the exact same as saying:
oooo = oooo;
You can not change where m_ref is pointing to at this point, but then I'm
not sure what you're trying to do here anyway since you aleady pointed m_ref
to oooo this seems to be a noop.
m_ref = m_ref.copy_sphere(my3d_device,tmp, m_ref );

Okay, you're assinging what m_ref is pointing to (oooo) whatever
m_ref.copy_sphere returns.
m_ref = m_ref.m_x_rotate(m_ref, 90.0, 1);

And now you're assigning what m_ref is pointing to (oooo) whatever
m_ref.m_x_rotate returns. So what were you planing on doing witth the
previous assignment?
oooo.X = 0;

And now you're setting the variable x in oooo (which is still the same place
m_ref is pointing to) a value.
oooo.Y = 0;
oooo.Z = 0;

same with these 2.
cluster_mesh[mesh_index] = m_ref;

Okay, so now cluster_mesh[mesh_index] is assigned the instance of oooo

Pretty much stopping commening on this code.
mesh_index++;

///////////////////////////////////////
// Getting and setting a vertex cluster
///////////////////////////////////////

if( f_thru == true )
{
m_ref.vIndex = new WORD [4];
m_ref.Cull_Type = new int [4];
f_thru = false;
}
else
{
delete[] m_ref.vIndex;
m_ref.vIndex = new WORD [4];
delete[] m_ref.Cull_Type;
m_ref.Cull_Type = new int [4];
i_pos = 0;
}

switch(type_cursor)
{
case 1:

numberOfpoints = 4;

break;

. . .

}

m_ref.Get_cursor_Onject_data(m_ref, type_cursor, numberOfpoints);

/////////////////////////
//Edit mesh object
/////////////////////////
m_ref = m_ref.process_Sub_Trunc_Level_Extrude( m_ref,
my3d_device ,
scale_value, true, e_type );

cluster_mesh[0] = m_ref;

You say that this code works the first time you run through it. I don't
know if that's by luck or hapinstance, but a lot of the code just isn't
making sense to me. You assign the reference m_ref to pont to oooo then
continue to use oooo some places anyway. If you're going to use oooo then
don't bother with a reference. A reference is just an alias. So after you
do:

m_model& m_ref = oooo;

every time you refer to m_ref you are refering to oooo. Change every
occurance of m_ref in this code with oooo and it should do the exact same
thing (except for the declaration of m_ref of course).

I don't think references are doing what you think they're doing. Or I"m
missing something.
 
W

watkinsdev

Sorry, I am not wearing glasses, reset/reseat
Anyhow here's some code ,I apologie if it looks, un-together.
//////////////////////////////////////////////////////////////////////
// Setting up the references and pointers, Globaly
//////////////////////////////////////////////////////////////////////
m_model& m_ref = oooo;

Okay, m_ref is pointing at the variable oooo;
m_model& b_ref = uuuu;

b_ref is pointing to the variable uuuu;
m_model* tmp;
m_model* z_mesh;
///////////////////////////////
// create sphere
///////////////////////////////
tmp = new m_model
;
D3DXCreateSphere(my3d_device,0.5, 32, 32,&tmp->m_Mesh, NULL);
m_ref = oooo;

And you are assigning m_ref to itself. m_ref is already pointing to oooo;
So this is the exact same as saying:
oooo = oooo;
You can not change where m_ref is pointing to at this point, but then I'm
not sure what you're trying to do here anyway since you aleady pointed m_ref
to oooo this seems to be a noop.
m_ref = m_ref.copy_sphere(my3d_device,tmp, m_ref );

Okay, you're assinging what m_ref is pointing to (oooo) whatever
m_ref.copy_sphere returns.
m_ref = m_ref.m_x_rotate(m_ref, 90.0, 1);

And now you're assigning what m_ref is pointing to (oooo) whatever
m_ref.m_x_rotate returns. So what were you planing on doing witth the
previous assignment?
oooo.X = 0;

And now you're setting the variable x in oooo (which is still the same place
m_ref is pointing to) a value.
oooo.Y = 0;
oooo.Z = 0;

same with these 2.
cluster_mesh[mesh_index] = m_ref;

Okay, so now cluster_mesh[mesh_index] is assigned the instance of oooo

Pretty much stopping commening on this code.




mesh_index++;
///////////////////////////////////////
// Getting and setting a vertex cluster
///////////////////////////////////////
if( f_thru == true )
{
m_ref.vIndex = new WORD [4];
m_ref.Cull_Type = new int [4];
f_thru = false;
}
else
{
delete[] m_ref.vIndex;
m_ref.vIndex = new WORD [4];
delete[] m_ref.Cull_Type;
m_ref.Cull_Type = new int [4];
i_pos = 0;
}
switch(type_cursor)
{
case 1:
numberOfpoints = 4;

. . .

m_ref.Get_cursor_Onject_data(m_ref, type_cursor, numberOfpoints);
/////////////////////////
//Edit mesh object
/////////////////////////
m_ref = m_ref.process_Sub_Trunc_Level_Extrude( m_ref,
my3d_device ,
scale_value, true, e_type );
cluster_mesh[0] = m_ref;

You say that this code works the first time you run through it. I don't
know if that's by luck or hapinstance, but a lot of the code just isn't
making sense to me. You assign the reference m_ref to pont to oooo then
continue to use oooo some places anyway. If you're going to use oooo then
don't bother with a reference. A reference is just an alias. So after you
do:

m_model& m_ref = oooo;

every time you refer to m_ref you are refering to oooo. Change every
occurance of m_ref in this code with oooo and it should do the exact same
thing (except for the declaration of m_ref of course).

I don't think references are doing what you think they're doing. Or I"m
missing something.- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

Thank you for reply, as you can see I have been slighlty confused. But
from reading your reply's I now understand that a reference, should
not be passed back from a function as this would mean the reference
would be reassigned.


I figured that I needed to convert my application from using pointers
to references because I needed to impliment a copy constructor.
However, I have ordered the strustrup book as my c++ archetecture
seems like it requires some work.

Thanks for your help.
 

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

Staff online

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,093
Latest member
EmiliaAlfo

Latest Threads

Top