gpt4 book ai didi

c++ - 如何创建循环以遍历数组

转载 作者:行者123 更新时间:2023-11-28 01:15:17 25 4
gpt4 key购买 nike

#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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com