Sign in to follow this  

SFML Error! :(

This topic is 1919 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So, I'm getting this error:

[CODE]
10.0/vc/include/vector
Line: 932
Expression: Vector Subscript out of range
[/CODE]

Whenever I create a MainGame object(The code is below) I get this error.
I'm pretty sure I'm using constructors wrong or I'm doing something with vectors that's stupid, but I can't track down the error(Believe me I'm trying!).

[url="http://www.mediafire.com/?s6ntr1ky1ydbbue"]Download Code Here[/url]

Share this post


Link to post
Share on other sites
Your code's a little messy and confusing:

1) Your main.cpp doesn't seem to be your actual main.cpp, since it's just basically a HelloWorld

2) Your actual main.cpp seems to be BREAKOUT.cpp. And in that file, you instance an object of type GameMain and call it MainGame. Crikey, confusing much?

3) Your post indicates you are creating a MainGame object, but you are actually creating a GameMain object. (Unless BREAKOUT.cpp is not your actual main.cpp)

This kind of thing makes it hard to convince anyone to take a look at your code.

That being said, have you tried stepping through with the debugger? Knowing exactly which line triggers the exception, and what the current state values are, can go great lengths toward knowing what the problem is.

Share this post


Link to post
Share on other sites
Oh, and BREAKOUT.cpp is Main.cpp, in visual studio your "actual" main.cpp has your solutions name, Sorry for the misunderstanding :).
I should rename that! I'm not sure what I should name it too, I'll think about it.
Typo! Sorry!

Share this post


Link to post
Share on other sites
1) Again, if you're sharing you code, don't include things like the contents of the Debug/ directory in the archive.
2) It looks like the problem is in your GameDraw constructor, which tries to assign to elements of a vector that you didn't initialize.
3) Also, even if that did work, it would probably break later because you're passing a vector to that function by value, which makes a temporary copy, and then tries storing pointers to the elements of that temporary vector. When the constructor finishes running the temporary vector gets destroyed and you're left with pointers to destroyed objects.

Share this post


Link to post
Share on other sites
1) I forgot! Stupid me. I'll do that next time.
2) I figured as much, I'll try passing by reference.
3) Same as answer two, I'll pass by reference.

Thanks for the help, I'll update you if I fix it.

Share this post


Link to post
Share on other sites
Passing by reference won't fix number two, the vector you are [i]assigning to[/i] doesn't have any elements in it. You either need to initialize it in the member initializer list, resize() it before you start accessing it, push_back() the pointers you are storing, or something else that actually allocates members in the vector.

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1350254649' post='4990175']
1) Again, if you're sharing you code, don't include things like the contents of the Debug/ directory in the archive.
[/quote]

To this point, I had to share a number of C++ projects in the past so [url="http://www.gamefromscratch.com/post/2011/08/25/Project-Cleaner.aspx"]I created a utility[/url] that cleaned them up shrinking them down. You may find it useful.

Share this post


Link to post
Share on other sites
So I fixed it doing everything you did, But now it says my image file is too large!
it says it's this (in format: amount of pixels width x amount of pixels height ): (300000000 pixels by 300000000 pixels) and it won't load the images! What?

Share this post


Link to post
Share on other sites
Does it actually say 3435973836 or 3452816845? Those would be the decimal values for 0xCCCCCCCC or 0xCDCDCDCD which are MSVC fill patterns for uninitialized memory. One is for uninitialized heap and the other for uninitialized stack. At a guess, you've run into the member variable constructor order problem, which happens when the compiler constructs member variables in order of their declaration in the class definition and not the order you specify in the member initialization list.

Share this post


Link to post
Share on other sites
Yes SiCrane, that's exactly the numbers! It says it is uninitialized by (3435973836 x 3435973836) two times, for two images. Can you elaborate on the member variable constructor order problem that you described :)?

Share this post


Link to post
Share on other sites
When an object is constructed, first any base classes are constructed, then its member variables are constructed in order of declaration in the class definition, regardless of what order you put them in the member initialization list. If you use one member variable to initialize another member variable in the member initialization list of a constructor you need to be sure that any member variable referenced actually appears above in the class definition. The easiest way to do this is to make sure that your member initialization list always follows the same order as the class definition and don't reference member variables that appear later. MSVC is unfortunately just about the only major compiler on the planet that won't give you a warning for not putting your member initialization list in class declaration order (which is frankly stupid).

Share this post


Link to post
Share on other sites
I did that, and now It shows the ball moving, you can't move your paddle, and none of the blocks are displayed. Well, I'm still trying to fix it :(.

Share this post


Link to post
Share on other sites
Okay, I'm not trying to fix it. I need to do something simpler. I do not have the knowledge required yet to make breakout. I am now going to attempt a space invaders clone, or maybe something of the sort. This project is dead, and the code design is stupid. I need to start preplanning, creating interfaces, and designing my classes better.

Share this post


Link to post
Share on other sites
[quote name='superman3275' timestamp='1350266912' post='4990212']
I need to start preplanning, creating interfaces, and designing my classes better.
[/quote]

To be honest, you don't. You aren't really at that stage yet where you should really be worrying about design all that much.

What you should focus on is figuring out *WHY* your current project doesn't work. You can't design around a problem you don't understand.

It's a frustrating experience, working through a problem you don't understand, but it is a valuable part of learning anything. Switching from Breakout to Space Invaders will not help you in the least. Now, if your problems are stemming from an overly complicated design ( aka, biting off more than you can chew ), by all means, revisit and simplify, but don't abandon a project just because you can't get it to work. The most likely reason is a lack of understanding of the language itself, which is a problem you are going to have to face and starting over isn't how you do it.

Finally, and I will say this like a broken record... [i][b]you can't skip the learning curve, don't bother trying[/b][/i]. You really are just going to make things harder on yourself over all.

I don't mean to be harsh, but comments like this are extremely telling:

[i]How can I use visual studios debugger?[/i]

Instead of starting a new project, this is EXACTLY what you should be learning next. You will advance your skillset a thousand fold more by learning how to debug, than you will by going from one game type to another. Until you have a pretty solid grasp of debugging, you will never really understand code. Trust me on this, the insight you will gain from interacting with your code as it runs will be a massive eye opener.

A quick google turned up [url="http://courses.cs.vt.edu/~cs1044/Notes/A02.MSVCDebug.pdf"]this introduction[/url] (pdf link) which seems pretty good. It also turned up a lot of crap tutorials too, oddly debugging seems to be something people did a lousy job of explaining... the one intro do debugging tutorial ( one of the top results ), jumped straight in to disassembly, perhaps the last thing a beginner needs to know.... There also appear to be a ton of video tutorials on YouTube, but I've personally never been a fan of a) video tutorials b) YouTube. Edited by Serapth

Share this post


Link to post
Share on other sites
Judging by your errors, the problem isn't that breakout is too complicated for you, the problem is that you're trying to tackle too much of the complexity all at once. Start in smaller chunks and grow slowly, testing as a you go. Don't try constructing a ball, paddle and bricks all at once. First put in the paddle, then maybe see about moving it around and then put in something else.

Share this post


Link to post
Share on other sites
Sign in to follow this