gpt4 book ai didi

c++ - 在类中创建 vector 的 vector (二维数组)- 错误 :C++ requires a type specifier for all declarations

转载 作者:行者123 更新时间:2023-11-30 04:46:59 25 4
gpt4 key购买 nike

因此,我在 C++ 中使用 Runge-Kutta 4(RK4) 方法程序化地编写了一种积分方程的方法。我目前正在尝试创建一个 RK4 类来为我的程序添加更多功能。但是,当我在类中设置我的二维数组时,我收到“C++ 需要所有声明的类型说明符”错误,这是我在原始程序中没有收到的。

起初,我使用的是 namespace std;,但是我遇到了一个回答,说那是糟糕的编程习惯,所以我删除了那行并改为调用 std::vector。我还确保包含 vector 库,但无济于事。

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <vector>

class RK4 {
public:
RK4(double x_i, double y_i, double z_i, double vx_i, double vy_i, double vz_i);
//time-related stuff
double dt = 0.01;
int numtimes = 1000;

int numvar = 3;
double mu = 398589590898200.0;

//setting up variables
std::vector<vector<double> > position(numtimes,vector<double>(numvar));
std::vector<vector<double> > velocity(numtimes,vector<double>(numvar));
std::vector<vector<double> > rk1(numtimes,vector<double>(numvar));
std::vector<vector<double> > rk2(numtimes,vector<double>(numvar));
std::vector<vector<double> > rk3(numtimes,vector<double>(numvar));
std::vector<vector<double> > rk4(numtimes,vector<double>(numvar));
std::vector<vector<double> > vk1(numtimes,vector<double>(numvar));
std::vector<vector<double> > vk2(numtimes,vector<double>(numvar));
std::vector<vector<double> > vk3(numtimes,vector<double>(numvar));
std::vector<vector<double> > vk4(numtimes,vector<double>(numvar));

这是我在原始程序中构建代码的方式,没有问题:

#include <iostream>
#include <ostream>
#include <cmath>
#include <vector>

using namespace std;

int main(){
// time related things
//double t_i = 0; // initial time
double dt = 0.01; // time step
int numtimes = 10000; // how many steps taken

vector<vector<double> > position(numtimes,vector<double>(numvar));
vector<vector<double> > velocity(numtimes,vector<double>(numvar));

vector<vector<double> > rk1(numtimes,vector<double>(numvar));
vector<vector<double> > rk2(numtimes,vector<double>(numvar));
vector<vector<double> > rk3(numtimes,vector<double>(numvar));
vector<vector<double> > rk4(numtimes,vector<double>(numvar));
vector<vector<double> > vk1(numtimes,vector<double>(numvar));
vector<vector<double> > vk2(numtimes,vector<double>(numvar));
vector<vector<double> > vk3(numtimes,vector<double>(numvar));
vector<vector<double> > vk4(numtimes,vector<double>(numvar));
}

我不太清楚为什么这个调用在我原来的(程序)程序中有效,但在我当前的程序中却无效。

最佳答案

对于类,您不能在函数之外初始化成员。不过,您可以在构造函数中分配它们。

另外,你在一堆需要“std::vector”的地方写了“vector”。这应该修复它:

#include <math.h>
#include <stdio.h>
#include <iostream>
#include <vector>

class RK4 {
public:
RK4(double x_i, double y_i, double z_i, double vx_i, double vy_i, double vz_i) {
double dt = 0.01;
int numtimes = 1000;

int numvar = 3;
double mu = 398589590898200.0;

//assigning the vectors
position = std::vector<std::vector<double> >(numtimes, std::vector<double>(numvar));
velocity = std::vector<std::vector<double> >(numtimes, std::vector<double>(numvar));
rk1 = std::vector<std::vector<double> >(numtimes, std::vector<double>(numvar));
rk2 = std::vector<std::vector<double> >(numtimes, std::vector<double>(numvar));
rk3 = std::vector<std::vector<double> >(numtimes, std::vector<double>(numvar));
rk4 = std::vector<std::vector<double> >(numtimes, std::vector<double>(numvar));
vk1 = std::vector<std::vector<double> >(numtimes, std::vector<double>(numvar));
vk2 = std::vector<std::vector<double> >(numtimes, std::vector<double>(numvar));
vk3 = std::vector<std::vector<double> >(numtimes, std::vector<double>(numvar));
vk4 = std::vector<std::vector<double> >(numtimes, std::vector<double>(numvar));
}
//time-related stuff
double dt = 0.01;
int numtimes = 1000;

int numvar = 3;
double mu = 398589590898200.0;

//declaring vectors
std::vector<std::vector<double> > position;
std::vector<std::vector<double> > velocity;
std::vector<std::vector<double> > rk1;
std::vector<std::vector<double> > rk2;
std::vector<std::vector<double> > rk3;
std::vector<std::vector<double> > rk4;
std::vector<std::vector<double> > vk1;
std::vector<std::vector<double> > vk2;
std::vector<std::vector<double> > vk3;
std::vector<std::vector<double> > vk4;
};

int main() {
RK4 r(1, 1, 1, 1, 1, 1);

return 0;
}

关于c++ - 在类中创建 vector 的 vector (二维数组)- 错误 :C++ requires a type specifier for all declarations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56403388/

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