473,325 Members | 2,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

g++ throws: error: no matching function for call to `block::block()'

I'm making a very simple game in SDL, and I'm not asking for SDL help I
hope - this looks like something C++ related, so I'll ask here.

I have a class for a simple block, or tile, in the game, which can be
either on or off. I'm having trouble with the constructor though.
>>
class block
{
private:
SDL_Surface *screen;
SDL_Surface *surface;
Uint32 selcol;
Uint32 offcol;
bool on;

public:
block(SDL_Surface *);
bool blit(int, int);
void enabled(bool);
} ;

/* ... */

block::block(SDL_Surface *mscreen)
{
screen = mscreen;
surface = SDL_CreateRGBSurface(screentype, blockw, blockh, bpp,
screen->format->Rmask, screen->format->Gmask,
screen->format->Bmask,
screen->format->Amask);

selcol = SDL_MapRGB(screen->format, selcolr, selcolg, selcolb);
offcol = SDL_MapRGB(screen->format, offcolr, offcolg, offcolb);

/* default is off */
on = false;
SDL_FillRect(surface, NULL, offcol);
}
>>
I'm calling it from my `game' class:
>>
class game
{
private:
SDL_Surface *screen;
Uint32 bgcol;
block grid[w][h];
bool blitblock(int, int);

public:
game();

} ;
>>
I don't think details matter too much, but with my game constructor,
which follows:
>>
game::game()
{
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("couldn't initialise SDL: %s\n", SDL_GetError());
exit(1);
}

screen = SDL_SetVideoMode(screenw, screenh, bpp, screentype);

for(int i = 0; i < w; i++)
for(int j = 0; j < h; j++)
grid[i][j] = block(screen);
}
>>
I get the following error from g++:
>>
g++ ./block.cpp ./game.cpp -c -lSDL -Wall
../game.cpp: In constructor `game::game()':
../game.cpp:4: error: no matching function for call to `block::block()'
../lgt.h:36: note: candidates are: block::block(const block&)
../lgt.h:45: note: block::block(SDL_Surface*)
>>
I hope this is enough information - it's probably something really
stupid, too. The only thing I can think of is that I can't call the
constructor like I am for my block array.

Thanks,
Alvin

Dec 1 '06 #1
8 3296
Alvin wrote:
[..]
I get the following error from g++:
>>>
g++ ./block.cpp ./game.cpp -c -lSDL -Wall
./game.cpp: In constructor `game::game()':
./game.cpp:4: error: no matching function for call to `block::block()'
./lgt.h:36: note: candidates are: block::block(const block&)
./lgt.h:45: note: block::block(SDL_Surface*)
>>>

I hope this is enough information - it's probably something really
stupid, too. The only thing I can think of is that I can't call the
constructor like I am for my block array.
That's right. You have an array which has to be initialised. The
compiler has to use the default c-tor for 'block' to intialise the
array elements. But there is no defatul c-tor in 'block'. Add one.

The fact that you assign different values to 'grid' in the body of
the 'game::game' does not matter, the array has to be initialised
before entering the body.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 1 '06 #2

Victor Bazarov wrote:
Alvin wrote:
[..]
I get the following error from g++:
>>
g++ ./block.cpp ./game.cpp -c -lSDL -Wall
./game.cpp: In constructor `game::game()':
./game.cpp:4: error: no matching function for call to `block::block()'
./lgt.h:36: note: candidates are: block::block(const block&)
./lgt.h:45: note: block::block(SDL_Surface*)
>>
I hope this is enough information - it's probably something really
stupid, too. The only thing I can think of is that I can't call the
constructor like I am for my block array.

That's right. You have an array which has to be initialised. The
compiler has to use the default c-tor for 'block' to intialise the
array elements. But there is no defatul c-tor in 'block'. Add one.

The fact that you assign different values to 'grid' in the body of
the 'game::game' does not matter, the array has to be initialised
before entering the body.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thanks for the quick reply! How would I go about initialising the array
before it enters my game constructor, though?

Thanks,
Alvin

Dec 1 '06 #3
On 1 Dec 2006 11:16:53 -0800 in comp.lang.c++, "Alvin"
<al***********@gmail.comwrote,
>How would I go about initialising the array
before it enters my game constructor, though?
Exactly as Victor says, you must supply a default constructor for block
- one that can be called with no arguments.

