Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 << "Our vectorsize: " << v.size() << endl; 18 cout << "STL vectorsize: " << stdv.size() << endl; 19 20 for(int i=0;i<10;i++){ 21 v.push_back(i+1); 22 stdv.push_back(i+1); 23 } 24 25 cout << "Our vector: ";// print our vector 26 for (int i = 0; i v1(5); 39 //now add some elements 40 for (int i = 0; i < 5;i++){ 41 cout << "The error: " << endl; 42 v1.push_back(i + 1);} 43 v1.push_back(7); 44 // cout << "The error: "<< endl; 45 //test copy constructor 46 vectorvcopy(v); 47 //print vector formed out ofcopy constructor 48 cout << "Our vcopyconstructor after calling copy constructor: "; 49 for (int i = 0; i

Hpp file: vector.h 1 #ifndef VECTOR_Himage 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 < s; i++) 53 v[i] =other[i]; 54 55 } 75image

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

Step: 1

The segment ation fault is caused by the fact that the code attem... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Managerial Accounting An Integrative Approach

Authors: C J Mcnair Connoly, Kenneth Merchant

2nd Edition

099950049X, 978-0999500491

More Books

Students also viewed these Electrical Engineering questions

Question

Why do computers use binary numbers?

Answered: 1 week ago