小白求助 关于返回引用当左值的问题
测试的函数#define _CRT_SECURE_NO_WARNINGS
#include "iostream"
#include "MyVector.cpp"
using std::endl;
using std::cout;
using std::cin;
class Teacher
{
private:
int age;
char name;
public:
Teacher()
{
this->age = 33;
strcpy(this->name, " ");
}
public:
Teacher(const char* name, int age)
{
this->age = age;
strcpy(this->name, name);
}
void printT()
{
cout << this->age << " " << this->name << endl;
}
};
void main()
{
//MyVector<T>::MyVector<T>(int len)
MyVector<Teacher> tArray(4);
Teacher t1("t1", 22), t2("t2", 32), t3("t3", 42), t4("t4", 52);
tArray = t1;
tArray = t2;
tArray = t3;
tArray = t4;
for (int i = 0; i < 4; i++)
{
Teacher tmp = tArray;
tmp.printT();
}
system("pause");
}
#include "MyVector.h"
template <typename T>
int MyVector<T>::getLen()
{
return this->m_len;
}
//MyVector<Teacher> tArray(4);
template <typename T>
MyVector<T>::MyVector<T>(int len)
{
this->m_len = len;
this->m_space = new T;
//this->m_space = new Teacher;
}
template <typename T>
MyVector<T>::MyVector<T>(const MyVector<T>& mv)
{
}
template <typename T>
MyVector<T>::~MyVector<T>()
{
}
template <typename T>
MyVector<T>& MyVector<T>::operator=(MyVector& mv)
{
}
template <typename T>
T& MyVector<T>::operator[](int index)
{
return this->m_space;
}
template <typename T>
ostream& operator<<<T>(ostream& out, MyVector<T>& mv)
{
}
#pragma once
#include "iostream";
using std::endl;
using std::cout;
using std::cin;
using std::ostream;
template <typename T>
class MyVector;
template <typename T>
ostream& operator<<(ostream& out, MyVector<T>& mv);
template <typename T>
class MyVector
{
private:
int m_len;
T* m_space;
public:
int getLen();
public:
MyVector(int len);
MyVector(const MyVector& mv);
~MyVector();
public:
MyVector& operator=(MyVector& mv);
T& operator[](int index);
public:
friend ostream& operator<<<T>(ostream& out, MyVector<T>& mv);
};
这是三个类 测试类TestVector MyVector.h MyVector.cpp
为啥
tArray = t1;
tArray = t2;
tArray = t3;
tArray = t4;
显示报错而在MyVector类中 写了下面的代码 就不报错了 这是当左值吗?左值返回的是引用谁是左值?是这个返回的this->m_space当左值吗?
template <typename T>
T& MyVector<T>::operator[](int index)
{
return this->m_space;
} 顶 顶
页:
[1]