Currently I am implementing the A* algorithm in C++. I have chosen to use a hybrid of a '2D vector grid' and two 1D pointer vectors to specific places in the '2D vector grid'. I have chosen to do it this way, so I can access the nodes using coordinates and also for iterating over the appropriate nodes(rather than the whole '2D vector grid').
In the below examples I have not included the full code because I deemed it irrelevant to the question.
vector <int> CInGame::AStarAlgorithm(vector<vector<bool>>& resistanceMap, int startX, int startY, int targetX, int targetY, int cutOff) {
vector <int> returnVec;
vector <vector<CNode>> twoDimNodes;
vector <CNode*> openSet;
vector <CNode*> closedSet;
[code].....
The error is:
_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
do I need to free the pointers or is it done automatically? If not then how would I do this?
I made a vector of pointers and the problem is that I have trouble deleting the pointers in the vector. I used to simply do vector.clear() or vector.erase() however it does not clear the actual memory. And I tried something like this:
std::vector<Foo*> Vector; std::vector<Foo*>::iterator i; for (i = Vector.begin(); i < Vector.end(); i++) delete *i;
It's all pretty simple and straight forward, but oftentimes (not always) when the weapon hits the asteroid, the program crashes. If for example I turn off the delete main_weapon[i] and main_weapon.erase(main_weapon.begin()+i); the program works perfectly, but of course the bullets go right though the asteroid object, which is not what I want. It's only the weapon deletion which is causing no end of trouble.
I created a very basic program which contains a vector (my vector) that holds 0, 1 and 1. This program compares each element in the vector. If the element after the one currently being compared is equal to it, it outputs "repeat found!" Now this all works perfectly, but my next step was to erase the repeated one. Everything was working up until the delete step. I get a weird error that says "vector iterator not dereferencable" .
// vector::begin/end #include <iostream> #include <vector> using namespace std; int main () { vector<int> myvector;
case DELETE_TITLE: std::cout << "Game to remove from list: "; std::cin.ignore(); getline(std::cin, gameTitle); for (iter = gameList.begin(); iter != gameList.end(); ++iter) {
[code]....
It deletes the text from the string but not the index it self.
It deletes the text but when I print the list of game titles it has it there blank. I want it to completely remove the object from the vector from where it was deleted
I am new to smart pointers and have a question.If you push a smart pointer onto a vector and then some where later pop it back off it will delete the memory right?
int main() { vec.push_back(b1); vec.push_back(b2);
[Code] ....
And it don't works at all. all i get (when playing with variations of this stuff until it compiles correctlly) is a memory leak.
For example, let say i have b1 address = 1234
I will effectively free the memory at 1234, but for a strange reason, the memory leak is elsewhere, for example, at 2345, and the memory value at this address is equal to... 1234, the address of the pointer i wanted to delete.
If I have a Vector of pointers, how can I iterate through that vector, to access the name function of each object in the vector? There seems to be a problem with my implementation, the output returns only random symbols.
Implementation of the name() function in Drug.cpp:
//name() is a function returning the name in the parent class string Drug::name() { string out = "Drug: "; out += mName + " [ "; //The problem is with this loop for (int k = 0; k < mComponents.size(); k++)
I have a vector of pointers inside a seperate Exam class.
vector <Question* > question_list
The Question class is my base class in which I have derived sub classes for the different types of questions (MultipleChoice, LongAnswer, etc.). I am using my vector to hold the different types of questions.
in each of those classes I have virtual "write" functions in both the base and the derived classes, that write to a file differing for each type of question.
My problem now is calling the write function from a Exam function. I've tried several methods, such as:
for (size_t i = 0; i < question_list.size(); i++) { question_list[i].write(testfile.c_str()); }
but it comes with two errors: "error C2228:left of '.write' must have class/struct/union" along with "IntelliSense: expression must have class type"
I have made a write function for the exam class as well but am not sure what it should include since the Exam class is not a derived class of the Question class.
I need to create a vector of pointers and hold the book objects in it. then i have a virtual function in my books which is a pure virtual in LibraryItems. When i try to add the books object in my code, i understand that since the scope runs out there is no object that is added. so when i run print it gives me an error.
#include<iostream> #include "books.h" #include "library.h" #include <vector> using namespace std;
int main(int argc, char * argv[]) { vector<LibraryItems* >libraryInfo;
Im creating a program for a race. The Race class has a vector of results and each element of that vector is a pointer to a result. The Result class has a Time and a pointer to a Participant. So in each race there are various results and it is a result for each participant.The Time is a class that has hours, minutes and seconds. How can I sort the vector of results from the result of the participant with the fastest time to the result of the participant with the slowest time?My code is like this:
//.h file: class Time { unsigned int hours; unsigned int minutes; unsigned int seconds;
Im creating a program for a race. The Race class has a vector of results and each element of that vector is a pointer to a result. The Result class has a Time and a pointer to a Participant. So in each race there are various results and it is a result for each participant. The Time is a class that has hours, minutes and seconds. How can I sort the vector of results from the result of the participant with the fastest time to the result of the participant with the slowest time?
Im getting some errors in my code. I put the error as comments in the code. Each error is after the line where it occurs. My code is like this:
I have a vector (structures) in a struct (instances). I make a declaration of this struct called instance. The vector is a 3-layer vector of pointers, like so:
vector < vector < vector<scene::IAnimatedMeshSceneNode*> > > structures; (The type is from Irrlicht 3D). I have 3 nested "for" loops which looks similar to the following:
for (int a = 0; a < instance.structures.size(); a++) { /*note:vector size previously set*/ for (int b = 0; b < instance.structures[a].size(); b++){ for (int c = 0; c < instance.structures[a][b].size(); c++) {
if (1) { //checking value of variable not included in snippet
These are currently referencing the pointers, it seems. The program compiles but crashes at this point. I need them to reference the values of the pointers. Problem is, I don't know where to put the dereference operator (*). Where should it go?
Using SFML, I had a Board class which held multiple vectors of all of my object types in the game, and then it also held a vector of pointers to the memory addresses of these object instances, like this
class Board{ //... std::vector<AbstractObject*> GetAllLevelObjects(){ return allLevelObjects; } //so these are used to hold my object instances for each level
[Code]....
When looping through this vector and drawing the sprites of the objects, I get the runtime error 0xC0000005: Access violation reading location 0x00277000. I solved this error by storing the vector of pointers in the class that holds my Board instance, but I'm wondering why only this solution worked? Why couldn't I just have my vector of pointers in the same class that the instances of those objects were in?
#include <string> #include <vector> using namespace std; class Question { string title; vector<Thing*> posAns; vector<Thing*> negAns;
[Code] ....
error: no instance of overloaded function 'std::vector::push_back()' matches the arguments list argument types are (const Thing *) object type is: std:: vector<Thing *, std::allocator<Thing *>>
So it cannot be constant, what if I just leave it non-constant? Will it be safe?
I am a little confused while comparing char pointers to integer pointers. Here is the problem:
Consider the following statement; char *ptr = "Hello"; char cArr[] = "Hello";
When I do cout << ptr; it prints Hello, same is the case with the statement cout << cArr;
As ptr and cArr are pointers, they should print addresses rather than contents, but if I have an interger array i.e. int iArr[] = {1, 2, 3};
If I cout << iArr; it displays the expected result(i.e. prints address) but pointers to character array while outputting doesn't show the address but shows the contents, Why??
Basically, I have a LIST that is saved in a CSV file, and I need to remove a particular record after using the search feature (which populates the fields from the CSV file... i.e. ID, Name, etc...).
I am able to get the details that are in the text fields by using a search feature. This is then put in:
logic.remove(tempIndividual)
In class myLogic.cs, then I have this method:
this.individual.Remove(tempIndividual)
Upon debugging, I can see that tempIndividual is populated with the correct data. However, I need to remove this data from the CSV file. Am I on the right track, or totally off? Maybe it is not complete, cause I can't get to the part where it actually removes the data from the CSV, or at least I'm not sure if .Remove is able to do it on its own.
I am writing a program to list the 8 planets, then you select which planet you want. it gives you the mass of the planet, the radius, then you use the mass and radius of the planet to find the surface area (sphere formula) and the density of the planet. Along with those options, you have to add and delete a planet from the list and then sort them alphabetically. All i am having trouble with is the adding and deleting part. The code the adding and deleting from the list would go in cases 9 and 10.