error C2512: 'Image' : no appropriate default constructor available

Hello friends,

I'm new at C++ and i've been trying to solve this problem for hours but i can't find any fix.

This is my code:

For Image.h

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

class Image : public Media {

	/*Variaveis*/
	private:
		//Posicao da imagem presente na original
		int* initPosition;
		//Tamanho da imagem presente na original 
		int* initSize;
		//Posicao da imagem presente na janela
		int* destPosition;
		//Tamanho da imagem presente na janela
		int* destSize;


	/*Metodos*/
	public:
		//Construtor
		Image(string path, int* initPosition, int* initSize, int* destPosition, int* destSize);
		//Obter posicao da imagem presente na original
		int* getInitPosition();
		//Obter tamanho da imagem presente na original
		int* getInitSize();
		//Obter posicao da imagem presente na janela
		int* getDestPosition();
		//Obter tamanho da imagem presente na janela
		int* getDestSize();
};


For Media.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

class Media {

	/*Variaveis*/
	private:
		//Caminho para encontrar o ficheiro de multimedia
		string path;


	/*Metodos*/
	public:
		//Construtor
		Media(string path);
		//Obter path do GUI
		string getPath();
		
};

#endif 


And the error is in this class:

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
33
34

class HUD {

	/*Variaveis*/
	private:
		//Conjunto de botoes existentes no jogo
		//vector<Button> buttons;
		//Grelha existente no jogo
		//Grid grid;
		//Imagem utilizada como fundo
		Image backgroundImg;


	/*Metodos*/
	public:
		//Construtor
		HUD();
		//Obter botoes
		//vector<Button> getButtons();
		//Obter grelha
		//Grid getGrid();
		//Obter imagem de fundo
		Image getBackground();
	private:
		//Criar imagem de fundo
		Image createBackground();
		//Criar botoes de jogo
		//vector<Button> createButtons();
		//Criar grelha
		//Grid createGrid();
};

#endif


I don't uderstand why do I need a default constructor for Image.
I would be appreciated if you could help me.

Thanks,
Pedro Gomes
Last edited on
You probably need the default, no argument constructor because you're using an instance of Image in your HUD class. But without seeing the HUD constructor this is only a guess. Remember whenever you define a constructor for a class the compiler will no longer supply the default no argument constructor.

Image backgroundImg; Is created using constructor... which? Unless you initialise it in membr initialisation list, it created using default constructor.
My HUD.cpp is something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
HUD::HUD() {
	this->backgroundImg = createBackground();
}

Image HUD::createBackground() {

	string path = "Background/Background.jpg";
	int initPos[2] = {0,0};
	int destPos[2] = {0,0};
	int initSize[2] = {1024, 640};
	int destSize[2] = {1024, 640};

	Image image(path, initPos, initSize, destPos, destSize);

	return image;
}


How can i say the compiler to not use the default constructor, but the one i've made in the Image.h??
You need to use an initialization list instead of initializing it in the constructor body and you need to pass the correct number of variables to match one of the constructors you have defined. Since you have pointers in your class you need to also define the destructor and the copy constructor and assignment operator=. Why the pointers by the way?
My HUD.cpp is something like this:

What we really need to see is the complete Image class definition and implementation.

The pointers is to act like an array (but im guessing thats not the right way to do it). Maybe i should use vector instead?

My Image.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

#include "Image.h"

Image::Image(string path, int* initPosition, int* initSize, int* destPosition, int* destSize) : Media(path) {
	this->initPosition = initPosition;
	this->initSize = initSize;
	this->destPosition = destPosition;
	this->destSize = destSize;
}

int* Image::getInitPosition() {
	return initPosition;
}

int* Image::getInitSize() {
	return initSize;
}

int* Image::getDestPosition() {
	return destPosition;
}

int* Image::getDestSize() {
	return destSize;
}


I'm gonna be honest with you, i'm more like a java/c# programmer, so this is getting me a bit confused. What is an initialization list? I check some examples over the Internet, and i thought this was my best bet. Sorry.
All of the variables should be initialized in the initialization list not just the Media() variable.
1
2
3
4
	this->initPosition = initPosition;
	this->initSize = initSize;
	this->destPosition = destPosition;
	this->destSize = destSize;


Also using assignment for those variables is probably not a good idea, it is possible that the original variables could have a shorter lifespan than this class. If those variables go out of scope before the class the class variables will be referencing memory that is no longer valid.

IMO returning pointers from classes should also be avoided, except in certain circumstances, unless those return values are const qualified.
At the moment my HUD.h:
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
33
34
35
36
37
38
#ifndef HUD_H
#define HUD_H

#include "Image.h"

class HUD {

	/*Variaveis*/
	private:
		//Conjunto de botoes existentes no jogo
		//vector<Button> buttons;
		//Grelha existente no jogo
		//Grid grid;
		//Imagem utilizada como fundo
		Image* backgroundImg;


	/*Metodos*/
	public:
		//Construtor
		HUD();
		//Obter botoes
		//vector<Button> getButtons();
		//Obter grelha
		//Grid getGrid();
		//Obter imagem de fundo
		Image getBackground();
	private:
		//Criar imagem de fundo
		Image createBackground();
		//Criar botoes de jogo
		//vector<Button> createButtons();
		//Criar grelha
		//Grid createGrid();
};

#endif


My Image.h

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
33
34
35
36
37
38
39
40
41

#ifndef Image_H
#define Image_H

#include "Media.h"

class Image : public Media {

	/*Variaveis*/
	private:
		//Posicao da imagem presente na original
		int* initPosition;
		//Tamanho da imagem presente na original 
		int* initSize;
		//Posicao da imagem presente na janela
		int* destPosition;
		//Tamanho da imagem presente na janela
		int* destSize;


	/*Metodos*/
	public:
		//Construtor
		Image(string path, int* initPosition, int* initSize, int* destPosition, int* destSize) : Media(path) {
			this->initPosition = initPosition;
			this->initSize = initSize;
			this->destPosition = destPosition;
			this->destSize = destSize;
		};
		//Obter posicao da imagem presente na original
		int* getInitPosition();
		//Obter tamanho da imagem presente na original
		int* getInitSize();
		//Obter posicao da imagem presente na janela
		int* getDestPosition();
		//Obter tamanho da imagem presente na janela
		int* getDestSize();
};

#endif


jlb, thanks for your cooperation, and i don't want to sound like a jerk, but I can't understand what I am doing wrong and what i suppose to do to make it right. So could you give me some examples or some links to help me understand?
Are you still talking about the constructor problem? Or are you talking about the returning of the pointer problems?

If it's still the constructor, I recommend just creating a default no argument constructor that initializes the variables to a known state.

Yes it was about the construtor, for the pointers i used vector (some people say its better than array).

Well it worked jlb, thanks for your time and patience.

Pedro Gomes
Topic archived. No new replies allowed.