No appropriate default constructor available error

I am trying to make a lunar lander type game in opengl/c++ but i keep getting the "No appropriate default constructor available error, and i cant figure out why.

So the problem is coming from the background i am trying to create,

Main.cpp where i am getting the error, which is for this line :Background background; I have done this so i can use it in the display function, the same way in which i done it for ground and the lander, but this time it is causing the problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
float startx = rnd(100, 500); float starty = rnd(350, 450); int startradius = 15; // Construction of lander 
Lander lander (startx, starty, startradius);
Ground ground;
Background background;
Ground *TheGround = NULL;

///////////////////////////////////////////////////////////// Background 
int width, height;
const int NStars = 200;
Background *stars[NStars];

void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	lander.show();
	ground.show();
	background.Show();
	glutSwapBuffers();
}


Background.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "Background.h"
#include<gl/glut.h>
#include <cmath>
#include "Random.h"

Background::Background(float ix, float iy, float isize)
{
	x = ix; y = iy;
	size = isize;


};

void Background::Show()
{
	
	glBegin(GL_POLYGON);
	glVertex2f(x, y);
	glVertex2f(x + size, y);
	glVertex2f(x + size, y + size);
	glVertex2f(x, y + size);
	glEnd();
};

void Background::update() {
	x += dx;
	if ((x + size)>500 || x < 0)
		dx = -dx;
	y += dy;
	if ((y + size)>500 || y < 0)
		dy = -dy;
}


background.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef Background_H
#define Background_H

class Background
{
	public:
		Background(float ix, float iy, float isize);
		void Show();
		void update();
	private:
		float x, y, size;
		float dx, dy;
		
};
#endif 





Edit

could it have something to do with this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void initObjects()
{
	for (int i = 0; i<NStars; i++) {
		int isize = rnd(3);
		int ix = rnd(width - isize);
		int iy = rnd(height - isize);
		float col[3];
		// Assign random colour components...
		col[0] = rnd(0.0, 1.0);
		col[1] = rnd(0.0, 1.0);
		col[2] = rnd(0.0, 1.0);
		stars[i] = new Background(ix, iy, isize);
	}
}


because of the new Background line?
Last edited on
Background background; <- here, you try to create a Background object using a constructor that takes no arguments (known as a default constructor,) but nowhere in your code do you provide such a constructor. Background has a single constructor that takes exactly three arguments.
Last edited on
so would i change the Background ground; to Background (parameters) background; or create another constructor in the background.cpp which i didnt think you could do?
You could do either of those. The second would require a corresponding change in the class definition. The first would make more sense to me.
Topic archived. No new replies allowed.