Error in C ++ user class

I need help finding a problem using a custom C ++ class to manage 3D positions. Here is the relevant code from the class

Punto operator+(Punto p){ return Punto(this->x + px, this->y + py, this->z + pz); } Punto operator+(Punto *p){ return Punto(this->x + p->x, this->y + p->y, this->z + p->z); } Punto operator-(Punto p){ return Punto(this->x - px, this->y - py, this->z - pz); } Punto operator-(Punto *p){ return Punto(this->x - p->x, this->y - p->y, this->z - p->z); } Punto *operator=(Punto p){ this->x = px; this->y = py; this->z = pz; return this; } Punto *operator=(Punto *p){ this->x = p->x; this->y = p->y; this->z = p->z; return this; } 

I use it like this:

 p = fem->elementoFrontera[i]->nodo[0] - fem->elementoFrontera[i]->nodo[1]; 

Where nodo [i] is Punto * and it compiles fine, but when I try to do:

 p = fem->elementoFrontera[i]->nodo[0] + fem->elementoFrontera[i]->nodo[1]; 

The compiler says:

In the void mdTOT::pintarElementosFrontera()': error: invalid operands of types member void mdTOT::pintarElementosFrontera()': error: invalid operands of types Punto *' and Punto*' to binary operator +'

+4
source share
2 answers

The first compiles fine, because you can subtract pointers in C / C ++ but not add pointers. But in any case, it does not do what you need - it does not use your overloaded operator. Since your statements are defined in a class, you need to work with class instances, not pointers. So change something like

 Punto p = *(fem->elementoFrontera[i]->nodo[0]) + *(fem->elementoFrontera[i]->nodo[1]); 

Another thing is that you should use class references, not values, in the operator definition. For instance.

  Punto& operator+(const Punto& p) { 

EDIT. To simplify the code, you can create an access function, for example:

 const Punto& NodoRef(int i, int j) { return *(fem->elementoFronteria[i]->Nodo[j]); } 

and then your code becomes as clean as

 p = NodoRef(i,0) + NodoRef(i,1); 

NodoRef can be defined in your fem class or externally. Just make sure the fem object is alive in the area where you are using NodoRef.

+5
source

The first version works because " - " does the usual pointer arithmetic in this case, it does not use any of your overloaded operators. " + " is not defined in regular pointers, so you get an error message. To use the overloaded version, look for the first pointer:

 p = *fem->elementoFrontera[i]->nodo[0] - fem->elementoFrontera[i]->nodo[1]; 

Separating both pointers should also work, since you have both types of overloads, but you must change your operator definitions to use const references in this case:

 Punto operator+(const Punto &p){ ... } 

This way, objects are not copied every time you use " + ".

Basically you want to do something like this:

 const Punto operator+(Punto *left, Punto *right); // not allowed 

But overloading the free operator+ function, which takes two pointers and appends them accordingly, does not work, because at least one of the parameters must be enum or the type of the class . There is no operator overload for primitive types, and pointers are considered to be such.

0
source

Source: https://habr.com/ru/post/1286324/


All Articles