dimanche 12 avril 2015

Passing by reference ends in error


Vote count:

0




I am encountering a problem that I am sure is small but when I try to run my program it states - push_back(T &):cannot convert argument 1 from 'Savings*' (or Checkings*) to 'Account *&. It works if I remove & from the push_back parameter but I don't understand what the difference is when you try to pass by reference rather than copy. Shouldn't it work the same? I have attached the code where I the error begins in the source.cpp file and the push_back function in MyVector.h. Thanks for any help in advance.


Source.cpp



MyVector<Account*> acc;

acc.push_back(new Savings(new Person("Bilbo Baggins", "43 Bag End"), 1, 500, 0.075));
acc.push_back(new Checkings(new Person("Wizard Gandalf", "Crystal Palace"), 2, 1000.00, 2.00));
acc.push_back(new Savings(new Person("Elf Elrond", "Rivendell"), 3, 1200, 0.050));


MyVector.h



template<class T>
void MyVector<T>::push_back(T& n)
{
if (vCapacity == 0)
{
vCapacity++;
T* tmp = new T[vCapacity];
delete[] vArray;
vArray = tmp;
}

if (vSize >= vCapacity)
{
grow();
}
vArray[vSize] = n;
vSize++;
}


asked 1 min ago







Passing by reference ends in error

Aucun commentaire:

Enregistrer un commentaire