+ 1
Display unchanged array when I call it
I insert a element into array and have a function that returns me the value where the insertion but I am getting the value wrong when I ask for its position. Look at the code https://code.sololearn.com/cX8AuakKH4y6/?ref=app
8 ответов
+ 5
If instead of copying, you pass &array you can make array_ as address of array. Then, you can modify what the array points to when it gets bigger or smaller.
+ 3
The vector copies the array into its own storage and forgets it existed.
+ 3
Write your own Vector class.
+ 2
Line 27
+ 2
Apart from what John Wells said, I see many problems in the code.
1. The constructor that is the spotlight of this question is not a good one. A vector class should maintain its own buffer and not allow users to give it the control of an unknown buffer.
2. In the copy constructor, you are allocating a buffer of x.size_ elements, but setting the capacity to x.capacity_. This is a blunder if x.size_ is not equal to x.capacity_
3. In the constructor on line 12, copy-constructor and copy-assign operator, you are not deallocting the already existing buffer. This id a memory leak. Keep in mind that just doing
`delete[] this->array;`
Will call the destructor on each element in the vector
4. In the destructor, you are deallocting the buffer when array_ IS nullptr, instead of when array_ IS NOT nullptr
0
John Wells how would i be able to resolve this problem?
0
I dont see where im storing the arrays into a vector
0
Getting an error that we cant asign to it unknow size