0
struct vect{
    float x,y,z;
    vect(float xx, float yy, float zz){
        x=xx;
        y=yy;
        z=zz;
    }
    vect(){
        x=0;
        y=0;
        z=0;
    }
    void normalize(){
        float len = sqrt(x*x + y*y + z*z);
        x = x/len;
        y = y/len;
        z = z/len;
    }
    vect operator-(const vect &other) const{return vect(x-other.x,y-other.y,z-other.z);}
};

bool tri_plane_x(vect *v1, vect *v2, vect *v3, vect *pos, float s){

    //removed unnessecary code

    if (vv1 != vv2){
        vect dir = v2 - v1; // getting an error on this line
        dir.normalize();
        float d = (pos->x - v1->x)/dir.x;
        hy[c] = d * dir.y + v1->y;
        hz[c] = d * dir.z + v1->z;
        c++;
    }

    //removed unnessecary code
}

do some one know why this wont compile? is there a error in how i overloaded the function or is this because i am subtracting two pointers to a vect?

./exporter/main.cpp:74:19: error: conversion from ‘long int’ to non-scalar type ‘vect’ requested

here is the compiler error related to this line. (didn't include the whole log as it is just a the same error repeated many times in the code)

i can't see where the "long int" comes from... is there another way of overloading when using pointers?

lasvig
  • 181
  • 4
  • 13

4 Answers4

0

The error is because you are assigning a difference of pointers to Vect which should object instance of type vect. So they are incompartible. In addition to that I don't think difference of two pointers to non-linear memory makes sense.

hmatar
  • 2,437
  • 2
  • 17
  • 27
0

v2 and v1 are pointers, the difference between two pointers is a number telling you the offset between two addresses, which is a number.

You don't want to "overload using pointers" you want the result of operator- on the objects pointed to, not on the pointers themselves.

You need to say *v2 - *v1 to dereference the pointers and access the objects they point to.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • thank you, that worked. I was thinking on the same thing but i used &v2 - &v1... guess I need to read up a bit more one pointers and references :) – lasvig Jan 19 '13 at 21:17
  • The `&` operator takes the address of an object, so `&v1` tells you the address of `v1` i.e. it's a `vect**`, a pointer to a pointer to a `vect`, which won't help! – Jonathan Wakely Jan 19 '13 at 21:18
0

Instead of

    vect dir = v2 - v1;

you should write

    vect dir = *v2 - *v1;

This is because v2 and v1 are pointers to vectors and applying the operator - on pointers will subtract their addresses (well, to be exact, it subtracts their addresses divided by the size of the type, so you get the number of offsets in memory which is between the two elements; good to know for raw array indexing).

By putting an asterisk in front of the pointer, you are dereferencing it, this means you are now dealing with the value behind the pointer rather than the pointer itself.

When you want to avoid copying of objects or pass objects by reference to functions, a better solution is to use references instead of pointers. This both avoids such situation as well as simplifies the syntax:

bool tri_plane_x(vect &v1, vect &v2, vect &v3, vect &pos, float s) {
    if (vv1 != vv2) {
        vect dir = v2 - v1;
        dir.normalize();
        // ...
    }
}
leemes
  • 44,967
  • 21
  • 135
  • 183
0

Your operator - declaration expects a vect reference but you have passed a vect pointer. References and pointers can produce similar behavior but they are not the same things.

See this post for more clarification on their differences.

Basically you are trying to call the operator - on a pair of pointers to vects, rather than vects themselves. to solve this, you ca use the deferences operator (*) to access the vect objects that the pointers point to:

vect dir = (*v2) - (*v1);

Or by declaring your arguments as references:

bool tri_plane_x(const vect& v1, const vect& v2, const vect& v3, const vect& pos, 
float s){
  ...

Or you can overload you operator- to accept pointers to vects as well:

vect operator-(const vect* other) const
{
     return vect(x-(other->x),y-(other->y),z-(other->z));
}

There may be some unnecessary parentheses here, but I figure that's always better than a counter-intuitive order of operations screwing up your code.

Community
  • 1
  • 1
ApproachingDarknessFish
  • 14,133
  • 7
  • 40
  • 79