Public Group

# Linker Errors(was Problem with for Loop )

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

## 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 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 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 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 on other sites
Quote:
 Original post by NiddlesThanks for your replies! Yes, assignment is what I meant. Now, the compiler is complaining about "name lookup of v changed for new ISO 'for' scopingusing 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 on other sites
Yuck. My eyes went right over that, I should probably go to bed ;-). Thanks though!

##### Share on other sites
Now It's throwing 2 linker errors.
Undefined Reference to CVertex::CVertex() twice.
Why would it do this?

##### Share on other sites
Quote:
 Original post by AgonyYou 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 on other sites
Quote:
 Original post by NiddlesNow 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.

1. 1
Rutin
26
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 10
• 9
• 9
• 9
• 46
• ### Forum Statistics

• Total Topics
631751
• Total Posts
3002077
×