I am receiving a segmentation fault whenever I attempt to push a templated object into a vector. I have ran gdb, and am still unable to understand why I get the segmentation fault error.
Program received signal SIGSEGV, Segmentation fault. 0x0000003f5869d4f3 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::assign(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) () from /usr/lib64/libstdc++.so.6
This is where I am inserting the object into the vector:
void ReadyDelivery::LoadTruck(){
string name = "";
int capacity = 0;
ifstream inputStream;
inputStream.open(m_truckFile.c_str());
while(inputStream >> name >> capacity ){
Truck<Item, MAX_CAPACITY> t(name,capacity);
cout<<name<<" "<<capacity<<endl;
m_truck.push_back(t);
}
cout<<"Trucks loaded: "<<m_truck.size()<<endl;
inputStream.close();
If I comment out where I push_back the object into the vector, there is no segmentation fault.
I also have a function that returns the vector. I am not sure if this could be causing it though...
vector<Truck<Item,MAX_CAPACITY> > & ReadyDelivery:: GetTruck(){
return m_truck;
}
Thanks for the help!
Here is the definition of m_truck, which is a private member variable:
private:
vector< Truck<Item, MAX_CAPACITY> > m_truck; //Vector of templated trucks
Here is where I construct the truck object in my template:
template <class T, int N>
Truck<T,N>::Truck(string inName, int capacity){
m_name = inName;
m_capacity = capacity;
}
If I run gdb and use the where command... I get this:
#0 0x000000346ce9d4f3 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::assign(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) () from /usr/lib64/libstdc++.so.6
#1 0x0000000000402bd9 in Item::operator= (this=0x60eb00) at Item.h:11
#2 0x0000000000402c66 in Tqueue<Item, 200>::~Tqueue (this=0x60e478,
__in_chrg=<value optimized out>) at Tqueue.h:96
#3 0x0000000000402b43 in Truck<Item, 200>::~Truck (this=0x60e450,
__in_chrg=<value optimized out>) at Truck.h:114
#4 0x00000000004027d5 in std::_Destroy<Truck<Item, 200> > (__pointer=0x60e450)
at /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_construct.h:90
#5 0x000000000040249c in std::_Destroy_aux<false>::__destroy<Truck<Item, 200>*>
(__first=0x60e450, __last=0x60e488)
at /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_construct.h:100
#6 0x00000000004021d3 in std::_Destroy<Truck<Item, 200>*> (__first=0x60e450,
__last=0x60e488)
at /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_construct.h:123
#7 0x0000000000401cdf in std::_Destroy<Truck<Item, 200>*, Truck<Item, 200> > (
__first=0x60e450, __last=0x60e488)
at /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_construct.h:149
#8 0x000000000040197c in std::vector<Truck<Item, 200>, std::allocator<Truck<Item, 200> > >::~vector (this=0x7fffffffe1b0, __in_chrg=<value optimized out>)
at /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_vector.h:313
#9 0x0000000000401593 in main (argc=1, argv=0x7fffffffe2c8) at driver.cpp:29