473,903 Members | 3,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_Surfa ce *);
bool blit(int, int);
void enabled(bool);
} ;

/* ... */

block::block(SD L_Surface *mscreen)
{
screen = mscreen;
surface = SDL_CreateRGBSu rface(screentyp e, blockw, blockh, bpp,
screen->format->Rmask, screen->format->Gmask,
screen->format->Bmask,
screen->format->Amask);

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

/* default is off */
on = false;
SDL_FillRect(su rface, 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_SetVideoMod e(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(co nst block&)
../lgt.h:45: note: block::block(SD L_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 3336
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(co nst block&)
./lgt.h:45: note: block::block(SD L_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(co nst block&)
./lgt.h:45: note: block::block(SD L_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.w est.earthlink.n et...
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*******@rock etmail.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_SetVideoMod e(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_SetVideoMod e(screenw, screenh, bpp, screentype);
block initblock(scree n);
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************ **********@j44g 2000cwa.googleg roups.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_Surfa ce *);
} ;
>>>

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_SetVideoMod e(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<blo ck>(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<blo ck>(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<blo ck>(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************ **********@j72g 2000cwa.googleg roups.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<b lock 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
1869
by: Nick Jacobson | last post by:
This works fine: x = 1 def execfunc(): print x execfunc() So why doesn't this? s = \
4
2040
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 value=2>Two</OPTION>" Response.Write "<OPTION value=3>Three</OPTION>" Response.Write "</SELECT>" Response.Write "</form>"
4
9729
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 http://mysql.progen.com.tr/doc/en/Group_by_functions.html. I am an experienced VB programmer that has recently moved to PHP/mySQL. My employer has a text file outputted from a vendor specific software with data. However it cannot be manipulated because it is text. I created a web that reads the...
9
2024
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: struct PDF_s { /* -------------------------- general stuff ------------------------ */
6
751
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 register the javascript code with the RegisterClientScriptBlock. How how do I call the javascript function inside the block from within C# asp.net page. (yes I know popups are bad, please do not reply if you
10
3841
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 For...Next and If...End If blocks can have their own variables. So,
8
74606
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
1728
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
1613
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
857
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() ... ... for j in range(i): ... print j, ... print
0
9999
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9847
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11283
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10875
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10986
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5894
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6093
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.