|  | 
 
| 
本帖最后由 小易zjy 于 2021-11-28 19:30 编辑
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  
 等号处报错,但我觉着赋值运算符重载没问题啊,两边都是同样类型的,有大佬知道原因嘛(main.cpp的55行报错)
 //Seqlist.h
 
 复制代码#pragma once
#ifndef SEQLIST_H
#define SEQLIST_H
#include <iostream>
#include <cstdlib>
#include <cassert>
#include<string>
using namespace std;
const int defaultSize = 100;
template <typename T>class SeqList {
        friend string multiply(string num1, string num2);
        friend        SeqList<T> mul(const SeqList<T> & list1,const SeqList<T> &list2);
protected:
        T* data;//存放数组
        int maxSize;//最大可容纳表项的项数
        int last;//当前已存表项的最后位置(从0开始)
public:
        SeqList(int sz = defaultSize);//构造函数
        SeqList(SeqList<T>& L);//复制构造函数
        ~SeqList() {
                delete[]data;
        }
        void reSize(int newSize);
        int Size() const {
                return maxSize;
        }
        int Length()const {
                return last + 1;
        }
        int Search(T& x) const;
        int Locate(int i) const;
        bool getData(int i, T& x) const {//改
                if (i > 0 && i <= last + 1) {
                        x = data[i - 1];
                        return true;
                }
                else return false;
        }
        void setData(int i, T& x) {
                if (i > 0 && i <= last + 1) {
                        data[i - 1] = x;
                }
        }
        bool Insert(int i, T& x);
        bool Remove(int i, T& x);
        bool IsEmpty() {
                return (last == -1);
        }
        bool IsFull() {
                return (last == maxSize - 1);
        }
        void Sort();
        void input();
        void output();
        SeqList<T> operator = (SeqList<T>& L);
        friend istream& operator >> (istream& in, SeqList<T>& R) {
                R.last = -1;//先清空
                while (!in.eof()) {
                        R.last++;
                        if (R.last == R.maxSize) R.reSize(2 * R.maxSize);
                        assert(in >> R.data[R.last]);
                }
                return in;
        }
        friend ostream& operator << (ostream& out, SeqList<T>& R) {
                for (int i = 0; i <= R.last; i++) {
                        cout << "#" << i + 1 << ":\t" << R.data[i] << endl;
                }
                return out;
        }
};
template <typename T>SeqList<T>::SeqList(int sz) {
        if (sz > 0) {
                maxSize = sz;
                last = -1;
                data = new T[maxSize];
                if (data == NULL) {
                        cerr << "Memory allocating error!" << endl;
                        exit(1);
                }
        }
}
template <typename T>SeqList<T>::SeqList(SeqList<T>& L) {
        maxSize = L.Size();
        last = L.Length() - 1;
        T value;
        data = new T[maxSize];
        if (data == NULL)
        {
                cerr << "Memory allocating error!" << endl;
                exit(1);
        }
        for (int i = 1; i <= last + 1; i++)
        {
                L.getData(i, value); data[i - 1] = value;
        };
}
template<typename T>void SeqList<T>::reSize(int newSize) {
        if (newSize <= 0) {
                cerr << "Invalid array index!" << endl;
                return;
        }
        if (newSize != maxSize) {
                T* newarray = new T[newSize];//建立新数组
                if (newarray == NULL) {
                        cerr << "Memory allocating error!" << endl;
                        exit(1);
                }
                int n = last + 1;//改
                T* srcptr = data;//源数组首地址
                T* destptr = newarray;//目的数组首地址
                while (n--)        *destptr++ = *srcptr++;//复制
                delete[]data;//删老数组
                data = newarray;//复制新数组
                maxSize = newSize;
        }
}
template<typename T>int SeqList<T>::Search(T& x)const {
        for (int i = 0; i <= last; i++) {
                if (data[i] == x)        return i + 1;
        }
        return 0;
}
template<typename T>int SeqList<T>::Locate(int i)const {
        if (i >= 1 && i <= last + 1)        return i;
        else        return 0;
}
template<typename T>bool SeqList<T>::Insert(int i, T& x) {
        if (last == maxSize - 1)        return false;
        if (i < 0 || i > last + 1) return false;
        for (int j = last; j >= i; j--)        data[j + 1] = data[j];
        data[i] = x;
        last++;
        return true;
}
template<typename T>bool SeqList<T>::Remove(int i, T& x) {
        if (last == -1)        return false;
        if (i < 1 || i > last + 1) return false;
        x = data[i - 1];
        for (int j = i; j <= last; j++)        data[j - 1] = data[j];
        last--;
        return true;
}
template<typename T>void SeqList<T>::Sort() {
        for (int i = 1; i <= last; i++) {
                for (int j = last; j >= i; j--) {
                        if (data[j - 1] > data[j]) {
                                T tmp = data[j - 1];
                                data[j - 1] = data[j];
                                data[j] = tmp;
                        }
                }
        }
}
template<typename T>void SeqList<T>::input() {
        cout << "Input the size of the list which will be created:";
        while (1) {
                assert(cin >> last);
                last--;
                if (last < 0) {
                        cout << "Input error, the size must be positive!\n";
                        cout << "Input the size again:";
                }
                else if (last > maxSize - 1) {//改一改可以扩大
                        cout << "Input error, the size must be less than maxSize!\n";
                        cout << "Input the size again:";
                }
                else        break;
        }
        cout << "\nInput the data for each element to create the list:" << endl;
        for (int i = 0; i <= last; i++) {
                cout << "#" << i + 1 << ":";
                assert(cin >> data[i]);
        }
}
template<typename T>void SeqList<T>::output() {
        cout << "\nThe size of the list is:" << last + 1 << endl;
        for (int i = 0; i <= last; i++)        cout << "#" << i + 1 << ":\t" << data[i] << endl;
}
//SeqList<T> operator = (SeqList<T>& L);
template<typename T>SeqList<T> SeqList<T>::operator = (SeqList<T>& L) {//改
        T value;
        maxSize = L.Size();
        last = L.Length() - 1;
        delete[]data;//先清空
        data = new T[maxSize];
        if (data == NULL) {
                cerr << "Memory allocating error!" << endl;
                exit(1);
        }
        for (int i = 1; i <= last + 1; i++)
        {
                L.getData(i, value);
                data[i - 1] = value;
        }
        return *this;
}
#endif
 //main.cpp
 
 复制代码#include <iostream>
