鱼C论坛

 找回密码
 立即注册
查看: 1459|回复: 10

[已解决]没有找到接受“SeqList<int>”类型的右操作数的运算符(或没有可接受的转换)

[复制链接]
发表于 2021-11-28 16:45:03 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 小易zjy 于 2021-11-28 19:30 编辑

等号处报错,但我觉着赋值运算符重载没问题啊,两边都是同样类型的,有大佬知道原因嘛(main.cpp的55行报错
  1. L = mul(list1, list2);
复制代码

//Seqlist.h
  1. #pragma once
  2. #ifndef SEQLIST_H
  3. #define SEQLIST_H

  4. #include <iostream>
  5. #include <cstdlib>
  6. #include <cassert>
  7. #include<string>
  8. using namespace std;

  9. const int defaultSize = 100;

  10. template <typename T>class SeqList {
  11.         friend string multiply(string num1, string num2);
  12.         friend        SeqList<T> mul(const SeqList<T> & list1,const SeqList<T> &list2);
  13. protected:
  14.         T* data;//存放数组
  15.         int maxSize;//最大可容纳表项的项数
  16.         int last;//当前已存表项的最后位置(从0开始)
  17. public:

  18.         SeqList(int sz = defaultSize);//构造函数
  19.         SeqList(SeqList<T>& L);//复制构造函数
  20.         ~SeqList() {
  21.                 delete[]data;
  22.         }
  23.         void reSize(int newSize);
  24.         int Size() const {
  25.                 return maxSize;
  26.         }
  27.         int Length()const {
  28.                 return last + 1;
  29.         }
  30.         int Search(T& x) const;
  31.         int Locate(int i) const;
  32.         bool getData(int i, T& x) const {//改
  33.                 if (i > 0 && i <= last + 1) {
  34.                         x = data[i - 1];
  35.                         return true;
  36.                 }
  37.                 else return false;
  38.         }

  39.         void setData(int i, T& x) {
  40.                 if (i > 0 && i <= last + 1) {
  41.                         data[i - 1] = x;
  42.                 }
  43.         }
  44.         bool Insert(int i, T& x);
  45.         bool Remove(int i, T& x);
  46.         bool IsEmpty() {
  47.                 return (last == -1);
  48.         }
  49.         bool IsFull() {
  50.                 return (last == maxSize - 1);
  51.         }
  52.         void Sort();
  53.         void input();
  54.         void output();
  55.         SeqList<T> operator = (SeqList<T>& L);
  56.         friend istream& operator >> (istream& in, SeqList<T>& R) {
  57.                 R.last = -1;//先清空
  58.                 while (!in.eof()) {
  59.                         R.last++;
  60.                         if (R.last == R.maxSize) R.reSize(2 * R.maxSize);
  61.                         assert(in >> R.data[R.last]);
  62.                 }
  63.                 return in;
  64.         }
  65.         friend ostream& operator << (ostream& out, SeqList<T>& R) {
  66.                 for (int i = 0; i <= R.last; i++) {
  67.                         cout << "#" << i + 1 << ":\t" << R.data[i] << endl;
  68.                 }
  69.                 return out;
  70.         }
  71. };

  72. template <typename T>SeqList<T>::SeqList(int sz) {
  73.         if (sz > 0) {
  74.                 maxSize = sz;
  75.                 last = -1;
  76.                 data = new T[maxSize];
  77.                 if (data == NULL) {
  78.                         cerr << "Memory allocating error!" << endl;
  79.                         exit(1);
  80.                 }
  81.         }
  82. }

  83. template <typename T>SeqList<T>::SeqList(SeqList<T>& L) {
  84.         maxSize = L.Size();
  85.         last = L.Length() - 1;
  86.         T value;
  87.         data = new T[maxSize];
  88.         if (data == NULL)
  89.         {
  90.                 cerr << "Memory allocating error!" << endl;
  91.                 exit(1);
  92.         }
  93.         for (int i = 1; i <= last + 1; i++)
  94.         {
  95.                 L.getData(i, value); data[i - 1] = value;
  96.         };
  97. }

  98. template<typename T>void SeqList<T>::reSize(int newSize) {
  99.         if (newSize <= 0) {
  100.                 cerr << "Invalid array index!" << endl;
  101.                 return;
  102.         }
  103.         if (newSize != maxSize) {
  104.                 T* newarray = new T[newSize];//建立新数组
  105.                 if (newarray == NULL) {
  106.                         cerr << "Memory allocating error!" << endl;
  107.                         exit(1);
  108.                 }
  109.                 int n = last + 1;//改
  110.                 T* srcptr = data;//源数组首地址
  111.                 T* destptr = newarray;//目的数组首地址
  112.                 while (n--)        *destptr++ = *srcptr++;//复制
  113.                 delete[]data;//删老数组
  114.                 data = newarray;//复制新数组
  115.                 maxSize = newSize;
  116.         }
  117. }

  118. template<typename T>int SeqList<T>::Search(T& x)const {
  119.         for (int i = 0; i <= last; i++) {
  120.                 if (data[i] == x)        return i + 1;
  121.         }
  122.         return 0;
  123. }

  124. template<typename T>int SeqList<T>::Locate(int i)const {
  125.         if (i >= 1 && i <= last + 1)        return i;
  126.         else        return 0;
  127. }

  128. template<typename T>bool SeqList<T>::Insert(int i, T& x) {
  129.         if (last == maxSize - 1)        return false;
  130.         if (i < 0 || i > last + 1) return false;
  131.         for (int j = last; j >= i; j--)        data[j + 1] = data[j];
  132.         data[i] = x;
  133.         last++;
  134.         return true;
  135. }

  136. template<typename T>bool SeqList<T>::Remove(int i, T& x) {
  137.         if (last == -1)        return false;
  138.         if (i < 1 || i > last + 1) return false;
  139.         x = data[i - 1];
  140.         for (int j = i; j <= last; j++)        data[j - 1] = data[j];
  141.         last--;
  142.         return true;
  143. }

  144. template<typename T>void SeqList<T>::Sort() {
  145.         for (int i = 1; i <= last; i++) {
  146.                 for (int j = last; j >= i; j--) {
  147.                         if (data[j - 1] > data[j]) {
  148.                                 T tmp = data[j - 1];
  149.                                 data[j - 1] = data[j];
  150.                                 data[j] = tmp;
  151.                         }
  152.                 }
  153.         }
  154. }

  155. template<typename T>void SeqList<T>::input() {
  156.         cout << "Input the size of the list which will be created:";
  157.         while (1) {
  158.                 assert(cin >> last);
  159.                 last--;
  160.                 if (last < 0) {
  161.                         cout << "Input error, the size must be positive!\n";
  162.                         cout << "Input the size again:";
  163.                 }
  164.                 else if (last > maxSize - 1) {//改一改可以扩大
  165.                         cout << "Input error, the size must be less than maxSize!\n";
  166.                         cout << "Input the size again:";
  167.                 }
  168.                 else        break;
  169.         }
  170.         cout << "\nInput the data for each element to create the list:" << endl;
  171.         for (int i = 0; i <= last; i++) {
  172.                 cout << "#" << i + 1 << ":";
  173.                 assert(cin >> data[i]);
  174.         }
  175. }

  176. template<typename T>void SeqList<T>::output() {
  177.         cout << "\nThe size of the list is:" << last + 1 << endl;
  178.         for (int i = 0; i <= last; i++)        cout << "#" << i + 1 << ":\t" << data[i] << endl;
  179. }

  180. //SeqList<T> operator = (SeqList<T>& L);
  181. template<typename T>SeqList<T> SeqList<T>::operator = (SeqList<T>& L) {//改
  182.         T value;
  183.         maxSize = L.Size();
  184.         last = L.Length() - 1;
  185.         delete[]data;//先清空
  186.         data = new T[maxSize];
  187.         if (data == NULL) {
  188.                 cerr << "Memory allocating error!" << endl;
  189.                 exit(1);
  190.         }
  191.         for (int i = 1; i <= last + 1; i++)

  192.         {
  193.                 L.getData(i, value);
  194.                 data[i - 1] = value;
  195.         }
  196.         return *this;
  197. }

  198. #endif
复制代码


//main.cpp
  1. #include <iostream>
  2. #include "SeqList.h"
  3. #include<string>
  4. #include <fstream>
  5. #include <cassert>
  6. using namespace std;

  7. template<typename T>
  8. SeqList<T> mul(const SeqList<T>& list1,const SeqList<T>& list2)
  9. {
  10.         SeqList<int> list3(list1.Size() + list2.Size());//初始化结果的最大长度,用list3保存。乘法得到数字的位数一定不超过乘数位数之和
  11.         for(int i=0;i<list1.Size();i++)
  12.                 for(int j=0;j<list2.Size();j++)
  13.                 {
  14.                         int x,y;
  15.                         list1.getData(i, x);
  16.                         list2.getData(j, y);
  17.                         int z=0;
  18.                         z += x * y;
  19.                         list3.setData(i + j, z);
  20.                 }

  21.         int t = 0;
  22.         for (int i = 0; i < list3.Size(); i++)//处理进位
  23.         {
  24.                 int p;
  25.                 list3.getData(i, p);
  26.                 t += p;
  27.                 int m = t % 10;
  28.                 list3.setData(i, m);
  29.                 t /= 10;
  30.         }

  31.         while (list3.Size() > 1 && (list3.last) == 0)//初始时结果的最高位为0,若最终最高位仍为0,去掉最高位的0
  32.         {
  33.                 int q;
  34.                 list3.Remove(list3.last, q);
  35.         }
  36.         return list3;
  37. }

  38. string multiply(string num1, string num2) {
  39.         //num1="123"
  40.         SeqList<int>list1,list2;
  41.         for(int i=num1.size()-1;i>=0;i--)
  42.         {
  43.                 int x = num1[i] - '0';
  44.                 list1.setData(list1.last, x);
  45.         }//list1=[3,2,1] 倒序输出
  46.         for (int i = num2.size() - 1; i >= 0; i--)
  47.         {
  48.                 int y = num2[i] - '0';
  49.                 list2.setData(list2.last, y);
  50.         }
  51.         SeqList<int> L;
  52.         L = mul(list1, list2);
  53.        

  54.         string res = "";
  55.         for (int i = L.Size() - 1; i >= 0; i--)
  56.         {
  57.                 int p;
  58.                 res += to_string(L.getData(i, p));
  59.         }

  60.         return res;
  61. }

  62. int main() {
  63.         SeqList<int>l1, l2, l3;
  64. }
复制代码
最佳答案
2021-11-29 21:03:21
典型的左右值不分问题
operator=()函数签名里写的的是一个SeqList<T>&类型的参数,接收一个左值引用
然而调用时mul()函数返回的是一个临时变量,也就是一个右值,无法进行转换
operator=()重载是有规则的,复制赋值运算符就写const SeqList<T>& ,移动赋值运算符就写SeqList<T>&& ,你这样SeqList<T>&显得不伦不类。
(关于移动赋值运算符和复制赋值运算符的区别,这个讲起来没完没了,建议上百度深入了解一下)
如果不想了解就无脑改成const SeqList<T>&
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-11-28 16:55:12 | 显示全部楼层
1>D:\C++\作业1 顺序表\Project3\源.cpp(55,23): error C2679: 二元“=”: 没有找到接受“SeqList<int>”类型的右操作数的运算符(或没有可接受的转换)
1>D:\C++\作业1 顺序表\Project3\SeqList.h(60,13): message : 可能是“SeqList<int> SeqList<int>::operator =(SeqList<int> &)”
1>D:\C++\作业1 顺序表\Project3\源.cpp(55,23): message : 尝试匹配参数列表“(SeqList<int>, SeqList<int>)”时
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-28 17:01:36 | 显示全部楼层
这是什么?一个字符串减一个字符?

Untitled.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2021-11-28 18:29:41 | 显示全部楼层
本帖最后由 小易zjy 于 2021-11-28 18:30 编辑
人造人 发表于 2021-11-28 17:01
这是什么?一个字符串减一个字符?


我代码复制过来的时候出错了,应该是这样的int y = num2 [ i ] -  '0';
不好意思
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-28 18:38:52 | 显示全部楼层
路过学习
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-28 18:46:03 | 显示全部楼层
少了 operator= 函数
  1. // Seqlist.h
  2. //#pragma once
  3. #ifndef SEQLIST_H
  4. #define SEQLIST_H

  5. #include <cassert>
  6. #include <cstdlib>
  7. #include <iostream>
  8. #include <string>

  9. using namespace std;

  10. const int defaultSize = 100;

  11. template <typename T> class SeqList {
  12.     friend string multiply(string num1, string num2);
  13.     friend SeqList<T> mul(const SeqList<T> &list1, const SeqList<T> &list2);

  14.   protected:
  15.     T *data;     //存放数组
  16.     int maxSize; //最大可容纳表项的项数
  17.     int last;    //当前已存表项的最后位置(从0开始)
  18.   public:


  19.     SeqList<T> &operator=(const SeqList<T> &rhs) {
  20.         // ...
  21.         return *this;
  22.     }




  23.     SeqList(int sz = defaultSize); //构造函数
  24.     SeqList(SeqList<T> &L);        //复制构造函数
  25.     ~SeqList() { delete[] data; }
  26.     void reSize(int newSize);
  27.     int Size() const { return maxSize; }
  28.     int Length() const { return last + 1; }
  29.     int Search(T &x) const;
  30.     int Locate(int i) const;
  31.     bool getData(int i, T &x) const { //改
  32.         if(i > 0 && i <= last + 1) {
  33.             x = data[i - 1];
  34.             return true;
  35.         } else
  36.             return false;
  37.     }

  38.     void setData(int i, T &x) {
  39.         if(i > 0 && i <= last + 1) {
  40.             data[i - 1] = x;
  41.         }
  42.     }
  43.     bool Insert(int i, T &x);
  44.     bool Remove(int i, T &x);
  45.     bool IsEmpty() { return (last == -1); }
  46.     bool IsFull() { return (last == maxSize - 1); }
  47.     void Sort();
  48.     void input();
  49.     void output();
  50.     SeqList<T> operator=(SeqList<T> &L);
  51.     friend istream &operator>>(istream &in, SeqList<T> &R) {
  52.         R.last = -1; //先清空
  53.         while(!in.eof()) {
  54.             R.last++;
  55.             if(R.last == R.maxSize)
  56.                 R.reSize(2 * R.maxSize);
  57.             assert(in >> R.data[R.last]);
  58.         }
  59.         return in;
  60.     }
  61.     friend ostream &operator<<(ostream &out, SeqList<T> &R) {
  62.         for(int i = 0; i <= R.last; i++) {
  63.             cout << "#" << i + 1 << ":\t" << R.data << endl;
  64.         }
  65.         return out;
  66.     }
  67. };

  68. template <typename T> SeqList<T>::SeqList(int sz) {
  69.     if(sz > 0) {
  70.         maxSize = sz;
  71.         last = -1;
  72.         data = new T[maxSize];
  73.         if(data == NULL) {
  74.             cerr << "Memory allocating error!" << endl;
  75.             exit(1);
  76.         }
  77.     }
  78. }

  79. template <typename T> SeqList<T>::SeqList(SeqList<T> &L) {
  80.     maxSize = L.Size();
  81.     last = L.Length() - 1;
  82.     T value;
  83.     data = new T[maxSize];
  84.     if(data == NULL) {
  85.         cerr << "Memory allocating error!" << endl;
  86.         exit(1);
  87.     }
  88.     for(int i = 1; i <= last + 1; i++) {
  89.         L.getData(i, value);
  90.         data[i - 1] = value;
  91.     };
  92. }

  93. template <typename T> void SeqList<T>::reSize(int newSize) {
  94.     if(newSize <= 0) {
  95.         cerr << "Invalid array index!" << endl;
  96.         return;
  97.     }
  98.     if(newSize != maxSize) {
  99.         T *newarray = new T[newSize]; //建立新数组
  100.         if(newarray == NULL) {
  101.             cerr << "Memory allocating error!" << endl;
  102.             exit(1);
  103.         }
  104.         int n = last + 1;      //改
  105.         T *srcptr = data;      //源数组首地址
  106.         T *destptr = newarray; //目的数组首地址
  107.         while(n--)
  108.             *destptr++ = *srcptr++; //复制
  109.         delete[] data;              //删老数组
  110.         data = newarray;            //复制新数组
  111.         maxSize = newSize;
  112.     }
  113. }

  114. template <typename T> int SeqList<T>::Search(T &x) const {
  115.     for(int i = 0; i <= last; i++) {
  116.         if(data == x)
  117.             return i + 1;
  118.     }
  119.     return 0;
  120. }

  121. template <typename T> int SeqList<T>::Locate(int i) const {
  122.     if(i >= 1 && i <= last + 1)
  123.         return i;
  124.     else
  125.         return 0;
  126. }

  127. template <typename T> bool SeqList<T>::Insert(int i, T &x) {
  128.     if(last == maxSize - 1)
  129.         return false;
  130.     if(i < 0 || i > last + 1)
  131.         return false;
  132.     for(int j = last; j >= i; j--)
  133.         data[j + 1] = data[j];
  134.     data = x;
  135.     last++;
  136.     return true;
  137. }

  138. template <typename T> bool SeqList<T>::Remove(int i, T &x) {
  139.     if(last == -1)
  140.         return false;
  141.     if(i < 1 || i > last + 1)
  142.         return false;
  143.     x = data[i - 1];
  144.     for(int j = i; j <= last; j++)
  145.         data[j - 1] = data[j];
  146.     last--;
  147.     return true;
  148. }

  149. template <typename T> void SeqList<T>::Sort() {
  150.     for(int i = 1; i <= last; i++) {
  151.         for(int j = last; j >= i; j--) {
  152.             if(data[j - 1] > data[j]) {
  153.                 T tmp = data[j - 1];
  154.                 data[j - 1] = data[j];
  155.                 data[j] = tmp;
  156.             }
  157.         }
  158.     }
  159. }

  160. template <typename T> void SeqList<T>::input() {
  161.     cout << "Input the size of the list which will be created:";
  162.     while(1) {
  163.         assert(cin >> last);
  164.         last--;
  165.         if(last < 0) {
  166.             cout << "Input error, the size must be positive!\n";
  167.             cout << "Input the size again:";
  168.         } else if(last > maxSize - 1) { //改一改可以扩大
  169.             cout << "Input error, the size must be less than maxSize!\n";
  170.             cout << "Input the size again:";
  171.         } else
  172.             break;
  173.     }
  174.     cout << "\nInput the data for each element to create the list:" << endl;
  175.     for(int i = 0; i <= last; i++) {
  176.         cout << "#" << i + 1 << ":";
  177.         assert(cin >> data);
  178.     }
  179. }

  180. template <typename T> void SeqList<T>::output() {
  181.     cout << "\nThe size of the list is:" << last + 1 << endl;
  182.     for(int i = 0; i <= last; i++)
  183.         cout << "#" << i + 1 << ":\t" << data << endl;
  184. }

  185. // SeqList<T> operator = (SeqList<T>& L);
  186. template <typename T> SeqList<T> SeqList<T>::operator=(SeqList<T> &L) {
  187.     T value;
  188.     maxSize = L.Size();
  189.     last = L.Length() - 1;
  190.     delete[] data; //先清空
  191.     data = new T[maxSize];
  192.     if(data == NULL) {
  193.         cerr << "Memory allocating error!" << endl;
  194.         exit(1);
  195.     }
  196.     for(int i = 1; i <= last + 1; i++)

  197.     {
  198.         L.getData(i, value);
  199.         data[i - 1] = value;
  200.     }
  201.     return *this;
  202. }

  203. #endif

  204. // main.cpp
  205. #include <iostream>
  206. //#include "SeqList.h"
  207. #include <cassert>
  208. #include <fstream>
  209. #include <string>
  210. using namespace std;

  211. template <typename T>
  212. SeqList<T> mul(const SeqList<T> &list1, const SeqList<T> &list2) {
  213.     SeqList<int> list3(list1.Size() + list2.Size());
  214.     for(int i = 0; i < list1.Size(); i++)
  215.         for(int j = 0; j < list2.Size(); j++) {
  216.             int x, y;
  217.             list1.getData(i, x);
  218.             list2.getData(j, y);
  219.             int z = 0;
  220.             z += x * y;
  221.             list3.setData(i + j, z);
  222.         }

  223.     int t = 0;
  224.     for(int i = 0; i < list3.Size(); i++) //处理进位
  225.     {
  226.         int p;
  227.         list3.getData(i, p);
  228.         t += p;
  229.         int m = t % 10;
  230.         list3.setData(i, m);
  231.         t /= 10;
  232.     }

  233.     while(list3.Size() > 1 && (list3.last) == 0) //去掉最高位的0
  234.     {
  235.         int q;
  236.         list3.Remove(list3.last, q);
  237.     }
  238.     return list3;
  239. }

  240. string multiply(string num1, string num2) {
  241.     SeqList<int> list1, list2;
  242.     for(int i = num1.size() - 1; i >= 0; i--) {
  243.         int x = num1[i] - '0';
  244.         list1.setData(list1.last, x);
  245.     }
  246.     for(int i = num2.size() - 1; i >= 0; i--) {
  247.         int y = num2[i] - '0';
  248.         list2.setData(list2.last, y);
  249.     }
  250.     SeqList<int> L;
  251.     L = mul(list1, list2); //这一行报错了

  252.     string res = "";
  253.     for(int i = L.Size() - 1; i >= 0; i--) {
  254.         int p;
  255.         res += to_string(L.getData(i, p));
  256.     }

  257.     return res;
  258. }

  259. int main() {
  260.     SeqList<int> l1, l2, l3;
  261.     return 0;
  262. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2021-11-28 18:54:51 | 显示全部楼层
人造人 发表于 2021-11-28 18:46
少了 operator= 函数

第209行开始的不是operator=嘛
还有我发现好多中括号直接复制过来格式都变了,现在都已经改过来了
之前没检查 抱歉抱歉
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-28 19:01:57 | 显示全部楼层
小易zjy 发表于 2021-11-28 18:54
第209行开始的不是operator=嘛
还有我发现好多中括号直接复制过来格式都变了,现在都已经改过 ...

你这代码写的,我有点看不懂,我找时间研究研究
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-11-28 19:06:52 | 显示全部楼层
人造人 发表于 2021-11-28 19:01
你这代码写的,我有点看不懂,我找时间研究研究

好滴好滴 感谢!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-28 20:49:17 | 显示全部楼层

改了一部分,剩下的函数不知道做什么用的,没法改了

  1. // Seqlist.h
  2. //#pragma once
  3. #ifndef SEQLIST_H
  4. #define SEQLIST_H

  5. #include <cassert>
  6. #include <cstdlib>
  7. #include <iostream>
  8. #include <string>
  9. #include <cstring>

  10. using namespace std;

  11. const int defaultSize = 100;

  12. template <typename T>
  13. class SeqList {
  14. protected:
  15.     T *data;
  16.     size_t maxSize;
  17.     size_t size;

  18.     void resize() {
  19.         size_t size = 2 * this->maxSize;
  20.         T *temp = new T[size];
  21.         memcpy(temp, this->data, this->size * sizeof(T));
  22.         delete[] this->data;
  23.         this->data = temp;
  24.         this->maxSize = size;
  25.     }
  26. public:
  27.     SeqList(size_t sz) {
  28.         this->maxSize = sz;
  29.         this->data = new T[sz];
  30.         this->size = 0;
  31.     }
  32.     SeqList(const SeqList<T> &L) {
  33.         this->data = nullptr;
  34.         this->operator=(L);
  35.     }
  36.     ~SeqList() {delete[] this->data;}
  37.     bool Insert(size_t i, const T &x) {
  38.         if(i > this->Length()) return false;
  39.         ++this->size;
  40.         if(this->size > this->maxSize) {
  41.             this->resize();
  42.         }
  43.         for(size_t j = this->size - 1; j != i; --j) {
  44.             this->data[j] = this->data[j - 1];
  45.         }
  46.         this->data[i] = x;
  47.         return true;
  48.     }
  49.     bool Remove(size_t i) {
  50.         if(i >= this->Length()) return false;
  51.         for(size_t j = i; j < this->size - 1; ++j) {
  52.             this->data[j] = this->data[j + 1];
  53.         }
  54.         --this->size;
  55.         return true;
  56.     }
  57.     int Length() const {return this->size;}
  58.     bool getData(size_t i, T &x) const {
  59.         if(i >= this->Length()) return false;
  60.         x = this->data[i];
  61.         return true;
  62.     }
  63.     bool setData(size_t i, const T &x) {
  64.         if(i >= this->Length()) return false;
  65.         this->data[i] = x;
  66.         return true;
  67.     }
  68.     bool IsEmpty() {return this->Length() == 0;}
  69.     SeqList<T> operator=(const SeqList<T> &L) {
  70.         delete[] this->data;
  71.         this->maxSize = L.maxSize;
  72.         this->data = new T[L.maxSize];
  73.         this->size = L.size;
  74.         memcpy(this->data, L.data, L.size * sizeof(T));
  75.         return *this;
  76.     }
  77.     void clean() {this->size = 0;}
  78.     bool append(const T &value) {
  79.         return this->Insert(this->Length(), value);
  80.     }
  81.     friend istream &operator>>(istream &in, SeqList<T> &R) {
  82.         R.clean();
  83.         T value;
  84.         while(in >> value) {
  85.             R.append(value);
  86.         }
  87.         return in;
  88.     }
  89.     friend ostream &operator<<(ostream &out, const SeqList<T> &R) {
  90.         for(int i = 0; i < R.Length(); i++) {
  91.             T value; R.getData(i, value);
  92.             cout << "#" << i + 1 << ":\t" << value << endl;
  93.         }
  94.         return out;
  95.     }
  96.     int Search(const T &x) const {
  97.         for(size_t i = 0; i < this->Length(); i++) {
  98.             T value; this->getData(i, value);
  99.             if(value == x) return i;
  100.         }
  101.         return -1;
  102.     }
  103.     void Sort() {
  104.         for(size_t i = 0; i < this->Length(); ++i) {
  105.             for(size_t j = i + 1; j < this->Length(); ++j) {
  106.                 T a, b;
  107.                 this->getData(i, a);
  108.                 this->getData(j, b);
  109.                 if(a > b) {
  110.                     this->setData(i, b);
  111.                     this->setData(j, a);
  112.                 }
  113.             }
  114.         }
  115.     }
  116. };

  117. // 这个函数做什么用的?
  118. // 判断索引i是否存在?
  119. // 你直接调用Length函数不就可以了?
  120. /*
  121. template <typename T> int SeqList<T>::Locate(int i) const {
  122.     if(i >= 1 && i <= last + 1)
  123.         return i;
  124.     else
  125.         return 0;
  126. }
  127. */

  128. // 下面这两个函数做什么用的?
  129. /*
  130. template <typename T> void SeqList<T>::input() {
  131.     cout << "Input the size of the list which will be created:";
  132.     while(1) {
  133.         assert(cin >> last);
  134.         last--;
  135.         if(last < 0) {
  136.             cout << "Input error, the size must be positive!\n";
  137.             cout << "Input the size again:";
  138.         } else if(last > maxSize - 1) { //改一改可以扩大
  139.             cout << "Input error, the size must be less than maxSize!\n";
  140.             cout << "Input the size again:";
  141.         } else
  142.             break;
  143.     }
  144.     cout << "\nInput the data for each element to create the list:" << endl;
  145.     for(int i = 0; i <= last; i++) {
  146.         cout << "#" << i + 1 << ":";
  147.         assert(cin >> data);
  148.     }
  149. }

  150. template <typename T> void SeqList<T>::output() {
  151.     cout << "\nThe size of the list is:" << last + 1 << endl;
  152.     for(int i = 0; i <= last; i++)
  153.         cout << "#" << i + 1 << ":\t" << data << endl;
  154. }
  155. */

  156. #endif

  157. // main.cpp
  158. #include <iostream>
  159. //#include "SeqList.h"
  160. #include <cassert>
  161. #include <fstream>
  162. #include <string>

  163. using namespace std;

  164. template <typename T>
  165. SeqList<T> mul(const SeqList<T> &list1, const SeqList<T> &list2) {
  166.     SeqList<int> list3(list1.Size() + list2.Size());
  167.     for(int i = 0; i < list1.Size(); i++)
  168.         for(int j = 0; j < list2.Size(); j++) {
  169.             int x, y;
  170.             list1.getData(i, x);
  171.             list2.getData(j, y);
  172.             int z = 0;
  173.             z += x * y;
  174.             list3.setData(i + j, z);
  175.         }

  176.     int t = 0;
  177.     for(int i = 0; i < list3.Size(); i++) //处理进位
  178.     {
  179.         int p;
  180.         list3.getData(i, p);
  181.         t += p;
  182.         int m = t % 10;
  183.         list3.setData(i, m);
  184.         t /= 10;
  185.     }

  186.     while(list3.Size() > 1 && (list3.last) == 0) //去掉最高位的0
  187.     {
  188.         int q;
  189.         list3.Remove(list3.last, q);
  190.     }
  191.     return list3;
  192. }

  193. string multiply(string num1, string num2) {
  194.     SeqList<int> list1, list2;
  195.     for(int i = num1.size() - 1; i >= 0; i--) {
  196.         int x = num1[i] - '0';
  197.         list1.setData(list1.last, x);
  198.     }
  199.     for(int i = num2.size() - 1; i >= 0; i--) {
  200.         int y = num2[i] - '0';
  201.         list2.setData(list2.last, y);
  202.     }
  203.     SeqList<int> L;
  204.     L = mul(list1, list2); //这一行报错了

  205.     string res = "";
  206.     for(int i = L.Size() - 1; i >= 0; i--) {
  207.         int p;
  208.         res += to_string(L.getData(i, p));
  209.     }

  210.     return res;
  211. }

  212. int main() {
  213.     SeqList<int> l1, l2, l3;
  214.     return 0;
  215. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-29 21:03:21 | 显示全部楼层    本楼为最佳答案   
典型的左右值不分问题
operator=()函数签名里写的的是一个SeqList<T>&类型的参数,接收一个左值引用
然而调用时mul()函数返回的是一个临时变量,也就是一个右值,无法进行转换
operator=()重载是有规则的,复制赋值运算符就写const SeqList<T>& ,移动赋值运算符就写SeqList<T>&& ,你这样SeqList<T>&显得不伦不类。
(关于移动赋值运算符和复制赋值运算符的区别,这个讲起来没完没了,建议上百度深入了解一下)
如果不想了解就无脑改成const SeqList<T>&
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-25 13:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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