double newpoly::add(polynomial &num)
{
int i;
double num4[12];
for (i = 12; i > 0; i--)
{
num4[i] = num.num[i] + num3[i];
}
if (num4[11] != 0)
{
cout << num4[11] << "x*x*x*x*x*x*x*x*x*x+";
};
if (num4[10] != 0)
{
cout << num4[10] << "x*x*x*x*x*x*x*x*x+";
};
if (num4[9] != 0)
{
cout << num4[9] << "x*x*x*x*x*x*x*x+";
};
if (num4[8] != 0)
{
cout << num4[8] << "x*x*x*x*x*x*x+";
};
if (num4[7] != 0)
{
cout << num4[7] << "x*x*x*x*x*x+";
};
if (num4[6] != 0)
{
cout << num4[6] << "x*x*x*x*x+";
};
if (num4[5] != 0)
{
cout << num4[5] << "x*x*x*x+";
};
if (num4[4] != 0)
{
cout << num4[4] << "x*x*x+";
};
if (num4[3] != 0)
{
cout << num4[3] << "x*x+";
};
if (num4[2] != 0)
{
cout << num4[2] << "x+";
};
if (num4[1] != 0)
{
cout << num4[1] << endl;
}
else
{
cout << endl;
};
return *num4;
};
class overloads
{
public:
double ope1[12], ope2[12], ope3[12];
double ans1[12], ans2[12];
double x2;
void convey3(newpoly &x)
{
x2 = x.x;
}
void convey1(polynomial &num)
{
int i;
for (i = 12; i > 0; i--)
{
ope1[i] = num.num[i];
}
};
void convey2(newpoly &num3)
{
int i;
for (i = 12; i > 0; i--)
{
ope2[i] = num3.num3[i];
}
};
void convey4(newpoly &num2)
{
int i;
for (i = 12; i > 0; i--)
{
ope3[i] = num2.num2[i];
}
};
overloads operator+(overloads &ope2)
{
overloads temp;
temp.ans1[1] = this->ope1[1] + ope2.ope2[1];
temp.ans1[2] = this->ope1[2] + ope2.ope2[2];
temp.ans1[3] = this->ope1[3] + ope2.ope2[3];
temp.ans1[4] = this->ope1[4] + ope2.ope2[4];
temp.ans1[5] = this->ope1[5] + ope2.ope2[5];
temp.ans1[6] = this->ope1[6] + ope2.ope2[6];
temp.ans1[7] = this->ope1[7] + ope2.ope2[7];
temp.ans1[8] = this->ope1[8] + ope2.ope2[8];
temp.ans1[9] = this->ope1[9] + ope2.ope2[9];
temp.ans1[10] = this->ope1[10] + ope2.ope2[10];
temp.ans1[11] = this->ope1[11] + ope2.ope2[11];
return temp;
}
overloads operator*(overloads &ope3)
{
overloads temp;
int i;
double x3[12];
for (i = 12; i > 0; i--)
{
x3[i] = x2;
}
temp.ans2[1] = this->ope3.ope3[1] * x3[1];
}
};
for (i = 12; i > 0; i--)
{
x3[i] = x2;
}
temp.ans2[1] = this->ope3.ope3[1] * x3[1];
}
然后VScode就出现了这个报错:member reference base type 'double[12]' is not a structure or union。
我想知道这个为什么会报错,因为operator+同样是类似的格式,但是没有报错。谢谢。
这不是结构体呀,你为什么按结构体的方式使用它,它是数组
直接用下标索引就行,不要用num.这种
for循环里 i 不能= 12 ,改成=11
不知道你这个问题是否已经解决, 如果还没有解决的话:回答:
这个错误信息表明在一个double
类型的数组上使用了类似于结构体或联合体的成员引用操作。这很可能是由于您在代码中使用了以下语法格式之一:
double my_nums[12];
my_nums.size(); // 错误!double 不是一个类,它没有成员函数
my_nums.first(); // 错误!double 不是一个类,它没有成员函数
以上代码使用了类似于C++ STL中vector
或array
的语法,但实际上它们只适用于类类型。由于double
不是一个类,因此在它上面使用这些成员引用操作就会触发编译错误。
如果您要使用类似于size()
或first()
的操作,您需要使用类类型,或者编写一个自己的类来模拟这些操作。以下是一个使用模板的示例,它将任何类型的数组转换为可迭代的类类型:
template<typename T, size_t N>
class Array
{
private:
T _data[N]; // 存储实际数据的数组
public:
// 返回数组大小
size_t size() const
{
return N;
}
// 返回数组的第一个元素
T& first()
{
return _data[0];
}
// 返回数组的最后一个元素
T& last()
{
return _data[N-1];
}
// 迭代器类型
typedef T* iterator;
// 返回指向第一个元素的迭代器
iterator begin()
{
return &_data[0];
}
// 返回指向最后一个元素后面一个的迭代器
iterator end()
{
return &_data[N];
}
// 迭代器类型(常量版本)
typedef const T* const_iterator;
// 返回指向第一个元素的常量迭代器
const_iterator begin() const
{
return &_data[0];
}
// 返回指向最后一个元素后面一个的常量迭代器
const_iterator end() const
{
return &_data[N];
}
// 索引运算符重载
T& operator[](size_t i)
{
return _data[i];
}
const T& operator[](size_t i) const
{
return _data[i];
}
};
使用此类,您可以像使用vector
或array
一样使用它:
Array<double, 12> my_nums;
cout << my_nums.size() << endl; // 输出 12
cout << my_nums.first() << endl; // 输出 my_nums[0] 的值
希望这对您有所帮助!