c++ vectors causing break in my game [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
my works like this i shot airplanes and if the shot toughed an airplane it change there positions(airplane and the shot) than it will delete them. if i toughed an airplane the airplane position gets changed. than it gets deleted than my health reduces. well that works well expect that it breaks when i have about 2-4 airplanes left and i toughed one of them and shot one of them my game breaks it pops a box says vector out of range... i have had something like this problem before i fixed it because i new where was the problem but the box doesn't give me any information where it is the problem. here my full code
Thanks in Advance
note: i don't think i can go any lowwer with my code i have put new one

Well I just had a quick look at that monstrosity, and I'm not going to look too far into it but right off the bat I see a problem.
I don't know if this is related to this particular problem you are asking about, but this type of thing isn't going to work the way you think:
for(long index=0; index < (long)RegularShots.vshots.size(); ++index)
{
RegularShots.vshots[index].y-=2;
// Delete Shots
if(RegularShots.vshots[index].y<-16)
{
RegularShots.vshots.erase(RegularShots.vshots.begin()+index);
}
}
When you erase an item from the vector, the next item in the vector is moved down one, so every time you erase an item, you are skipping over the next item and not performing your test on it.
A quick fix here would be to do an index-- after the erase call.
But based on what I've seen, I would seriously recommend that you do some reading on STL containers and iterators in particular. And then read some books on good coding style ;)

Learn to 'refactor': for example, code like line 241 looks like it would be better as a subroutine.
Anyway, to find where this particular problem is, do "Debug/Break All" with the debugger while the error box is being displayed; and then look at the debugger's "Call stack" window, to see what code is being executed which triggers the popping of the box.

Related

Creating minesweep in C++ using vector arrays [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
So basically im trying to create the computer game minesweeper for uni. I got it working with arrays, but now i have found out i have to use vectors instead which i am completely helpless with. If any one could help me it would be great. Basically i want to use vectors to create a matrix that will get bigger after each round of the game is played. There will be three rounds, the first one starting with a 9x9 matrix, then 12x12 then 24x24. One of the arrays will be made up completely of X's and the second will have randomly generated mines hidden within it, along with any neseccary numbers that are touching the mines. Any help at all would be much appreciated and if there were little pointers in any code that is sent to help me understand it would be fantastic :)
Thanks
Why don't you use a vector of vectors, e.g.vector<vector<int>>? I'm not sure if this has performance issues. But it's just a simple game, so who cares?
It's pretty easy using vectors.
vector<vector<char> > map ( size, vector<char> ( size ) );
That creates a 2d vector of char's of size x size.
How you use it exactly depends on the structure of your game's code.

Massively struggling with laying out DirectX and windows [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have managed to get this example to work:
http://msdn.microsoft.com/en-us/library/windows/desktop/dd370994(v=vs.85).aspx
My problem is that I am trying to structure the code there into something more understandable...
My first step is to remove all the stuff that's not absolutely necessary to lanching a window with a direct x surface... but as soon as I start messing with the code, I get massively confused at which components are core, and which are not...
Do you know of a kind of boilerplate project with literally the utmost basics in it for a directx surface in a window.
D2D is fine unless you suggest I just code straight in D3D... even though i'm only going to be drawing 2d stuff.
I've read so many resources and tutorials for directX but every blooming one has different bits and bobs placed in and it's so difficult to just learn what the "must" bits are!...
Just to help you with the question.
I have started by trying to create a Gfx object that will start and create surfaces and resources for direct X, I also tried to separate my windows code but ofcourse got a bit lost there too... Finally I wrapped up my Game class so that the windows loop just ran begin draw, render and end draw. in the tutorial above you have 2 brushes which as i'm not familiar with directX, I'm unsure if I can just whip them out completely or what?
I guess a step in the right direction is really what i'm looking for.
I very strongly suggest that you get hold of a decent book that can guide you through the steps:
For example - try to get a copy of this

Graph editor on Qt [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm writing a graph editor now. The programs must be written on Qt, but the problem is that I don't have enough experience with Qt, but I'm little experienced with C++. Last weeks I read one book about Qt, but I've read about 100 pages, so I know only some basics.
Can somebody give me advices about what classes should I use, please?
What I've already done:
I filled menubar with menus File, Algorithms, About etc.
I suppose that graph vertexes etc I should draw on QGraphicsView, so I added it too, also I add QGraphicsScene and bind them together (ui->setScene(scene)).
Sorry for my mistakes, unfortunately I don't have a complete understanding about all these things.
Also I added QGraphicsRectItem with scene->addRect() and set the flag moveable. But I don't know what classes should I have to use.
So, in general I want to understand how to do next things:
I want to add a panel with 2 buttons (vertex, line). After I chose vertex button (or what it will be) and click on the QGraphicsView - vertex should appear at that point. Also I must be able to change vertex name
After I chose line mode (link, which connected 2 vertexes) I should be able to connect 2 vertexes together depending on graph type (oriented or not).
By clicking on link between vertexes I should be able to change the weight of link.
I think it would be enough for one question.
Sorry if the question is very simple or stupid.
Thanks.
Your question is rather broad so it's nearly impossible to answer it entirely. So instead, I'll offer a really good example you might go look at. This example does much of what you want and might be a good starting set of code to both look at as well as reuse.

Dynamic allocation as a parameter, so to speak? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Assume we have a vector of Cursor object pointers. A cursor object is constructed with a single int parameter. So will this syntax work?
vector<cursor*> cursors;
cursors.push_back(new cursor(4));
Or do I have to do it as:
cursor* tempCursor = new cursor(4);
cursors.push_back(tempCursor);
Despite the fact that you may not have tried this here's an explanation of what's going on:
when you create a new cursor object it returns a new cursor object. When you use a push_back function it pushes an object back on a vector. So, when you create a new object inside the push_back it evaluates that function which returns a new cursor which then gets pushed back.
Basically its all about return values and evaluations.
You will leak memory if you do this as you are stating and don't clean it up afterwards.
A better way would be to use a shared_ptr.
The code would look like this
cursors.push_back(std::make_shared<cursor>(4));
It's a little hard to understand exactly what you're trying to accomplish though.
As mentioned below in the comments by #cat-plus-plus, unique_ptr should be used unless you explicitly want the object shared elsewhere, the code then would look like the following:
cursors.push_back(std::unique_ptr<cursor>(new cursor(4)));
It will work, but chances are pretty good that you're better off doing something else. Absent a really good reason to do otherwise, I'd consider making it a vector of cursors (instead of pointers to cursors). In this case, you can just do something like:
std::vector<cursor> cursors;
cursors.push_back(4); // or cursors.emplace_back(4);
This generally improves programmer efficiency by automating memory management, and improves code efficiency by eliminating unnecessary levels of indirection.

How to fix heap corruption in c/c++? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
This is a further question for my previous one here.
Is there a general solution for this?
Fix all dangling pointers
Fix all buffer overflows
Use pointers only where they are really needed
Reading your original post I'm not 100% you are facing a heap-corruption and you really hope you don't because if you would this is one of the most tricky errors to track down and AFAIK there are no solutions that always work.
However, if you are positive its a heap-corruption and you are on the windows platform you could try out the tool gflags. This advanced debugging tools allow you to install a debug heap manager in order to make it possible to find certain kinds of heap corruptions. One of the neat options it has is to make each allocation in it's own page or to write protect the heap datastructures. This makes it easier to pinpoint the exact location of the heap-corruption. It sucks lots of memory and CPU though.
Another tip if you are using Visual Studio is that if you manage to find that something is corrupting the data after a certain point of time and you wish to find the code that corrupts the data you can use Data Breakpoint to break whenever someone changes the data in question.
I wish you good luck.
Adding to swegi's answer I would check all your C++ constructors and all C initializations. Transform all dynamic C++ constructors (those where you put the init code in the function body) into static ones (where you initialize with the special constructor syntax). And be sure that you init all pointers and pointer members to 0. For C, I would initialize all variables.
Then, on unix I would use valgrind, usually this is quite good in finding all access violations and if you compile with all debugging options on it is able to trace it to the source line. There should be something similar on windows, too.