Question: C++ code has segmentation fault. Need help CPP file: buffers #include vector.h #include #include //We do not want to include either stmt. We wouldn't ////be

C++ code has segmentation fault. Need help

CPP file: buffers #include "vector.h" #include #include //We do not want to include either stmt. We wouldn't ////be able to compare our vector template to theStandard ////using namespace std; 7 ////using std::vector; 8 using std::cout; 9 using std::endl; 10 int main (){ 11 vector v; //Our vector class 12 std::vector stdv;//Standard vector 13 14 //Compare operation of ourvector to std 15 v.push_back(23); 16 stdv.push_back(23); 17 cout v1(5); 39 //now add some elements 40 for (int i = 0; i vcopy(v); 47 //print vector formed out ofcopy constructor 48 cout

Hpp file: vector.h 1 #ifndef VECTOR_Htry_vector.cpp 1 #include vector.h 2 #include 3 #include 4 //We do not 2 #define VECTOR_H 3 #include 4 #include 5 #include 6 #include 7 //using namespce std; 8 9 template 10 class vector { 11 private: 12 T*v; 13 int s; 14 int capacity; 15 public: 16 vector(){ 17 s=0; 18 v=NULL; 19 capacity=100; 20 } 21 vector(int cap){ //constructor 22 capacity = cap; 23 v = new T[cap]; 24 } 25 ~vector(){ //destructor 26 delete [] v; 27 } 28 29 int size() { 30 return s; 31 } 32 void push_back(T ele) { 33 T *temp; 34 //temp = new T[++s]; 35 if((s+1)>capacity){ 36 temp = newT[2*capacity]; 37 ++s; 38 }else 39 temp = newT[++s]; 40 41 for(int i=0; i &other){ //assignment operatoroverloaded 49 s = other.s; 50 capacity = other.capacity; 51 v = new T[s]; 52 for (int i = 0; i

try_vector.cpp 1 #include vector.h 2 #include 3 #include 4 //We do not want to include either stmt. We wouldn t 5 ////be able to compare our vector template to the Standard 6 ////using namespace std; 7 ////using std::vector; 8 using std::cout; 9 using std::endl; 10 int main () { 11 12 13 14 15 16 17 18 19 20 21 23 24 25 26 27 28 29 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 } vector v; //Our vector class std::vector stdv; //Standard vector //Compare operation of our vector to std v.push_back (23); stdv.push_back (23); cout < < Our vector size: < < v.size() < < < endl; cout < < STL vector size: < < stdv.size() < < endl; for(int i=0; i capacity) { }else temp = new T[2*capacity]; ++s; temp = new T[++s]; for(int i=0; i

Step by Step Solution

3.44 Rating (151 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The segment ation fault is caused by the fact that the code attem... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Electrical Engineering Questions!