引用传递以错误结束

Passing by reference ends in error

我遇到了一个我确定很小的问题,但是当我尝试 运行 我的程序时,它指出 - push_back(T &):cannot convert argument 1 from 'Savings*' (or Checkings*) to 'Account *&

如果我从 push_back 参数中删除 & 它会起作用,但我不明白当您尝试通过引用传递而不是复制时有什么区别。它不应该一样工作吗?
我附上了 source.cpp 文件中错误开始的代码和 MyVector.h 中的 push_back 函数。

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++;
}

假设 SavingsCheckings 派生自 Accounts,通过引用传递不起作用,因为您不能将临时引用绑定到非常量引用。

将签名更改为

template<class T>
void MyVector<T>::push_back(const T& n)

按值传递有效,因为您有效地复制了参数。