Author Topic: Won't let me add pointers :(  (Read 4544 times)

0 Members and 1 Guest are viewing this topic.

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Won't let me add pointers :(
« on: March 27, 2007 »
It probably sounds like quite a strange thing to want to do but is it possible to do something like this?

Code: [Select]
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.

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Won't let me add pointers :(
« Reply #1 on: March 27, 2007 »
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:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Won't let me add pointers :(
« Reply #2 on: March 27, 2007 »
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'.

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Won't let me add pointers :(
« Reply #3 on: March 27, 2007 »
A quick (not terribly well implemented) example of operator overloading
Code: [Select]
#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:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Won't let me add pointers :(
« Reply #4 on: March 27, 2007 »
Ah right, slight misunderstanding methinks. I'm wanting to actually add and subtract the pointers.

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

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Won't let me add pointers :(
« Reply #5 on: March 27, 2007 »
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:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Won't let me add pointers :(
« Reply #6 on: March 27, 2007 »
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
« Last Edit: March 27, 2007 by Stonemonkey »

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Won't let me add pointers :(
« Reply #7 on: March 27, 2007 »
I see what you're trying to do.

If you have
Code: [Select]
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.

Code: [Select]
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:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Won't let me add pointers :(
« Reply #8 on: March 27, 2007 »
ok, thanks Jim.