Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

<vector> problem 1

Status
Not open for further replies.

revit

Programmer
Oct 27, 2001
36
IL
I use the <vector> data structure from std.
the program is compiled without no errors, but when i run it with an input i get -Segmentation fault (core dumped).
i run it with debugger and descovered that in one point when i come to the line: MyVector.push_back(NULL);
the program gives me an error and jump to a file called :
Dbgheap.c - this happens after several times the program did MyVector.push_back(NULL); seccessfully.

please help!
Thanks!
 
Hi,
I already use: vector<class MyClass*> so i do'nt think it's the problem...
thanks...
 
Hi,
I already use: vector<class MyClass*> so i do'nt think it's the problem...
thanks...
 
what type of vector is it? ( vector<T> MyVector, what is T? ) and why do you need to push a NULL into it? try getting rid of the &quot;class&quot; in vector<class MyClass*> and just do vector<MyClass*> (ie. vector<int*> instead of vector<class int*>
 
Hello,
the vector is of type of pointer to a class i made:
vector<BRTree*>

that's why i pushed a NULL into it (it is the initialization)...
i tried to get rid of the &quot;class&quot; but it wo'nt help...

Thanks.
 
here i wrote a little code that might help you out. i made my own little BRTree class that im sure isnt want you have but it gets the point across. since you are using a vector of pointers, the syntax gets a little funky. i used arrows (->) and (*) to dereference it, it doesnt matter which you use, and i also showed that you can use pointers to the BRTree or the BRTree themselves, you just have to remember to dereference the ones that arent pointers. a is a regular BRTree and b is a pointer to a BRTree

#include<iostream>
#include<vector>
using namespace std;

class BRTree
{
private:
int num;
public:
void setnum(int n) {num=n;}
int getnum() {return num;}
void printnum() {cout<<num<<endl;}
};
void main()
{
BRTree a;
BRTree *b;
a.setnum(5);
b->setnum(10);

vector<BRTree*> vec;

vec.push_back(&a);
vec.push_back(NULL);
vec.push_back(b);


vector<BRTree*>::iterator i;
for(i=vec.begin();i!=vec.end();i++)
{
if((*i)!=NULL)
(*(*i)).printnum();
else
cout<<&quot;NULL&quot;<<endl;
}
}
 
Thanks! it helped me to debug the program and find my mistake!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top