### Defining some non-std vectors

Hi,
Please take a look the exercises 16 and 17 here:
https://s2.postimg.org/wyjwt43ll/image.png

For 16 this code:

 ``12345678910111213141516171819202122232425262728293031323334353637383940`` ``````#include using namespace std; template class ovector { public: ovector() = default; // default constructor ovector(const ovector& N) // Copy constructor { for (const auto& v : N.vec) push_back(new T(*v)); } ovector& operator= (const ovector& N) // Copy assignment { auto l = N; swap(l.vec, vec); return *this; } ~ovector() { for (auto ptr : vec) delete ptr; } auto begin() const { return vec.begin(); } auto end() const { return vec.end(); } auto size() const { return vec.size(); } void push_back(T* ptr) { vec.push_back(ptr); } void pop_back() { delete vec.back(); vec.pop_back(); } void resize(size_t n) { vec.resize(n); } T& operator*(size_t pos) { return *vec[pos]; } T& operator[] (size_t pos) { return *vec[pos]; } const T& operator[] (size_t pos) const { return *vec[pos]; } private: vector vec; // vector of pointers };``````

And for 17 I added `vector<T*> del;` to the private section and changed the destructor to:

 ``1234`` ``````~ovector() { for (auto ptr : del) delete ptr; }``````

The code for 16 doesn't work and the one for 17 looks very awkward to me.

Would you please explain the exercises for me what they really want?
I don't want code, only clearing the issue is good. Thanks.
Last edited on
 The code for 16 doesn't work

Why doesn't it work?

 And for 17 I added vector del; to the private section

Why did you add another vector? Why didn't you just use the vector that is already in the class?

 Would you please explain the exercises for me what they really want?

What does the code to exercise 15 look like? Does the code for Exercise 15 compile and produce the desired results? Exercise 16 is based on the code for Exercise 15.

Exercise 16 is probably trying to illustrate the differences between pointers and references in the content of the "enhanced" vector class.

 and the one for 17 looks very awkward to me.

What, exactly, looks awkward to you? You need to iterate through the vector and delete the memory, which your loop accomplishes.

When dealing with templates you should post a small complete program that illustrates the problems you are encountering. You need to show how you're using that template class.

Last edited on
 Why doesn't it work?
I've defined the operator * in line 33 and use it like:

 ``12345678910111213`` ``````int main() { pvector v1; for (size_t i = 0; i < 10; i++) v1.push_back(new double(2.5*i)); cout << "vector v1 = "; for (const auto& v : v1) cout << *v << ' '; cout << endl; *v1(0) = 3; return 0; }``````

It shows these errors for line 11:

Severity Code Description Project File Line Suppression State
Error (active) E0980 call of an object of a class type without appropriate operator() or conversion functions to pointer-to-function type

Severity Code Description Project File Line Suppression State
Error C2064 term does not evaluate to a function taking 1 arguments

 Why did you add another vector? Why didn't you just use the vector that is already in the class?
It says "which objects are deleted by the destructor". And destructors don't accept arguments to, for example, tell them which object to delete. So I thought of a vector the user puts whatever object they want to delete into that and in the destructor to delete that vector.

 What does the code to exercise 15 look like? Does the code for Exercise 15 compile and produce the desired results? Exercise 16 is based on the code for Exercise 15.
The code for 16 minus the operator * is the code for 15 and I used as above in main() and works fine.

 What, exactly, looks awkward to you?
I meant defining another vector (del) to be able to tell the destructor which object to be deleted.

Last edited on
 It says "which objects are deleted by the destructor". And destructors don't accept arguments to, for example, tell them which object to delete. So I thought of a vector the user puts whatever object they want to delete into that and in the destructor to delete that vector.

Did you still have the other vector in the class? If so did you delete the elements of that vector as well?

For your error messages shouldn't main() be more like:
 ``1234567891011121314151617181920`` ``````int main(){ ovector v1; // Note the use of ovector instead of pvector to match the class definition provided. for(size_t i = 0; i < 10; i++) v1.push_back(new double(2.5 * i)); cout << "vector v1 = "; for(const auto& v : v1) cout << *v << ' '; cout << endl; v1[0] = 3.0; // Notice the change here. cout << "vector v1 = "; for(const auto& v : v1) cout << *v << ' '; cout << endl; return 0; }``````

And I changed your destructor so that it prints the elements when it deletes them.

 ``1234567`` `````` ~ovector(){ for(auto& ptr : vec) { cout << "deleting: " << *ptr << endl; delete ptr; } }``````

By the way I was asleep for Chapter 13 so I don't know what the author means by having the destructor decide what to delete.
Last edited on
Let's solve the exercise 16 first.
This is the code I test for this point of the code. I get those errors for line 54 where I want to use the overloaded operator* there.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758`` ``````#include using namespace std; template class ovector { public: ovector() = default; // default constructor ovector(const ovector& N) // Copy constructor { for (const auto& v : N.vec) push_back(new T(*v)); } ovector& operator= (const ovector& N) // Copy assignment { auto l = N; swap(l.vec, vec); return *this; } ~ovector() { for (auto ptr : vec) delete ptr; } auto begin() const { return vec.begin(); } auto end() const { return vec.end(); } auto size() const { return vec.size(); } void push_back(T* ptr) { vec.push_back(ptr); } void pop_back() { delete vec.back(); vec.pop_back(); } void resize(size_t n) { vec.resize(n); } T& operator*(size_t pos) { return *vec[pos]; } T& operator[] (size_t pos) { return *vec[pos]; } const T& operator[] (size_t pos) const { return *vec[pos]; } private: vector vec; // vector of pointers }; //*************************************** int main() { ovector v1; for (size_t i = 0; i < 10; i++) v1.push_back(new double(2.5*i)); cout << "vector v1 = "; for (const auto& v : v1) cout << *v << ' '; cout << endl; *v1(0) = 3; system("pause"); return 0; }``````

 I get those errors for line 54 where I want to use the overloaded operator* there.

To use the overloaded operator * as you have defined it you would need to do the following `v1 * 0 = 3;` since you have overloaded the multiplication(Binary) operator not a "pointer"(unary) operation.

Edit: By the way I gleaned this information from what appears to be your thread in another forum: