Sign in to follow this  

Linker Errors(was Problem with for Loop )

This topic is 4056 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

Hey everyone. I have a vertex class, and I think I need to overload the equal operator so I can say two vertices are the same. Is this the correct way of doing so?
class Vertex
{
    public:
        Vertex(float itex_x, float itex_y, float ix, float iy, float iz) {tex_x = itex_x; tex_y = itex_y;
                                                  x = ix; y = iy; z = iz;}
        float tex_x;
        float tex_y
        float x;
        float y;
        float z;
        Vertex operator=(Vertex v) {return Vertex(v.tex_x, v.tex_y, v.x, v.y, v.z);}
};




Thanks, Adam [/source] [Edited by - Niddles on November 7, 2006 9:34:51 PM]

Share this post


Link to post
Share on other sites
Wouldn't you want to overload operator== if you just want to test for equality? operator= is the assignment operator. It should look something like

bool operator==(const Vertex& v) const
{
//return whether x, y, z are equal (too lazy to type it all out) :P
//oh, and don't make the mistake of using == to check equality
//with floats
}

Share this post


Link to post
Share on other sites
You mean you want to assign one vector to another? If so, the it is technically the "assignment operator", and it should look something like this:

Vertex& operator = (const Vertex& vertex)
{
x = vertex.x;
y = vertex.y;
z = vertex.z;
tex_x = vertex.tex_x;
tex_y = vertex.tex_y;
return *this;
}

The parameter is the object on the right side of the assignment operator, and the vector represented by *this is the object on the left side of the operator, receiving the values. The right side is constant, since it doesn't change. The return is a reference to the object that got assigned to, so you can do assignment chaining:
Vertex A;
Vertex B;
Vertex C;

A = B = C = Vertex(0, 0, 0, 0, 0);

Share this post


Link to post
Share on other sites
Thanks for your replies! Yes, assignment is what I meant. Now, the compiler is complaining about "name lookup of v changed for new ISO 'for' scoping
using obsolete binding at v.
Here is the code that it doesn't like.

for(int temp=0; temp<MySection.numberOfTriangles; temp++);
{
for(int v=0; v<3; v++);
{
sscanf(line, "%f %f %f %f %f\n", &TempVert.x, &TempVert.y, &TempVert.z,
&TempVert.tex_x, &TempVert.tex_y);
TempTri.TriangleVertices[v] = TempVert;
}
MySection.WorldTriangles.push_back(TempTri);
}


Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by Niddles
Thanks for your replies! Yes, assignment is what I meant. Now, the compiler is complaining about "name lookup of v changed for new ISO 'for' scoping
using obsolete binding at v.
Here is the code that it doesn't like.
*** Source Snippet Removed ***
Thanks.


for(int temp=0; temp<MySection.numberOfTriangles; temp++); //<- I'm guessing this semicolon isn't supposed to be here
{
for(int v=0; v<3; v++); //<- and here's another one! semicolons don't belong here unless you want a for loop that does nothing
{
sscanf(line, "%f %f %f %f %f\n", &TempVert.x, &TempVert.y, &TempVert.z,
&TempVert.tex_x, &TempVert.tex_y);
TempTri.TriangleVertices[v] = TempVert;
}
MySection.WorldTriangles.push_back(TempTri);
}


Share this post


Link to post
Share on other sites
Quote:
Original post by Agony
You mean you want to assign one vector to another? If so, the it is technically the "assignment operator", and it should look something like this:

Vertex& operator = (const Vertex& vertex)
{
x = vertex.x;
y = vertex.y;
z = vertex.z;
tex_x = vertex.tex_x;
tex_y = vertex.tex_y;
return *this;
}


Not needed... The compiler will insert one for you... Isn't it nice :-)

Share this post


Link to post
Share on other sites
Quote:
Original post by Niddles
Now It's throwing 2 linker errors.
Undefined Reference to CVertex::CVertex() twice.
Why would it do this?


Because you don't have a default constructor for CVertex, but the compiler sees places where you've asked it to make a CVertex without calling the other constructor.

Share this post


Link to post
Share on other sites

This topic is 4056 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