先看代码
#include "pch.h"#include#include using namespace std;template class MyArray {public: MyArray(int capacity); MyArray(const MyArray & arr); ~MyArray(); elemType& operator[](int index); MyArray operator=(const MyArray & arr); void PushBack(elemType& data); int GetSize() const { return this->mSize; } //void PushBack(T&& data);private: int mCapacity; int mSize; elemType *pAddr;};template MyArray ::MyArray(int capacity){ this->mCapacity = capacity; this->mSize = 0; this->pAddr = new elemType[this->mCapacity];}template elemType& MyArray ::operator[](int index){ if (index > this->mCapacity || index < 0) { //异常 } else { return *(this->pAddr + index); }}template void MyArray ::PushBack(elemType& data){ if (this->mSize >= this->mCapacity) { //异常 return; } else { *(this->pAddr + this->mSize) = data; this->mSize++; }}template MyArray ::MyArray(const MyArray & arr){ this->mCapacity = arr.mCapacity; this->mSize = arr.mSize; //申请内存空间 this->pAddr = new elemType[this->mCapacity]; //数据拷贝 for (int ix = 0; ix < this->mSize; ++ix) { this->pAddr[ix] = arr.pAddr[ix]; }}template MyArray ::~MyArray(){ if (this->pAddr != NULL) { delete[] this->pAddr; }}template MyArray MyArray ::operator=(const MyArray & arr){ if (this->pAddr != NULL) { delete[] this->pAddr; } this->mCapacity = arr.mCapacity; this->mSize = arr.mSize; //申请内存空间 this->pAddr = new elemType[this->mCapacity]; //数据拷贝 for (int ix = 0; ix < this->mSize; ++ix) { this->pAddr[ix] = arr.pAddr[ix]; } return *this;}void test01(){ MyArray marray(20); int a = 10; int b = 20; int c = 30; int d = 40; marray.PushBack(a); marray.PushBack(b); marray.PushBack(c); marray.PushBack(d); marray.PushBack(100); marray.PushBack(200); for (int ix = 0; ix < marray.GetSize(); ++ix) { cout << marray[ix] << " "; }}int main(){ test01(); return 0;}
代码模拟了STL中的array容器,编译代码,报错
报错的代码为marray.PushBack(100); marray.PushBack(200);
PushBack()的实现如下
templatevoid MyArray ::PushBack(elemType& data){ if (this->mSize >= this->mCapacity) { //异常 return; } else { *(this->pAddr + this->mSize) = data; this->mSize++; }}
其参数为引用,不能对右值取引用,也就是说
int i = &42;
这行代码是错误的。
//不能对右值取引用 //左值 可以在多行使用 //右值 即临时变量,只能在当前行使用 marray.PushBack(100); marray.PushBack(200);
解决办法:重载PushBack()函数
templatevoid MyArray ::PushBack(elemType && data){ if (this->mSize >= this->mCapacity) { //异常 return; } else { *(this->pAddr + this->mSize) = data; this->mSize++; }}
另:
在VS2017开发环境中,将PushBack()的函数实现如下void PushBack(const elemType& data); //类内声明template//类外实现void MyArray ::PushBack(const elemType& data){ if (this->mSize >= this->mCapacity) { //异常 return; } else { *(this->pAddr + this->mSize) = data; this->mSize++; }}
这样在使用PushBack()时,编译不会报错
marray.PushBack(100); marray.PushBack(200);
但在Linux下,gcc版本为4.4.6,即便是写为
void PushBack(const elemType& data); //类内声明
编译器仍旧会报错。