c++ - 输出数组的值而不是数组的内存地址
问题描述
所以我在 C++ 中创建了一个基本的多项式类,它将这些多项式的系数动态地存储在堆上。我目前正在重载运算符,以便我可以一起添加/减去多项式以简化它们等。但是,当我尝试重载 * 运算符时,我得到了意想不到的结果。看起来它不是返回数组中索引的值,而是返回数组的位置。这是我的 .cpp 文件中的 *operator 方法:
Polynomial Polynomial::operator*(Polynomial p) {
int maxDegree = (degree)+(p.degree - 1);
int *intArray3 = new int[maxDegree];
int i, j;
for (int i = 0; i < degree; i++) {
for (int j = 0; j < p.degree; j++) {
cout << getCoef(i) << " * " << p.getCoef(j) << " = " << getCoef(i)*p.getCoef(j) << endl;
intArray3[j] += (getCoef(i))*(p.getCoef(j));
cout << " intArray3[" << j << "] contains : " << intArray3[j] << endl;
}
}
return Polynomial(maxDegree, intArray3);}
这些行:
cout << getCoef(i) << " * " << p.getCoef(j) << " = " << getCoef(i)*p.getCoef(j) << endl;
和
cout << " intArray3[" << j << "] contains : " << intArray3[j] << endl;
返回
10 * 1 = 10
intArray3[0] contains : -842150441
在我的控制台中。我假设问题出在我在某处使用指针,但我无法终生思考为什么。我以与我的 + 和 - 重载类似的方式实现了这个重载,它们工作正常。任何帮助将不胜感激。干杯。
解决方案
推荐阅读
- javascript - 我们可以在类方法中使用 IIFE(立即调用函数表达式)吗
- spring-webflux - Reactor - 第一次清空时停止源
- excel - 使用 VBA 将 .xlsx 文件中的唯一值打印和计数到 Excel 工作表中的另一列
- javascript - 将数组中的字符串连接在一起,限制字符重复
- azure - 需要在 csv 文件中添加标题和尾部记录 - Azure 数据工厂
- delphi - 有什么方法可以获取Delphi编译器的子版本
- python-3.x - 我如何解决这个问题?混淆安装 wxpython
- html - 当我只想滚动一个部分时,整个页面都会滚动
- python - If x == y: print z - 为什么我的代码不打印?
- angular - 如何让验证器触发或获取验证器内部的正确控件?