Dec 1 '06 #4
"David Harmon" <so****@netcom.comwrote in message
news:46****************@news.west.earthlink.net...
On 1 Dec 2006 11:16:53 -0800 in comp.lang.c++, "Alvin"
<al***********@gmail.comwrote,
>>How would I go about initialising the array
before it enters my game constructor, though?

Exactly as Victor says, you must supply a default constructor for block
- one that can be called with no arguments.
In which case, since things need to be initialized to be used for your
class, you probably want to add an init() method which needs be called
before the class can be used. One possible thing to do is add a private
bool initialized_; and set it to false in the default constructor. Set it
to true in init and have calls check to see if it's initialized. Or set
your screen to NULL in the default constructor and test it, since you'll
initialize it in your init() method.
Dec 2 '06 #5
On Fri, 1 Dec 2006 22:37:47 -0800 in comp.lang.c++, "Jim Langston"
<ta*******@rocketmail.comwrote,
>In which case, since things need to be initialized to be used for your
class, you probably want to add an init() method which needs be called
before the class can be used.
Alvin is currently setting up the array by copying with:

:: screen = SDL_SetVideoMode(screenw, screenh, bpp, screentype);
::
:: for(int i = 0; i < w; i++)
:: for(int j = 0; j < h; j++)
:: grid[i][j] = block(screen);

which is just as good as an init() method, in my opinion. Do you see an
advantage to init()? It could probably be micro-optimized a little
with:

screen = SDL_SetVideoMode(screenw, screenh, bpp, screentype);
block initblock(screen);
for(int i = 0; i < w; i++)
for(int j = 0; j < h; j++)
grid[i][j] = initblock;

Dec 2 '06 #6

Alvin wrote in message
<11**********************@j44g2000cwa.googlegroups .com>...
>I'm making a very simple game in SDL, and I'm not asking for SDL help I
hope - this looks like something C++ related, so I'll ask here.
I have a class for a simple block, or tile, in the game, which can be
either on or off. I'm having trouble with the constructor though.
>>>
class block{
public:
block(SDL_Surface *);
} ;
>>>

I'm calling it from my `game' class:
>>>
class game{
private:
SDL_Surface *screen;
Uint32 bgcol;
block grid[w][h];
Suggestion:

std::vector<std::vector<block grid; // note space between ''
bool blitblock(int, int);
public:
game();
} ;
>>>

I don't think details matter too much, but with my game constructor,
which follows:
>>>
// >game::game()

game::game() : grid( w, h ) // note the colon
>{
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("couldn't initialise SDL: %s\n", SDL_GetError());
exit(1);
}

screen = SDL_SetVideoMode(screenw, screenh, bpp, screentype);

for(int i = 0; i < w; i++)
for(int j = 0; j < h; j++)
grid[i][j] = block(screen);
That will still work, or for better 'range safety':

for( size_t i( 0 ); i < grid.size(); ++i)
for( size_t j( 0 ); j < grid.at( i ).size(); ++j)
grid.at( i ).at( j ) = block( screen );
>}
>>>
If you could somehow pass a 'screen' to your 'game' constructor, you could do
the whole initialization in the init list.

game::game( block screen ) : grid( w, std::vector<block>(h, block(screen))){}

--
Bob R
POVrookie
Dec 2 '06 #7
BobR wrote:
Alvin wrote:
block grid[w][h];
Note, 'w' and 'h' look like abbreviations for 'width' and 'height', but
this array has 'w' rows and 'h' columns.
>
Suggestion:

