C++ :: Iterating Through A Vector Of Pointers
Sep 4, 2014
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++)
[Code] .....
View 10 Replies
ADVERTISEMENT
Aug 21, 2014
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?
View 1 Replies
View Related
Jul 25, 2013
I have something like this:
class A {
};
class B : public A {
};
class C : public A {
};
B*b1;
B*b2;
C*c1;
C*c2;
vector<A*>vec;
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.
View 7 Replies
View Related
Nov 23, 2013
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?
View 3 Replies
View Related
Oct 14, 2014
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;
View 5 Replies
View Related
Mar 27, 2013
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.
View 15 Replies
View Related
May 13, 2014
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;
[Code] .....
View 4 Replies
View Related
Nov 5, 2014
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;
[code]....
What am I missing to get my code to work?
View 9 Replies
View Related
Nov 5, 2014
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:
//.h file:
class Time
{
unsigned int hours;
[Code]....
View 8 Replies
View Related
Sep 10, 2014
Is it possible to create a temporary
std::list of pointers
I would like to pass a temporary
std::list
to the constructor of a class to initialize its own one.
For example, using a
std::vector
:
#include <iostream>
#include <vector>
void func(const std::vector<int*>& myVec) {
for(int i=0; i<myVec.size(); ++i){
[code]....
Can we do this? What are other possible problems in addition the ones I have just mentioned above?
View 14 Replies
View Related
Sep 4, 2014
Lets say for example I have the following vector:
vector<Component*> mComponents;
and a function print() inside of class Component that prints the objects name.
How do I loop/iterate through the vector to access the print function in each object.
View 1 Replies
View Related
Apr 30, 2013
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
(instance.structures)[a][b][c] = smgr->addAnimatedMeshSceneNode(fl);
(instance.structures)[a][b][c]->setPosition(renderPos);
}
}
}
}
The problem is in these two lines, I think:
(instance.structures)[a][b][c] = smgr->addAnimatedMeshSceneNode(fl);
(instance.structures)[a][b][c]->setPosition(renderPos);
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?
View 4 Replies
View Related
Jul 3, 2014
Suppose I want to represent an apartment building in terms of floors and rooms with a single data object. Each floor has the same number of rooms at any given time. During the course of the program, I will need to change the number of floors occasionally and will need to change the number of rooms on each floor occasionally. (Don't worry about the building permits needed to do this.) Now, I also want to be able to iterate over either each floor (think "row") or each room (think "column"). For example, I might want to program the equivalent of "Johnson, go fix all the front doors on the 8th floor" or the equivalent of "Johnson, go fix all the front doors to room B on each floor". (Yeah, I know, for the latter I could iterate over each floor and then access room B but that doesn't feel as "clean" as being able to iterate over a separate room/column.)
View 7 Replies
View Related
Jul 31, 2014
Suppose I want to represent an apartment building in terms of floors and rooms with a single data object. Each floor has the same number of rooms at any given time. During the course of the program, I will need to change the number of floors occasionally and will need to change the number of rooms on each floor occasionally. (Don't worry about the building permits needed to do this.)
Now, I also want to be able to iterate over either each floor (think "row") or each room (think "column"). For example, I might want to program the equivalent of "Johnson, go fix all the front doors on the 8th floor" or the equivalent of "Johnson, go fix all the front doors to room B on each floor". (Yeah, I know, for the latter I could iterate over each floor and then access room B but that doesn't feel as "clean" as being able to iterate over a separate room/column.)
View 2 Replies
View Related
Feb 10, 2015
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?
View 2 Replies
View Related
Jan 21, 2014
This code work perfectly, as follows.
Code #A:
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
typedef std::vector <void *> gr_vector_void_star;
gr_vector_void_star output_items;
[Code] .....
Output of above code #A:
char * sentence = "Angel";
for (int i=0; i < 5; i++)
{ out[i] = sentence[i]; } // error: invalid conversion from 'char' to 'char*' [-fpermissive]
It fails to compile with error message "invalid conversion from 'char' to 'char*'".
View 19 Replies
View Related
Apr 4, 2013
Suppose I have a struct with 20 members. I want to assign each of those structs with values. Instead of accesing by explicit convention, i.e.:
Code: struct.member1 = 1;
struct.member2 = 2;
...
...
...
struct.member20 = 6;
Is there any way to encapsulate it in a for loop and use an iterator variable to indicate the member? i.e.:
Code: for (int i = 0; i < 20; i++)
struct.i = i;
The above is pseudocode but Im sure you get what Im trying to do?
View 2 Replies
View Related
Apr 11, 2014
I have a class called Question:
#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?
View 2 Replies
View Related
Jan 31, 2013
there is a way to iterate through the controls on a webpage. For instance, I tried EnumChildWindows using the parent window of the browser, but I haven't had any luck - it returns nothing. s where I can start researching how to find the child controls?
I'm using Visual Studio 2010, programming in C++.
View 6 Replies
View Related
Mar 17, 2015
what do I do if the while loop repeat iterating and do not stop running even if it did meet the conditions required?
View 3 Replies
View Related
May 2, 2012
I am writing code to access vector elements as, to print first "n" elements of vectors out of its total N elements at a time.
For example, Total vector elements : N= 23
Want to print n elements at time : n =3
How this can be explored using vectors?
View 6 Replies
View Related
Feb 28, 2014
I'm trying to create an array of pointers to pointers which will point to array of pointers (to strings) I tried
Code:
int i;
char *string[]={
"my name is dave",
"we like to dance together",
"sunny day",
"hello",
[code]...
the app keeps crashing , I don't know how to make the array-elements to point to another array-elements..
View 4 Replies
View Related
May 21, 2013
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??
View 2 Replies
View Related
Feb 9, 2015
How to output vector contents using the push_back function. My program reads in values just fine, but it does not output anything and I've been stuck on why.
here is my code:
#include <iostream>
#include <array>
#include <vector>
using namespace std;
int duplicate( vector < int > &vector1, const int value, const int counter)
[Code].....
View 3 Replies
View Related
Jun 7, 2012
I have a cpp app that reads in a number of files and writes revised output. The app doesn't seem to be able to open a file with a ' in the file name, such as,
N,N'-dimethylethylenediamine.mol
This is the function that opens the file :
Code:
// opens mol file, reads in rows to string vector and returns vector
vector<string> get_mol_file(string& filePath) {
vector<string> mol_file;
string new_mol_line;
// create an input stream and open the mol file
ifstream read_mol_input;
read_mol_input.open( filePath.c_str() );
[Code] ....
The path to the file is passed as a cpp string and the c version is used to open the file. Do I need to handle this as a special case? It is possible that there could be " as well, parenthesis, etc.
View 9 Replies
View Related
May 13, 2013
I need to create a class vector as a template and define operations on vectors.
And this is what I made.
#include<iostream>
using namespace std;
template<class T>
[Code].....
View 2 Replies
View Related