error when compiling with classes

I’m trying to implement a simple vector class, but I’m continually getting an error, and I cannot figure out why (though I increasingly suspect it is a stupid problem).

vector.h looks like this :

  
  
#ifndef PVECTOR_H  
#define PVECTOR_H  
  
#include "ofMain.h"  
  
class PVector {  
  
public:   
	  
	//methods  
	void add(PVector v);  
  
	//constructor  
	PVector(float x, float y);  
	  
	//variables  
	float x;  
	float y;	  
};  
  
#endif  
  

vector.cpp

  
  
#include "vector.h"  
  
PVector::PVector(float _x, float _y)  
{  
	x=_x;  
	y=_y;  
}  
  
void PVector::add(PVector _v){  
	x=x+_v.x;  
	y=y+_v.y;  
}  
  

testApp.h

  
  
#ifndef _TEST_APP  
#define _TEST_APP  
  
#include "ofMain.h"  
#include "vector.h"  
  
class testApp : public ofBaseApp{  
  
	public:  
		void setup();  
		void update();  
		void draw();  
	  
	PVector velocity;  
	PVector position;  
};  
  
#endif  
  

I’ve stripped out any instances in testApp.cpp, so this is essentially all I’ve got. When I compile I get the following error ::

error: no matching function for call to ‘PVector::PVector()’

So, what gives? I thought I had set up the constructor appropriately, but that seems to not be the case.

when you add a constructor with parameters like:

  
PVector(float x, float y);  

the default constructor doesn’t exists anymore so when you create a variable of that type it fails because there’s no constructor to call.

add this to your class:

PVector.h

  
PVector();  
  

PVector.cpp

  
PVector::PVector(){  
x=0;  
y=0;  
}  

also take a look at the vectorMathExample and the ofxVectorMath addon, there you have some vector classes that you can use instead of creating your own

Thanks.

That did solve the problem, though I thought it would be possible to pass arguments to a constructor when the object is instantiated. For now I’ve just created a .set method that works fine.

I am familiar with the vectorMath addon, but sometimes it’s easiest for me to learn by reinventing the wheel.

yes, it’s kind of weird if you come from java/processing but when you declare a variable like:

PVector vec;

that’s actually calling the default constructor so if it doesn’t exists you’re going to get an error.