std::vector<std::vector<block grid; // note space between ''
game::game() : grid( w, h ) // note the colon
Of course, the OP code doesn't work unless w and h are constants.
So it does need modification. If you are going to go with a vector
of vectors (I wouldn't personally), then you might as well solve his
other problem at the same time, and stick with:

game::game(): grid(w) { ...

so that 'block' does not need to have a default constructor.

Then you can insert items into the vector that are already
constructed, rather than creating it full of empty ones and
then assigning them other values.
> for(int i = 0; i < w; i++)
for(int j = 0; j < h; j++)
grid[i][j] = block(screen);

That will still work, or for better 'range safety':

for( size_t i( 0 ); i < grid.size(); ++i)
for( size_t j( 0 ); j < grid.at( i ).size(); ++j)
grid.at( i ).at( j ) = block( screen );
If you're iterating from 0 to size, then you will always
be in range, so there's no need to bother with the 'at'.

Further, if you follow my above suggestion, namely
that grid be initialized to have 'w' empty rows, then
this line will initialize it:

std::fill( grid.begin(), grid.end(), std::vector<block>(h, screen)
);

If you could somehow pass a 'screen' to your 'game' constructor, you could do
the whole initialization in the init list.

game::game( block screen ) : grid( w, std::vector<block>(h, block(screen))){}
Agree, but 'screen' is the result of a function that's being called
in the constructor, so that would be a bit awkward (But not impossible).

Dec 3 '06 #8

Old Wolf wrote in message
<11**********************@j72g2000cwa.googlegroups .com>...
>BobR wrote:
>Alvin wrote:
block grid[w][h];

Note, 'w' and 'h' look like abbreviations for 'width' and 'height', but
this array has 'w' rows and 'h' columns.
You know that, and I know that. But if:

vector<vector<block V2d( h, w );

....and OP did:

V2d[w][h] = something;

Oops (assuming w != h).
So, I left it 'w, h'.
>
>>
Suggestion:
std::vector<std::vector<block grid; // note space between ''
game::game() : grid( w, h ) // note the colon
>> for(int i = 0; i < w; i++)
for(int j = 0; j < h; j++)
grid[i][j] = block(screen);

That will still work, or for better 'range safety':

for( size_t i( 0 ); i < grid.size(); ++i)
for( size_t j( 0 ); j < grid.at( i ).size(); ++j)
grid.at( i ).at( j ) = block( screen );

If you're iterating from 0 to size, then you will always
be in range, so there's no need to bother with the 'at'.
You are looking at '.at()', I was looking at:

w vs. grid.size()
h vs. grid.at(i).size()

That's the "better 'range safety' " I was thinking about.
( assume there is no 'insert'/'erase' inside loop).

I left the '.at()'s in there to show an alt. usage.
I not only agree with you, but, usually point that out myself.

--
Bob R
POVrookie
Dec 3 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Nick Jacobson | last post by:
This works fine: x = 1 def execfunc(): print x execfunc() So why doesn't this? s = \
4
by: Jack | last post by:
Hello, <%@ Language=VBScript %> <% Response.Write "<form name=form1>" Response.Write "<SELECT id=select1 name=select1>" Response.Write "<OPTION value=1>One</OPTION>" Response.Write "<OPTION...
4
by: Ross Contino | last post by:
Hello to all: I have been searching the web for examples on how to determine a median value in a mySQL table. I have reviewed the article at...
9
by: Chris Saunders | last post by:
I'm getting this error report from my compiler (MSVC++ 7): c:\Eiffel55\library\cjs\pdf\spec\include\p_intern.h(258) : error C2143: syntax error : missing ')' before '*' Here's the code: ...
6
by: szabelin | last post by:
Hello, I am trying to call a function inside the javascript block from asp.net during the postback (NOT button's OnClick event handler though). The javascript function creates new popup window. I...
10
by: ElanKathir .S.N | last post by:
Hi all ! VB.NET adds the ability to create variables that are visible only within a block. A block is any section of code that ends with one of the words End , Loop , or Next . This means that...
8
by: Pedro Pinto | last post by:
When compiling my program i got this error: Error: 'for' loop initial declaration used outside c99 mode What is it and how can i solve it? Thanks in advance! Regards
11
by: utab | last post by:
Dear all, in a class definition class X{ private: static map< string , map<string, int word_map; static void initialize(); };
7
by: iu2 | last post by:
Hi, Playing with imitating lambdas and ruby blocks in Python, I came up with a very simple construct, for example: import compiler def dotimes(i, code): for i in range(i): exec code
0
by: Jean-Paul Calderone | last post by:
On Tue, 29 Jul 2008 07:26:38 -0700 (PDT), "jiri.zahradil@gmail.com" <jiri.zahradil@gmail.comwrote: Python doesn't have dynamic scoping. ... for i in range(n): ... callable()...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.