no appropriate default constructor available

when im coding, i compile the app from time to time to check for errors rather then spend hours coding then find out the whole thing is trash.
because of that i found this early on and am confused..

my 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
class Card
{
public: 
	Card(const int cardtype);
	Card(const int cardtype, const int face);
	~Card() {}
	const int GetValue() { return value; }

	bool operator == (const Card& itemToCompare) const
    {
        return (itemToCompare.value == this->value);
    }

    bool operator < (const Card& itemToCompare) const
    {
        return (this->value < itemToCompare.value);
    }
private:
	int value;
};
Card::Card(const int cardtype)
{
	switch (cardtype)
case 1:
	break;
}



causes the compiler to spit out the "no appropriate default constructor available" error... wtf?

im using the class in a std::list..

full source..


cardtypse.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
enum CARDTYPE { NUMBER, DRAWTWO, REVERSE, SKIP, WILD, WILDDRAWFOUR };

class Card
{
public: 
	Card(const int cardtype);
	Card(const int cardtype, const int face);
	~Card() {}
	const int GetValue() { return value; }

	bool operator == (const Card& itemToCompare) const
    {
        return (itemToCompare.value == this->value);
    }

    bool operator < (const Card& itemToCompare) const
    {
        return (this->value < itemToCompare.value);
    }
private:
	int value;
};
Card::Card(const int cardtype)
{
	switch (cardtype)
case 1:
	break;
}

Card::Card(const int cardtype, const int face){}

//class NumberCard
//{
//public:
//	NumberCard(int face) { value = face; }
//	~NumberCard();
//private:
//	int value;
//};
//
//class DrawTwo
//{
//public:
//	DrawTwo();
//	~DrawTwo();
//private:
//	static const int value = 20;
//};
//
//class Reverse
//{
//public:
//private:
//	static const int value = 20;
//};
//
//class Skip
//{
//public:
//private:
//	static const int value = 20;
//};
//
//class Wild
//{
//public:
//private:
//	static const int value = 50;
//};
//
//class WildDrawFour
//{
//public:
//private:
//	static const int value = 50;
//}; 



main.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "SDL.h"
#include <string>
#include <list>
#include "cardtypes.h"

#pragma comment (lib, "SDL.lib")
#pragma comment (lib, "SDLmain.lib")

const int SCREEN_WIDTH = 480;
const int SCREEN_HEIGHT = 272;
const int SCREEN_BPP = 32;

SDL_Surface * Load_Image( std::string filename )
{
	SDL_Surface *loadedimage = NULL;
	SDL_Surface *optimizedimage = NULL;

	loadedimage = SDL_LoadBMP( filename.c_str() );

	if( loadedimage != NULL )
	{
		optimizedimage = SDL_DisplayFormat( loadedimage );
		
		SDL_FreeSurface( loadedimage );
	}

	return optimizedimage;
}

void Apply_Surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
	SDL_Rect offset;

	offset.x = x;
	offset.y = y;

	SDL_BlitSurface( source, NULL, destination, &offset );
}
/*********************************/
//	108 cards total
//	19 Blue
//	19 Green
//	19 Red
//	19 Yellow
//	8 Draw two cards
//	8 Reverse cards
//	8 Skip cards
//	4 wild cards
//	4 Wild draw four cards
/*********************************/

int main( int argc, char* args[] )
{
	std::list <Card> deck(108); 
	//std::vector <int>
	if( SDL_Init( SDL_INIT_EVERYTHING ) )
		return 1;

	SDL_Surface *screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
	SDL_Surface *card = Load_Image( "card.bmp" );

	if( screen == NULL )
		return -1;

	SDL_WM_SetCaption( "UNO", NULL );

	Apply_Surface( ((SCREEN_WIDTH / 2)-(50/2)), ((SCREEN_HEIGHT / 2)-(60/2)), card, screen );

	if( SDL_Flip( screen ) == -1 )
		return 1;

	SDL_Delay( 5000 );

	SDL_FreeSurface( card );

	SDL_Quit();

	return 0;
}



something to note, it compiles fine if i comment out my constructors...
so... i guess i screwed up something there.. (obviously)

please help. :) and thanks.


EDIT:

full error is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1
1>Compiling...
1>main.cpp
1>c:\program files\microsoft visual studio 9.0\vc\include\list(447) : error C2512: 'Card::Card' : no appropriate default constructor available
1>        c:\program files\microsoft visual studio 9.0\vc\include\list(444) : while compiling class template member function 'std::list<_Ty>::list(unsigned int)'
1>        with
1>        [
1>            _Ty=Card
1>        ]
1>        d:\my documents\visual studio 2008\projects\my sdl games\pc_uno\main.cpp(54) : see reference to class template instantiation 'std::list<_Ty>' being compiled
1>        with
1>        [
1>            _Ty=Card
1>        ]
1>c:\program files\microsoft visual studio 9.0\vc\include\list(447) : error C2512: 'Card' : no appropriate default constructor available
1>Build log was saved at "file://d:\My Documents\Visual Studio 2008\Projects\My SDL games\PC_UNO\Debug\BuildLog.htm"
1>PC_UNO - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Last edited on
If you define a classs with a constructor that takes parameters, then you the programmer
must also provide a default constructor that takes no parameters because the compiler will
not automatically do it for you.

So when you create Card List of 108 cards like this
 
std::list <Card> deck(108); 

It is the default constructor that will be used to create them and this defualt constructor is missing and is the cause of the error.
argh... duh.. thanks can't believe i forgot something so simple.. thanks. lol
Topic archived. No new replies allowed.