About a year ago, I built a simple ray tracer in Java, and now I'm trying to port it to c++, but I can't figure out how to get a couple of my classes to work together. It should be fairly strait forward, but its not working. Here is a very simple example.

// main.cpp
#include <iostream>
#include "Vector.h"
#include "Camera.h"

using namespace std;

int main() {
	Vector X (1, 0, 0);
	return 0;
}

The main.cpp file just imports the header files and declares a new Vector. No big problem there.

// Vector.h
#ifndef _VECTOR_H 
#define _VECTOR_H

class Vector {
	float x, y, z;
	
	public:
	
	// consructor function
	Vector (float,float,float);

	// method functions
	float getVectorX() { return x; }
	float getVectorY() { return y; }
	float getVectorZ() { return z; }
};

Vector::Vector (float a, float b, float c) {
	x = a;
	y = b;
	z = c;
}

#endif

The Vector.h file declares a super simple Vector class. So far so good.

// Camera.h
#ifndef _CAMERA_H 
#define _CAMERA_H

#include "Vector.h"

class Camera {
      Vector campos, camdir, camright, camdown;
      
      public:

      // consructor function
      Camera (Vector,Vector,Vector,Vector);
};

Camera::Camera (Vector pos, Vector dir, Vector right, Vector down) {
	campos = pos;
	camdir = dir;
	camright = right;
	camdown = down;
}

#endif

The Camera.h file creates the Camera class, which as you can see, REQUIRES the Vector class. The four input arguments of the Camera class are all Vectors. But no matter what I do, c++ doesn't want to let me use the Vector class in my Camera class. In Java all this is solved with packages, but I don't know enough about c++ to figure it out on my own.

The compiler is telling me:
In constructor 'Camera::Camera(Vector, Vector, Vector, Vector):
error: no matching function for call to 'Vector::Vector()'
note: candidates are: Vector::Vector(const Vector&)
note: Vector::Vector(float, float, float)

Any help would be appreciated.

Note that C++ doesn't use references for objects by default, so Vector campos; in C++ is equivalent to Vector campos = new Vector(); in Java. With that in mind, it should be obvious that your Vector class doesn't have a default constructor even though you try to call one.

Note that C++ doesn't use references for objects by default, so Vector campos; in C++ is equivalent to Vector campos = new Vector(); in Java. With that in mind, it should be obvious that your Vector class doesn't have a default constructor even though you try to call one.

Thanks. So basically all my c++ classes need to have a default constructor like ...

#ifndef _VECTOR_H 
#define _VECTOR_H

class Vector {
	float x, y, z;
	
	public:
	
	// default constructor, no arguments
	Vector ();
	
	// consructor function
	Vector (float,float,float);

	// method functions
	float getVectorX() { return x; }
	float getVectorY() { return y; }
	float getVectorZ() { return z; }
};

Vector::Vector () {
	x = 0;
	y = 0;
	z = 0;
}

Vector::Vector (float a, float b, float c) {
	x = a;
	y = b;
	z = c;
}

#endif

You should get into the habit of initializing data member like so

Vector::Vector ()
:x = 0,y = 0,z = 0 
{}

Vector::Vector (float a, float b, float c) 
:x = a,y = b,z = c
{}

In some situations its more efficient.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.