Help: Classes and arguments for methods

Hi. I ask for help becouse I cant find any good guide for class using in Arduino. I want to make a Library for Vectors(the matematical ones), for using cross product, dot product, etc. I have a few things than I cant make. First of all, how can I reference an attribute of that class in the method definition? Ie: for dot product in Python I do:

    def dot(self,other):
        return self.x*other.x + self.y*other.y + self.z*other.z

How can I do that in Arduino? Thanks a lot!

Use

this->member;

or

(*this).member;

Here is what I think you are trying to achieve:

struct Foo{
  int x;
  int y;
  int z;
  
  int operator *( const Foo &RHS ){
    this->x * RHS.x + this->y * RHS.y + this->z * RHS.z;
  }
};

void setup() {

  Foo a = { 1, 2, 3 };
  Foo b = { 4, 5, 6 };
  
  int result = a * b;
}

void loop() {}

The operator* returns an integer, not a new object so it may be better using a function called dot like you had.

Yes, that was exactly, but how can I make that in my Class definition?

Vector operator +(const Vector &v1) {

  this->_a += v1._a;
  this->_b += v1._b;
  this->_c += v1._c;

}

I make that and the compiler says:

/home/robertito/arduino-1.0.5/libraries/Vector/Vector.cpp: In function ‘Vector operator+(const Vector&)’:
/home/robertito/arduino-1.0.5/libraries/Vector/Vector.cpp:25: error: no matching function for call to ‘Vector::Vector()’
/home/robertito/arduino-1.0.5/libraries/Vector/Vector.cpp:4: note: candidates are: Vector::Vector(float, float, float)
/home/robertito/arduino-1.0.5/libraries/Vector/Vector.h:8: note:                 Vector::Vector(const Vector&)
/home/robertito/arduino-1.0.5/libraries/Vector/Vector.cpp:26: error: invalid use of ‘this’ in non-member function
/home/robertito/arduino-1.0.5/libraries/Vector/Vector.cpp:27: error: invalid use of ‘this’ in non-member function
/home/robertito/arduino-1.0.5/libraries/Vector/Vector.cpp:28: error: invalid use of ‘this’ in non-member function

What can I do? Thanks a lot for the fast reply!

To move a definition out side the class, you need to make sure you define it as part of the class:

struct Foo{
int x;
int y;
int z;
Foo operator +( const Foo &RHS );
};

Foo Foo::operator +( const Foo &RHS ){

}

However your logic is wrong.

notice result is not used, only returned. Also it probably does not have a default ( empty ) constructor, just the one you made to assign values in.

Also you += v1 onto the current vector, rather than adding them together and returning the result.

Here you can see the two different logic patterns, (notice the += returns a reference to the object being modified, whereas the + returns a new object.

struct Foo{
  int x;
  int y;
  int z;
  Foo operator +( const Foo &rhs );
  Foo &operator +=( const Foo &rhs );
};

Foo Foo::operator +( const Foo &rhs ){
  Foo ret = { this->x + rhs.x, this->y + rhs.y, this->z + rhs.z };
  return ret;
}

Foo &Foo::operator +=( const Foo &rhs ){
  this->x += rhs.x;
  this->y += rhs.y;
  this->z += rhs.z;
  return *this;
}

And the two operations are:

  Foo a = { 1, 2, 3 };
  Foo b = { 4, 5, 6 };

  Foo c = a + b;
  
  a += b;

Oh, that's great! Now I understand it!! Thanks a lot!! Works like a charm ]:D

Oh, and one mor question :p I want to access a class method from another class method, like this:

float Vector::norma(){
  return sqrt(sq(_x)+sq(_y)+sq(_z));
  
}


void Vector::normal(){
  float t_norma = Vector::norma();
  _x = _x/t_norma;
  _y = _y/t_norma;
  _z = _z/t_norma;

}

Is this correct? Thanks again!!

only if the function is marked static.

Otherwise like every other member:

this->norma();

Thanks!!! Im about finishing my class!! Thanks for your help, I was about to quit, and now it's pretty funny!!

No worries, sometimes you just need to get over the edge, then its an easy roll down.

I feel like im abusing, but I have a new problem. In this code:

float Vector::norma(){
  return sqrt(sq(this->_x)+sq(this->_y)+sq(this->_z));
  
}

float Vector::angulo_vectores(const Vector &vec) {
    float ang = 0;
    float a = this->norma();
    float b = vec.norma();
    float c = this->escalar(vec);
    if (!(a*b))  ang = acos(c/(a*b));
    return ang*RAD_TO_DEG;
}

dont compile because:

/home/robertito/arduino-1.0.5/libraries/Vector/Vector.cpp: In member function ‘float Vector::angulo_vectores(const Vector&)’:
/home/robertito/arduino-1.0.5/libraries/Vector/Vector.cpp:65: error: passing ‘const Vector’ as ‘this’ argument of ‘float Vector::norma()’ discards qualifiers

How can I solve this? Thanks again, I owe you a lot :p

Change the declaration of norma() to:

float norma() const;

As the parameter to angulo_vectores is const it cannot be changed. norma() accesses its members which the compiler cannot guarantee that they are not modified. so making the whole function const restricts the 'this' pointer to reads only.

You are really helping me here. People like you make this community grow. Thanks a lot!!!

No problem, when I've got a little bit of spare time I try and pitch in.