作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
#include <stdio.h>
#include <iostream>
#include <conio.h>
#include <locale>
using namespace std;
class Equation
{
private:
double a, b, c, d;
public:
double fun[4];
Equation()
{}
Equation(double a, double b, double c, double d)
{
fun[0] = a;
fun[1] = b;
fun[2] = c;
fun[3] = d;
}
};
int main(int argc, char** argv)
{
for (int i = 0; i<5; i++)
{
Equation arr[i] = new Equation(1, 2, 3, 4);
}
return 0;
}
在这段代码中,我尝试创建(for 循环)和一个数组及其类型 Equation 并给它一些值
Equation arr[i] = new Equation(1, 2, 3, 4);
但是当我运行代码时遇到错误(表达式必须具有常量值)。
最佳答案
你正在重新声明 arr[i]。
for (int i=0; i<5; i++){
Equation arr[i] = new Equation(1, 2, 3, 4);
}
改用这个:
Equation arr[5]; // Array of Equation objects
for (int i=0; i<5; i++){
arr[i] = Equation(1, 2, 3, 4); // assign different object
}
关于c++ - 如何创建循环以遍历数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58901818/
我是一名优秀的程序员,十分优秀!