Greetings, I'm trying to design a "copy constructor", everything was fine until I came up with the copy algorithm and I have obtained a C2780 because apparently it only recognizes 2 of 3 arguments for copy ...
#include<iostream>
#include<algorithm>
using namespace std;
// a very simplified vector of doubles
class vector {
int sz; // the size
double* elem; // a pointer to the elements
public:
vector(int s) :sz{ s }, elem{ new double[s] }
{ for (int i = 0; i<sz; ++i) elem[i] = 0.0; } // constructor
vector(const vector&); // copy constructor: define copy
~vector() { delete[] elem; } // destructor
int size() const { return sz; } // the current size
double get(int n) const { return elem[n]; } // access: read
void set(int n, double v) { elem[n] = v; } // access: write
};
vector::vector(const vector& arg)
// allocate elements, then initialize them by copying
:sz{ arg.sz }, elem{ new double[arg.sz] }
{
copy(arg, arg + sz, elem); // std::copy(); see §B.5.2
}
int main()
{
vector v(5);
for (int i = 0; i < v.size(); ++i) {
v.set(i, 1.1*i);
cout << "v[" << i << "]==" << v.get(i) << '\n';
}
}
Thank you very much for your time.