没有找到接受“SeqList<int>”类型的右操作数的运算符(或没有可接受的转换)
本帖最后由 小易zjy 于 2021-11-28 19:30 编辑等号处报错,但我觉着赋值运算符重载没问题啊,两边都是同样类型的,有大佬知道原因嘛(main.cpp的55行报错L = mul(list1, list2);)
//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;
return true;
}
else return false;
}
void setData(int i, T& x) {
if (i > 0 && i <= last + 1) {
data = 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);
}
return in;
}
friend ostream& operator << (ostream& out, SeqList<T>& R) {
for (int i = 0; i <= R.last; i++) {
cout << "#" << i + 1 << ":\t" << R.data << endl;
}
return out;
}
};
template <typename T>SeqList<T>::SeqList(int sz) {
if (sz > 0) {
maxSize = sz;
last = -1;
data = new T;
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;
if (data == NULL)
{
cerr << "Memory allocating error!" << endl;
exit(1);
}
for (int i = 1; i <= last + 1; i++)
{
L.getData(i, value); data = 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;//建立新数组
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 == 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 = data;
data = 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;
for (int j = i; j <= last; j++) data = data;
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 > data) {
T tmp = data;
data = data;
data = 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);
}
}
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 << 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;
if (data == NULL) {
cerr << "Memory allocating error!" << endl;
exit(1);
}
for (int i = 1; i <= last + 1; i++)
{
L.getData(i, value);
data = 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 - '0';
list1.setData(list1.last, x);
}//list1= 倒序输出
for (int i = num2.size() - 1; i >= 0; i--)
{
int y = num2 - '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;
} 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>)”时 这是什么?一个字符串减一个字符?
本帖最后由 小易zjy 于 2021-11-28 18:30 编辑
人造人 发表于 2021-11-28 17:01
这是什么?一个字符串减一个字符?
我代码复制过来的时候出错了{:10_266:},应该是这样的int y = num2 [ i ] -'0';
不好意思{:10_266:} 路过学习{:5_108:} 少了 operator= 函数
// Seqlist.h
//#pragma once
#ifndef SEQLIST_H
#define SEQLIST_H
#include <cassert>
#include <cstdlib>
#include <iostream>
#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<T> &operator=(const SeqList<T> &rhs) {
// ...
return *this;
}
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;
return true;
} else
return false;
}
void setData(int i, T &x) {
if(i > 0 && i <= last + 1) {
data = 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);
}
return in;
}
friend ostream &operator<<(ostream &out, SeqList<T> &R) {
for(int i = 0; i <= R.last; i++) {
cout << "#" << i + 1 << ":\t" << R.data << endl;
}
return out;
}
};
template <typename T> SeqList<T>::SeqList(int sz) {
if(sz > 0) {
maxSize = sz;
last = -1;
data = new T;
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;
if(data == NULL) {
cerr << "Memory allocating error!" << endl;
exit(1);
}
for(int i = 1; i <= last + 1; i++) {
L.getData(i, value);
data = 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; //建立新数组
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 == 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 = data;
data = 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;
for(int j = i; j <= last; j++)
data = data;
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 > data) {
T tmp = data;
data = data;
data = 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);
}
}
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 << 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;
if(data == NULL) {
cerr << "Memory allocating error!" << endl;
exit(1);
}
for(int i = 1; i <= last + 1; i++)
{
L.getData(i, value);
data = value;
}
return *this;
}
#endif
// main.cpp
#include <iostream>
//#include "SeqList.h"
#include <cassert>
#include <fstream>
#include <string>
using namespace std;
template <typename T>
SeqList<T> mul(const SeqList<T> &list1, const SeqList<T> &list2) {
SeqList<int> list3(list1.Size() + list2.Size());
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
{
int q;
list3.Remove(list3.last, q);
}
return list3;
}
string multiply(string num1, string num2) {
SeqList<int> list1, list2;
for(int i = num1.size() - 1; i >= 0; i--) {
int x = num1 - '0';
list1.setData(list1.last, x);
}
for(int i = num2.size() - 1; i >= 0; i--) {
int y = num2 - '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;
return 0;
}
人造人 发表于 2021-11-28 18:46
少了 operator= 函数
第209行开始的不是operator=嘛{:10_250:}
还有我发现好多中括号直接复制过来格式都变了,现在都已经改过来了
之前没检查 抱歉抱歉{:10_266:} 小易zjy 发表于 2021-11-28 18:54
第209行开始的不是operator=嘛
还有我发现好多中括号直接复制过来格式都变了,现在都已经改过 ...
你这代码写的,我有点看不懂,我找时间研究研究
人造人 发表于 2021-11-28 19:01
你这代码写的,我有点看不懂,我找时间研究研究
好滴好滴 感谢! 小易zjy 发表于 2021-11-28 19:06
好滴好滴 感谢!
改了一部分,剩下的函数不知道做什么用的,没法改了
// Seqlist.h
//#pragma once
#ifndef SEQLIST_H
#define SEQLIST_H
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
const int defaultSize = 100;
template <typename T>
class SeqList {
protected:
T *data;
size_t maxSize;
size_t size;
void resize() {
size_t size = 2 * this->maxSize;
T *temp = new T;
memcpy(temp, this->data, this->size * sizeof(T));
delete[] this->data;
this->data = temp;
this->maxSize = size;
}
public:
SeqList(size_t sz) {
this->maxSize = sz;
this->data = new T;
this->size = 0;
}
SeqList(const SeqList<T> &L) {
this->data = nullptr;
this->operator=(L);
}
~SeqList() {delete[] this->data;}
bool Insert(size_t i, const T &x) {
if(i > this->Length()) return false;
++this->size;
if(this->size > this->maxSize) {
this->resize();
}
for(size_t j = this->size - 1; j != i; --j) {
this->data = this->data;
}
this->data = x;
return true;
}
bool Remove(size_t i) {
if(i >= this->Length()) return false;
for(size_t j = i; j < this->size - 1; ++j) {
this->data = this->data;
}
--this->size;
return true;
}
int Length() const {return this->size;}
bool getData(size_t i, T &x) const {
if(i >= this->Length()) return false;
x = this->data;
return true;
}
bool setData(size_t i, const T &x) {
if(i >= this->Length()) return false;
this->data = x;
return true;
}
bool IsEmpty() {return this->Length() == 0;}
SeqList<T> operator=(const SeqList<T> &L) {
delete[] this->data;
this->maxSize = L.maxSize;
this->data = new T;
this->size = L.size;
memcpy(this->data, L.data, L.size * sizeof(T));
return *this;
}
void clean() {this->size = 0;}
bool append(const T &value) {
return this->Insert(this->Length(), value);
}
friend istream &operator>>(istream &in, SeqList<T> &R) {
R.clean();
T value;
while(in >> value) {
R.append(value);
}
return in;
}
friend ostream &operator<<(ostream &out, const SeqList<T> &R) {
for(int i = 0; i < R.Length(); i++) {
T value; R.getData(i, value);
cout << "#" << i + 1 << ":\t" << value << endl;
}
return out;
}
int Search(const T &x) const {
for(size_t i = 0; i < this->Length(); i++) {
T value; this->getData(i, value);
if(value == x) return i;
}
return -1;
}
void Sort() {
for(size_t i = 0; i < this->Length(); ++i) {
for(size_t j = i + 1; j < this->Length(); ++j) {
T a, b;
this->getData(i, a);
this->getData(j, b);
if(a > b) {
this->setData(i, b);
this->setData(j, a);
}
}
}
}
};
// 这个函数做什么用的?
// 判断索引i是否存在?
// 你直接调用Length函数不就可以了?
/*
template <typename T> int SeqList<T>::Locate(int i) const {
if(i >= 1 && i <= last + 1)
return i;
else
return 0;
}
*/
// 下面这两个函数做什么用的?
/*
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);
}
}
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 << endl;
}
*/
#endif
// main.cpp
#include <iostream>
//#include "SeqList.h"
#include <cassert>
#include <fstream>
#include <string>
using namespace std;
template <typename T>
SeqList<T> mul(const SeqList<T> &list1, const SeqList<T> &list2) {
SeqList<int> list3(list1.Size() + list2.Size());
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
{
int q;
list3.Remove(list3.last, q);
}
return list3;
}
string multiply(string num1, string num2) {
SeqList<int> list1, list2;
for(int i = num1.size() - 1; i >= 0; i--) {
int x = num1 - '0';
list1.setData(list1.last, x);
}
for(int i = num2.size() - 1; i >= 0; i--) {
int y = num2 - '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;
return 0;
}
典型的左右值不分问题
operator=()函数签名里写的的是一个SeqList<T>&类型的参数,接收一个左值引用
然而调用时mul()函数返回的是一个临时变量,也就是一个右值,无法进行转换
operator=()重载是有规则的,复制赋值运算符就写const SeqList<T>& ,移动赋值运算符就写SeqList<T>&& ,你这样SeqList<T>&显得不伦不类。
(关于移动赋值运算符和复制赋值运算符的区别,这个讲起来没完没了,建议上百度深入了解一下)
如果不想了解就无脑改成const SeqList<T>&
页:
[1]