马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
测试的函数#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[32];
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[0] = t1;
tArray[1] = t2;
tArray[2] = t3;
tArray[3] = t4;
for (int i = 0; i < 4; i++)
{
Teacher tmp = tArray[i];
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[m_len];
//this->m_space = new Teacher[m_len];
}
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[index];
}
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[0] = t1;
tArray[1] = t2;
tArray[2] = t3;
tArray[3] = t4;
显示报错 而在MyVector类中 写了下面的代码 就不报错了 这是当左值吗?左值返回的是引用 谁是左值?是这个返回的this->m_space[index]当左值吗?
template <typename T>
T& MyVector<T>::operator[](int index)
{
return this->m_space[index];
} |