• Welcome to Dark Bit Factory & Gravity.
 

Won't let me add pointers :(

Started by Stonemonkey, March 27, 2007

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

Stonemonkey

It probably sounds like quite a strange thing to want to do but is it possible to do something like this?


p0=v0;
p2=v2;
if (v1->y<v0->y) p0=v1;
if (v2->y<p0->y) p0=v2;
if (v1->y>v2->y) p2=v1;
if (v0->y>p2->y) p2=v0;
p1=(v0+v1+v2)-(p0+p2);   //doesn't like this.


where p_ and v_ are pointers.

Cheers.
Fryer.

Jim

You can do that in C++ with operator overloading.  You need to define the + and - operators on the vector class.  In C you just have to write it out long hand.

Jim
Challenge Trophies Won:

Stonemonkey

Ah, thanks Jim......for sending me into advanced topics in c++ where it starts off with 'when you can stand without toppling over,the real work begins'.

Jim

A quick (not terribly well implemented) example of operator overloading

#include <iostream>
using namespace std;

class My3DVector
{
public:
float x,y,z;

My3DVector operator+(My3DVector r)
{
My3DVector w;
w.x = x+r.x;
w.y = y+r.y;
w.z = z+r.z;
return w;
}

My3DVector operator-(My3DVector r)
{
My3DVector w;
w.x = x-r.x;
w.y = y-r.y;
w.z = z-r.z;
return w;
}
};



int main(void)
{
My3DVector a, b, c;

a.x = 1;
a.y = 2;
a.z = 3;

b.x = 10;
b.y = 11;
b.z = 12;

c = a + b;

cout << c.x << "," << c.y << "," << c.z << endl;

return 0;
}

Challenge Trophies Won:

Stonemonkey

Ah right, slight misunderstanding methinks. I'm wanting to actually add and subtract the pointers.

Thanks for that though, that'll be useful too.

Jim

Why would you want to do that?  Can you post a whole thing - with the types of p and q there - and the error message?

Jim
Challenge Trophies Won:

Stonemonkey

#6
They're all vertex pointers, v0,v1,v2 are the 3 verts for a triangle and the code above is to sort them by y for drawing a triangle. The Ifs find the lowest and highest y verts and put them into p0 and p2. The additions and subtractions then find which vert is left and puts that into p1 (supposed to anyway, I did say it would sound strange).

EDIT: y should really be y_2d or something.

error C2110: '+' : cannot add two pointers

Jim

I see what you're trying to do.

If you have

int *a, *b;
int diff;
diff = a-b;

Then diff is meaningful.  It's the number of elements between a and b.  Note that the result is an integer, not a pointer.


int sum;
int *sptr;
sum = a+b;
sptr = a+b;

Neither of those are meaningful in C or C++.  You have to cast to integers to do it, and then you're on your own as to what the result is supposed to mean.

Why not create p0,p1,p2 pointing to the three vectors, and then just swap pair of them based on the y value?

Jim
Challenge Trophies Won:

Stonemonkey