Sign in to follow this  

changing c_vector3d to *c_vector3d

This topic is 4165 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, so I continue in my quest to build a bunch of 3D geometry classes. I used to have a triangle class that had 3 c_vetor3d members (the points a, b, c forming the triangle). But now I changed it so that, instead of having 3 vertexes stored, my triangle class has 3 pointers to vertexes stored. Then I created the c_mesh class that has a vector (std::vector) of triangles and meshes, so that the triangles can get their vertex from some place. When I tried to compile, tought, I was (well, obviously to be honest) swarmed with errors. Some I managed to correct/make them stop showing, but now I'm reduced to two: The first one is on the functions:
//in geometry.cpp
c_vector3d c_triangle::*Get_a(){return a;}
c_vector3d c_triangle::*Get_b(){return b;}
c_vector3d c_triangle::*Get_c(){return c;}

//They are defined in "geometry.h" as:
    c_vector3d * a;
    c_vector3d * b;
    c_vector3d * c;


will get an error 'a' undeclared. I believe this is auto-fix once I get the others one fixed, so I'll move on: (this is kind of solved, see EDIT)
c_triangle::c_triangle(c_vector3d &value_a, c_vector3d &value_b, c_vector3d &value_c)
{
    a = *value_a;
    b = *value_b;
    c = *value_c;
}
//defined as:
c_triangle(c_vector3d &value_a, c_vector3d &value_b, c_vector3d &value_c);


The error I get is no mach for 'operator*' in '*value_a' Anyway, I have to go now, and will probably try to make some more research when I get back home, but i just tought it would be better to post this earlier. EDIT: well, the second problem disapeared when I changed a = *value_a; to a = &value_a; (wow, now it looks extremely obvious!)... but the 'a' undecleared remains... how can this be? I'm stearing at the 'a' declaration right now...

Share this post


Link to post
Share on other sites
You need to change get functions like this:

//in geometry.cpp
// you are returning pointers
c_vector3d* c_triangle::Get_a(){return a;}
c_vector3d* c_triangle::Get_b(){return b;}
c_vector3d* c_triangle::Get_c(){return c;}



Share this post


Link to post
Share on other sites
Quote:
Original post by algumacoisaqualquer
the 'a' undecleared remains... how can this be? I'm stearing at the 'a' declaration right now...


You should post the complete declaration of relevant classes, not just "relevant fragments", which, to outside observer, may seem a bit too messy to read.

Share this post


Link to post
Share on other sites
Quote:
Original post by deffer
Quote:
Original post by algumacoisaqualquer
the 'a' undecleared remains... how can this be? I'm stearing at the 'a' declaration right now...


You should post the complete declaration of relevant classes, not just "relevant fragments", which, to outside observer, may seem a bit too messy to read.


Yes, sorry about that. I tought that, by posting the entire class, I would be placing too much information, making it harder to people to understand what is going on. Anyway, here's the definition:


class c_triangle
{

c_vector3d normal;
public:
c_vector3d * a;
c_vector3d * b;
c_vector3d * c;

c_triangle(c_vector3d &value_a, c_vector3d &value_b, c_vector3d &value_c);

c_vector3d *Get_a();
c_vector3d *Get_b();
c_vector3d *Get_c();

c_vector3d Get_normal();

void Set_a(c_vector3d *value_a);
void Set_b(c_vector3d *value_b);
void Set_c(c_vector3d *value_c);

void calculate_normal();

};


and the implementation:

c_vector3d *c_triangle::Get_a(){return a;}
c_vector3d *c_triangle::Get_b(){return b;}
c_vector3d *c_triangle::Get_c(){return c;}

c_vector3d c_triangle::Get_normal(){return normal;}

// These are the Set'ers
/*not to much to say here. a, b, c are privates,
so you need functions to set them*/

void c_triangle::Set_a(c_vector3d *value_a){a = value_a;}
void c_triangle::Set_b(c_vector3d *value_b){b = value_b;}
void c_triangle::Set_c(c_vector3d *value_c){c = value_c;}


// And here is the constructor: he takes 3 points and make a triangle
c_triangle::c_triangle(c_vector3d &value_a, c_vector3d &value_b, c_vector3d &value_c)
{
a = &value_a;
b = &value_b;
c = &value_c;

calculate_normal();
}

void c_triangle::calculate_normal()
{
normal = Normalize(Cross_product(*b - *a, *c - *a)); //is this right?
}



I made a couple of changes in it since I last posted. Mainly, the a, b, c c_vector3d are now public... I was going to do that to remove the Gets and Sets, but now that Paulius told me what was wrong, they are working (by the way, thanks Paulius!). The second error is also working now, I changed a * to a & and it worked...

In retrospect, I should have waited some more time to post this, or maybe posted in on the begginers forum (well, I didn't because sometimes I just feel that the begginers forum has way to much complex stuff). The thing was that I was kind in a rush (I was allredy late... because I kept trying to make the program work), so I decided to just post it anyway. But hey, it is partialy working now, except... hmm... hold on, it just worked! I mean, I get a run-time error now that it worked, but at least it is...

Share this post


Link to post
Share on other sites
(0) Remove the gets and sets. They are doing nothing for you, and all my experience suggests they will continue to do nothing. Member functions should Do Something(TM).

(1) To create a pointer from a reference, you would indeed use '&' rather than '*'. Stare at the number of '*'s on each variable: '*'ing (dereferencing) decreases the level-of-pointing of the type, and '&'ing (referencing) increases the count. Whether the value is a reference makes no difference in that counting.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
(0) Remove the gets and sets. They are doing nothing for you, and all my experience suggests they will continue to do nothing. Member functions should Do Something(TM).

(1) To create a pointer from a reference, you would indeed use '&' rather than '*'. Stare at the number of '*'s on each variable: '*'ing (dereferencing) decreases the level-of-pointing of the type, and '&'ing (referencing) increases the count. Whether the value is a reference makes no difference in that counting.


(0)Yep. Just done that. I actually had done it earlier trying to make the code compile, but re-added it when asked for the full source (I mean, since they were getting an error I was asking about, it would be nice to show the implementation that actually had them)

(1)The pointer thing really confuses me actually. Sometimes, I just cought myself randomly changing * to & until it compiles... but I guess it's just a matter of geting used to how they work.

EDIT- I didn't realised that I still hasn't told people that it is working now! Well, it's working, and the run-time error I was getting was because, when I checked that the vertices existed before I created the triangle, I placed a < instead of a > (the code was if(int a < std::vector.size()) return false;), so everytime I tried to add a vertex that did existed, the triangle would fail to be created. Then, by accessing it with std::vector[int n], the program would make an illegal operation and windows would shut him down. After I replaced std::vector[int n] with std::vector.at(n), windows shuted down my program nicely, without the illegal operation message... then I fixed the error I commented before and... there's a cube spinning in my screen!!

Share this post


Link to post
Share on other sites

This topic is 4165 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this