Reputation: 51
I'm trying to get hands on C++. Sorry if the post already exists, that's could be because I don't have the necessary C++ background to put the exact words on it.
I'm creating a class to handle math vectors. So I have a Point
struct, and a Vector
struct. I want to be able to create an instance of a Vector
by giving two instances of Point
to a method.
I posted the code in this gist : https://gist.github.com/benoitjupille/9bd759042e201d0e8b3c76b708467fb5
This code is not compiling, I got this :
In file included from /home/bjupille/code/games/Math/Math.ino:2:0:
/tmp/arduino_build_457310/sketch/Point.h: In member function 'Benov::Vector2D Benov::Point::between(Benov::Point)':
Point.h:15:60: error: no matching function for call to 'Benov::Vector2D::between(Benov::Point*, Benov::Point&)'
Vector2D vector = Vector2D::between(this, point);
^
In file included from /tmp/arduino_build_457310/sketch/Point.h:4:0,
from /home/bjupille/code/games/Math/Math.ino:2:
/tmp/arduino_build_457310/sketch/Vector2D.h:22:25: note: candidate: static Benov::Vector2D Benov::Vector2D::between(Point, Point)
static Vector2D between(Point a, Point b)
^~~~~~~
/tmp/arduino_build_457310/sketch/Vector2D.h:22:25: note: no known conversion for argument 1 from 'Benov::Point*' to 'Point'
Could you please give me some leads on what is happening here ?
Upvotes: 1
Views: 504
Reputation: 13557
between
is defined as this:
static Vector2D between(Point a, Point b){...}
You are calling it like this from Point
implementation:
Vector2D vector = Vector2D::between(this, point);
this
is a pointer to a Point
in it's current context -- between
doesn't take a pointer to a Point
, it takes a Point
. So you need to dereference the pointer:
Vector2D vector = Vector2D::between(*this, point);
Once you do this and compile, you will get one more error. Add this to your Vector2D.h
struct Point; // <-- add this right before the struct definition
struct Vector2D
{
float x;
float y;
//....
}
The reason you have to do that is because Vector2D
doesn't know what a Point
is because of compilation order (there is a circular reference, Point
needs Vector2D
and Vector2D
needs Point
). We are going to put that there to let the compiler know that... yes... we know what it will be eventually. (That's probably a bad explanation, but you may get the jist.)
Upvotes: 4