#include "SeqList.h"
#include<string>
#include <fstream>
#include <cassert>
using namespace std;
template<typename T>
SeqList<T> mul(const SeqList<T>& list1,const SeqList<T>& list2)
{
        SeqList<int> list3(list1.Size() + list2.Size());//初始化结果的最大长度,用list3保存。乘法得到数字的位数一定不超过乘数位数之和
        for(int i=0;i<list1.Size();i++)
                for(int j=0;j<list2.Size();j++)
                {
                        int x,y;
                        list1.getData(i, x);
                        list2.getData(j, y);
                        int z=0;
                        z += x * y;
                        list3.setData(i + j, z);
                }
        int t = 0;
        for (int i = 0; i < list3.Size(); i++)//处理进位
        {
                int p;
                list3.getData(i, p);
                t += p;
                int m = t % 10;
                list3.setData(i, m);
                t /= 10;
        }
        while (list3.Size() > 1 && (list3.last) == 0)//初始时结果的最高位为0,若最终最高位仍为0,去掉最高位的0
        {
                int q;
                list3.Remove(list3.last, q);
        }
        return list3;
}
string multiply(string num1, string num2) {
        //num1="123"
        SeqList<int>list1,list2;
        for(int i=num1.size()-1;i>=0;i--)
        {
                int x = num1[i] - '0';
                list1.setData(list1.last, x);
        }//list1=[3,2,1] 倒序输出
        for (int i = num2.size() - 1; i >= 0; i--)
        {
                int y = num2[i] - '0';
                list2.setData(list2.last, y);
        }
        SeqList<int> L;
        L = mul(list1, list2);
        
        string res = "";
        for (int i = L.Size() - 1; i >= 0; i--)
        {
                int p;
                res += to_string(L.getData(i, p));
        }
        return res;
}
int main() {
        SeqList<int>l1, l2, l3;
}
典型的左右值不分问题operator=()函数签名里写的的是一个SeqList<T>&类型的参数,接收一个左值引用
 然而调用时mul()函数返回的是一个临时变量,也就是一个右值,无法进行转换
 operator=()重载是有规则的,复制赋值运算符就写const SeqList<T>& ,移动赋值运算符就写SeqList<T>&& ,你这样SeqList<T>&显得不伦不类。
 (关于移动赋值运算符和复制赋值运算符的区别,这个讲起来没完没了,建议上百度深入了解一下)
 如果不想了解就无脑改成const SeqList<T>&
 | 
 |