鱼C论坛

 找回密码
 立即注册
查看: 1851|回复: 2

小白求助 关于返回引用当左值的问题

[复制链接]
发表于 2020-5-21 10:54:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
测试的函数
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include "iostream"
  3. #include "MyVector.cpp"
  4. using std::endl;
  5. using std::cout;
  6. using std::cin;

  7. class Teacher
  8. {
  9. private:
  10.         int age;
  11.         char name[32];
  12. public:
  13.         Teacher()
  14.         {
  15.                 this->age = 33;
  16.                 strcpy(this->name, " ");
  17.         }
  18. public:
  19.         Teacher(const char* name, int age)
  20.         {
  21.                 this->age = age;
  22.                 strcpy(this->name, name);
  23.         }
  24.         void printT()
  25.         {
  26.                 cout << this->age << " " << this->name << endl;
  27.         }
  28. };

  29. void main()
  30. {
  31.         //MyVector<T>::MyVector<T>(int len)
  32.         MyVector<Teacher> tArray(4);
  33.         Teacher t1("t1", 22), t2("t2", 32), t3("t3", 42), t4("t4", 52);
  34.         tArray[0] = t1;
  35.         tArray[1] = t2;
  36.         tArray[2] = t3;
  37.         tArray[3] = t4;
  38.         for (int i = 0; i < 4; i++)
  39.         {
  40.                 Teacher tmp = tArray[i];
  41.                 tmp.printT();
  42.         }


  43.         system("pause");
  44. }
复制代码

  1. #include "MyVector.h"

  2. template <typename T>
  3. int MyVector<T>::getLen()
  4. {
  5.         return this->m_len;
  6. }

  7. //MyVector<Teacher> tArray(4);
  8. template <typename T>
  9. MyVector<T>::MyVector<T>(int len)
  10. {
  11.         this->m_len = len;
  12.         this->m_space = new T[m_len];
  13.         //this->m_space = new Teacher[m_len];
  14. }

  15. template <typename T>
  16. MyVector<T>::MyVector<T>(const MyVector<T>& mv)
  17. {

  18. }

  19. template <typename T>
  20. MyVector<T>::~MyVector<T>()
  21. {

  22. }

  23. template <typename T>
  24. MyVector<T>& MyVector<T>::operator=(MyVector& mv)
  25. {

  26. }

  27. template <typename T>
  28. T& MyVector<T>::operator[](int index)
  29. {
  30.         return this->m_space[index];
  31. }

  32. template <typename T>
  33. ostream& operator<<<T>(ostream& out, MyVector<T>& mv)
  34. {


  35. }
复制代码

  1. #pragma once
  2. #include "iostream";
  3. using std::endl;
  4. using std::cout;
  5. using std::cin;
  6. using std::ostream;

  7. template <typename T>
  8. class MyVector;

  9. template <typename T>
  10. ostream& operator<<(ostream& out, MyVector<T>& mv);

  11. template <typename T>
  12. class MyVector
  13. {
  14. private:
  15.         int m_len;
  16.         T* m_space;
  17. public:
  18.         int getLen();
  19. public:
  20.         MyVector(int len);
  21.         MyVector(const MyVector& mv);
  22.         ~MyVector();
  23. public:
  24.         MyVector& operator=(MyVector& mv);
  25.         T& operator[](int index);

  26. public:
  27.         friend ostream& operator<<<T>(ostream& out, MyVector<T>& mv);
  28. };
复制代码


这是三个类 测试类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];
}
问题1.PNG
问题2.PNG
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-5-21 11:09:10 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-5-21 11:19:59 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-1 11:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表