Template copy constructor - c++

I need to create a copy constructor for my class MyVector.
#include <iostream>
using namespace std;
template<class T>
class MyVector{
private:
T *v;
int size;
int max;
public:
MyVector();
MyVector(const MyVector &l);
MyVector& operator=(const MyVector &lhs);
T &operator[](unsigned int i);
};
int main() {
return 0;
}
template<class T>
MyVector& MyVector<T>::operator = (const MyVector &lhs){
if (this == &lhs) return *this;
for (int i = 0; i < size; ++i){
delete v[i];
}
delete [] v;
max = lhs.max;
size = lhs.size;
v = new T[max];
for(int i = 0; i < size; ++i) {
v[i] = new T(*(lhs.v[i]));
}
return *this;
}
im getting the error: expected constructor, destructor, or type conversion before âmyVectorâ
Not sure where the problem is, I am fairly new to c++.
Thanks.

The following is the problem:
template<class T>
MyVector& MyVector<T>::operator = (const MyVector &lhs){
/* ... */
}
The return type, MyVector& should be MyVector<T>& because you're outside of the class scope, so you need to provide the template parameter there, like this:
template<class T>
MyVector<T>& MyVector<T>::operator = (const MyVector &lhs){
/* ... */
}

In the return for the function implementation, write MyVector<T>.
template<class T>
MyVector<T>& MyVector<T>::operator = (const MyVector &lhs)
{
Note that you're implementing an assignment operator, not a copy constructor.
A nice alternative is to use C++11 syntax where the return type follows the function head:
template<class T>
auto MyVector<T>::operator = (const MyVector &lhs)
-> MyVector&
{

MyVector(const MyVector &l);
should be:
MyVector(const MyVector<T> &l);
and similarly everywhere else you use MyVector as a type, since the type is not complete without the template arguments.

Related

Overloading operator= in c++ templates (objects)

I'm trying to write an overload to operator = for templates objects,
I'm creating a template matrix. I need that if I will do something like: m[i][j] = m2[i][j]; it should work with any kind of parameters and objects.
this is my code:
copy constructor:
template<class T>
inline Matrix<T>::Matrix(const Matrix& other)
{
this->_rows = other._rows;
this->_cols = other._cols;
this->_array = new T*[rows];
for (int i = 0; i < rows; i++)
this->_array[i] = new T[cols];
// Copy 'temp' to 'this'
for (int i = 0; i < this->_rows; i++)
for (int j = 0; j < this->_cols; j++)
this->_array[i][j] = other._array[i][j];
}
operator= overload:
template<class T>
inline T& Matrix<T>::operator=(const T &obj)
{
// Is the same
if (this == &obj)
{
return *this;
}
// Go to copy constructor of 'T'
T* temp = new T(obj);
return temp;
}
can you tell me what I need to change or fix please?
The problem of your code is that you trying to assign the Matrix<T> by reference of T, so Matrix<T> object will didnot be affected by assignments.
In design you must always return Matrix<T>& from your assignment operator. So the code must look like this.
template<class T>
inline Matrix<T>& Matrix<T>::operator=(const T &obj)
{
// Some object member assignment
m_SomeMember = obj;
return *this;
}
You in this case must not check objects for dublicates because T is not Matrix<T>, its just reassign object with different type member.
If you still want to assign Matrix<T> object with another Matrix<T>. You code must look like:
template<class T>
inline Matrix<T>& Matrix<T>::operator=(const Matrix<T>& obj)
{
if (this == &obj)
{
return *this;
}
// Performing deep copying
m_SomeMember = obj.m_SomeMember;
return *this;
}
I have also detected problems with your copy constructor. You must use:
template<class T>
inline Matrix<T>::Matrix(const Matrix<T>& other)
{
/* Copy constructors body */
}
instead of
template<class T>
inline Matrix<T>::Matrix(const Matrix& other)
{
/* Copy constructors body */
}

Can't access global Swap function within from a class

this is my first question on the site.
A little problem met me today. I can't call a global function Swap within a assignment operator and move constructor from the DynamicArray template class. When I try to do this I get an error C2660 saying:
'DynamicArray<int>::Swap' : function does not take 2 arguments.
It's strange for me, because I can call this function with 2 arguments in main function. Does anyone know what is the reason of that?
#include <iostream>
#include <stdexcept>
using std::cout;
using std::cin;
using std::endl;
template <typename T>
class DynamicArray;
template <typename T>
inline void Swap(DynamicArray<T>& lhs, DynamicArray<T>& rhs) {
lhs.Swap(rhs);
}
template <typename T>
class DynamicArray {
public:
DynamicArray(size_t Size = 0, T Content = T())
: m_Size(Size), m_Array(Size ? new T[m_Size] : nullptr) {
for (size_t i = 0; i < m_Size; ++i) {
m_Array[i] = Content;
}
}
DynamicArray(const DynamicArray<T>& rhs)
: m_Size(rhs.m_Size), m_Array(m_Size ? new T[m_Size] : nullptr) {
std::copy(rhs.m_Array, rhs.m_Array + m_Size, m_Array);
}
DynamicArray(DynamicArray<T>&& rhs)
: DynamicArray<T>() {
this->Swap(rhs);
//Swap(*this, rhs); //C2660
}
DynamicArray<T>& operator= (DynamicArray<T> rhs) {
this->Swap(rhs);
//Swap(*this, rhs); //C2660
return *this;
}
~DynamicArray() {
delete [] m_Array;
}
void Swap(DynamicArray<T>& rhs) {
std::swap(m_Size, rhs.m_Size);
std::swap(m_Array, rhs.m_Array);
}
private:
size_t m_Size;
T* m_Array;
};
int main() {
DynamicArray<int> DA1(4, 64);
DynamicArray<int> DA2(2, 33);
DA1 = std::move(DA2); //using move constructor
DynamicArray<int> DA3(8, 4);
DynamicArray<int> DA4(2, 55);
DA3 = DA4; //using assignment operator
Swap(DA1, DA3); //here I can call this function with 2 arguments
cout << endl;
system("pause");
}
To specify that you want to call the global Swap, you should prefix it perhaps with the scope resolution operator ::, like this ::Swap(...).
It's been a long while since I last coded in c++ but I believe the member Swap method takes precedence in the resolution of the Swap symbol, when it's used within a member method.
To explicitly use std::swap function try:
void Swap(DynamicArray<T>& rhs)
{
using std::swap;
std::swap(m_Size, rhs.m_Size);
std::swap(m_Array, rhs.m_Array);
}
DynamicArray(DynamicArray<T>&& rhs)
: DynamicArray<T>()
{
this->Swap(rhs);
}
...
Swap<int>( DA1, DA3 );
but I would remove global inline void Swap(DynamicArray<T> and start using:
DA1.Swap( DA3 );

Strange "Member function not viable" error in templated linear algebra vector class

I'm implementing a templated vector class (not the data container, but the vector in the linear algebra sense), and I'm getting quite a few errors whenever I refer to rhs in my operator overloading. Also, my copy constructor doesn't seem to be working.
#ifndef __VecXd__VecXd__
#define __VecXd__VecXd__
#define ULL unsigned long long
#include <iostream>
using namespace std;
template <class T>
class VecXd
{
public:
explicit VecXd(ULL newDimension = 1) { dimension = newDimension; vector = new T[newDimension];}
VecXd(const VecXd<T> &rhs);
VecXd<T>& operator=(const VecXd<T> &rhs);
const VecXd<T> operator+(const VecXd<T> &rhs) const;
VecXd<T>& operator+=(const VecXd<T> &rhs);
friend ostream& operator<<(ostream &out, VecXd<T> vec);
friend istream& operator>>(istream &in, VecXd<T>& vec);
~VecXd() { delete[] vector; }
const ULL getDimension() { return dimension; }
const T itemAtIndex(ULL index) { if(index >= dimension) throw 1; return vector[index]; }
private:
ULL dimension;
T *vector;
};
template <class T>
VecXd<T>::VecXd(const VecXd<T> &rhs)
{
dimension = rhs.getDimension();
vector = new T[dimension];
for (ULL i = 0; i < dimension; ++i)
vector[i] = rhs.itemAtIndex(i);
}
template <class T>
VecXd<T>& VecXd<T>::operator=(const VecXd<T> &rhs)
{
if (this != &rhs)
{
if (dimension != rhs.getDimension())
{
delete [] vector;
dimension = rhs.getDimension();
vector = new T[dimension];
}
for (ULL i = 0; i < dimension; ++i)
vector[i] = rhs.itemAtIndex(i);
}
return *this;
}
template <class T>
VecXd<T>& VecXd<T>::operator+=(const VecXd<T> &rhs)
{
if (dimension != rhs.getDimension())
{
cout << "\nCannot perform addition. Vectors do not have the same dimensions.\n";
throw 1;
}
else
{
for (ULL i = 0; i < dimension; ++i)
vector[i] += rhs[i];
}
return *this;
}
template <class T>
const VecXd<T> VecXd<T>::operator+(const VecXd<T> &rhs) const
{
VecXd<T> temp = *this;
temp += rhs;
return temp;
}
template <class T>
ostream& operator<<(ostream &outs, VecXd<T> vec)
{
for (ULL i = 0; i < vec.dimension; ++i)
out << vec.vector[i] << (i+1 < vec.dimension ? " " : "");
return out;
}
template <class T>
istream& operator>>(istream &in, VecXd<T> &vec)
{
ULL newDim = 1;
cin >> newDim;
if (!cin.good())
{
cout << "\nImproper input.\n";
throw 1;
}
else
{
delete [] vec.vector;
vec.dimension = newDim;
vec.vector = new T[vec.dimension];
for (ULL i = 0; i < vec.dimension; ++i)
in >> vec.vector[i];
}
return in;
}
#endif /* defined(__VecXd__VecXd__) */
I'm getting errors of this style:
Member function 'getDimension' not viable: 'this' argument has type 'const VecXd<int>', but function is not marked const
This happens every time rhs calls a function (such as getDimension() or itemAtIndex()); two error appear (once for VecXd<int> and another for VecXd<int>).
Also, the copy constructor is not recognized in the overloaded +operator function in this line:
VecXd<T> temp = *this;
Help?
To be able to call a function on a const object, you need to promise the compiler that the function will not modify the object. To do that, you mark the function with the keyword const after its argument list. For example, to make getDimension a const member function, you would change it to:
const ULL getDimension() const { return dimension; }
(Note that the const in the return type will have absolutely no effect, so you should get rid of it)

Overloading operator[] for a template class- c++

just wrote a code for a template array class (I know its not finished yet), and trying to remember how to overload operators (meanwhile without special difficulties...).
Anyway, while thinking of how to implement operator[] I wondered what would happen if the index is outside the boundaries of the array... I'm pretty sure it is not possible for me to return a NULL (because of the return type), right? and if so, what should I return in case the index is out of boundries?
here's the code, most of it is redundant to my question, but it might help anyone who google's operators overloading so I post the complete code...
#ifndef __MYARRAY_H
#define __MYARRAY_H
#include <iostream>
using namespace std;
template <class T>
class MyArray
{
int phisicalSize, logicalSize;
char printDelimiter;
T* arr;
public:
MyArray(int size=10, char printDelimiter=' ');
MyArray(const MyArray& other);
~MyArray() {delete []arr;}
const MyArray& operator=(const MyArray& other);
const MyArray& operator+=(const T& newVal);
T& operator[](int index);
friend ostream& operator<<(ostream& os, const MyArray& ma)
{
for(int i=0; i<ma.logicalSize; i++)
os << ma.arr[i] << ma.printDelimiter;
return os;
}
};
template <class T>
T& MyArray<T>::operator[]( int index )
{
if (index < 0 || index > logicalSize)
{
//do what???
}
return arr[index];
}
template <class T>
const MyArray<T>& MyArray<T>::operator+=( const T& newVal )
{
if (logicalSize < phisicalSize)
{
arr[logicalSize] = newVal;
logicalSize++;
}
return *this;
}
template <class T>
const MyArray<T>& MyArray<T>::operator=( const MyArray<T>& other )
{
if (this != &other)
{
delete []arr;
phisicalSize = other.phisicalSize;
logicalSize = other.logicalSize;
printDelimiter = other.printDelimiter;
arr = new T[phisicalSize];
for(int i=0; i<logicalSize; i++)
arr[i] = other.arr[i];
}
return *this;
}
template <class T>
MyArray<T>::MyArray( const MyArray& other ) : arr(NULL)
{
*this = other;
}
template <class T>
MyArray<T>::MyArray( int size, char printDelimiter ) : phisicalSize(size), logicalSize(0), printDelimiter(printDelimiter)
{
arr = new T[phisicalSize];
}
#endif
operator[] generally does no bounds checking. Most of the standard containers that can have a range utilize a separate function, at(), which is range checked and throws a std::out_of_range exception.
You likely want to implement a const T& operator[] overload, too.

templated code not working while non-templated code works!

I spent several hours, completely stuck when I realized that only the templated version of my code has a bug.
In the following code, when pushing_back elements in the myMap, the original vectors myVec1 and myVec2 are modified and contain garbage at the end of the execution. If I un-template everything, just replacing template<T> by double, then the code works fine as I would expect (the original arrays are untouched).
The funny thing is if I put a cout in the copy constructor, it does not get called if the code is templated. But it gets called if I replace the copy constructor with Vector<T2> by the original type Vector<T>, and then everything work fine.
Why wouldn't the compiler know that T2==T since I only use double?
(note, the code has been made as short as possible so as to show the errors - I thus removed accessors, made everything public etc.).
#include <vector>
#include <map>
template<class T>
class Vector{
public:
Vector():n(0),data(0){};
Vector(int N):n(N),data(new T[N]){};
Vector(T x, T y):n(2),data(new T[2]){data[0]=x; data[1]=y;};
template<class T2> Vector(const Vector<T2>& rhs):n(rhs.n), data(new T[n])
{
for (int i=0; i<n; i++)
data[i] = T(rhs.data[i]);
}
~Vector(){delete[] data;}
Vector& operator=(const Vector& rhs)
{
if (rhs.n != n)
{
if (data)
delete[] data;
data = new T[rhs.n];
}
n = rhs.n;
memcpy(data, rhs.data, n*sizeof(T));
return *this;
}
T& operator[](int i){return data[i];}
const T& operator[](int i) const {return data[i];}
int n;
T* data;
};
typedef Vector<double> Vectord;
template <class T> inline bool operator<(const Vector<T>& v1, const Vector<T>& v2)
{
for (int i=0; i<v1.n; i++)
{
if (v1[i]<v2[i]) return true;
if (v1[i]>v2[i]) return false;
}
return false;
}
int main(int argc, char** argv)
{
std::vector<Vectord> myVec1(3);
myVec1[0] = Vectord(1.,3.);
myVec1[1] = Vectord(3.,3.);
myVec1[2] = Vectord(1.,5.);
std::vector<Vectord> myVec2(3);
myVec2[0] = Vectord(4.,1.);
myVec2[1] = Vectord(2.,5.);
myVec2[2] = Vectord(6.,5.);
std::map<Vectord, std::vector<Vectord> > myMap;
for (int i=0; i<3; i++)
{
myMap[myVec1[i]].push_back(myVec2[i]);
}
return 0;
}
A templated constructor is never a copy constructor.
So your class is using the automatically generated copy constructor.
Cheers & hth.,