|
-
September 25th, 2017, 02:33 AM
#1
A non std::vector<> definition
Hi,
Please take a look at exercise 16 here.
I wrote the code below:
Code:
#include <std_lib_facilities_4.h>
using namespace std;
template <class T> 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<T*> vec; // vector of pointers
};
//***************************************
int main() {
ovector<double> v1;
for (size_t i = 0; i < 10; i++)
v1.push_back(new double(2.5*i));
cout << "vector<double> v1 = ";
for (const auto& v : v1)
cout << *v << ' ';
cout << endl;
*v1(0) = 3;
system("pause");
return 0;
}
I get these errors on line 54 where I want to use the overloaded operator* there:
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
What the problem could be please?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|