C++ 在C+中为对象赋值+;

C++ 在C+中为对象赋值+;,c++,C++,我算是编程的初学者。我试图编写一个C++类,可以保存多维数据(比如MXN矩阵)。我不想用向量的方法来做。我写了下面的代码,但是当我用g++编译它时,我得到了分段错误:11错误。另一方面,当我尝试用Xcode编译它时,它在第行给出“thread1:EXC_BAD_ACCESS(code=1,address=0x0)” particles[i].x = x0 + R*cos(theta*i); particles[0].Fx = -k*(particles[1].x - particles[N].

我算是编程的初学者。我试图编写一个C++类,可以保存多维数据(比如MXN矩阵)。我不想用向量的方法来做。我写了下面的代码,但是当我用g++编译它时,我得到了分段错误:11错误。另一方面,当我尝试用Xcode编译它时,它在第行给出“thread1:EXC_BAD_ACCESS(code=1,address=0x0)”

particles[i].x = x0 + R*cos(theta*i);
particles[0].Fx = -k*(particles[1].x - particles[N].x);
所以,我想我得到了一种与给对象赋值有关的内存错误

C++中是否允许使用这种语法:

particles_old[i].x = particles[i].x;
或者我可以这样说:

// say A is a class with x a vector this time, instead of a double data. 
// in nested for loops
vector<A> B;
B[i].x[j] = some value;
//假设A是一个类,而x是一个向量,而不是双数据。
//在嵌套for循环中
载体B;
B[i].x[j]=某个值;
我知道这有点模糊,但至少从语法的角度来看,它是正确的吗

#include <iostream>
#include <cmath>
#include <fstream>
#include <string>
#include <vector>
#include <random>
using namespace std;

class Particle{
public:
    double x; // x position
    double y; // y position
    double vx; // velocity in the x direction
    double vy; // velocity in the y direction
    double Fx; // force in the x direction
    double Fy; // force in the y direction

    // Default constructor
    Particle()
    : x(0.0),y(0.0),vx(0.0),vy(0.0),Fx(0.0),Fy(0.0){
    }
};

int main() {

    const float pi = 3.14;
    int N = 30; // Number of 'particles' that make up the cell
    float theta = 2*pi/N; // Angle between two particles in radians
    float x0 = 0; // Center of the cell [x]
    float y0 = 0; // Center of the cell [y]
    float R = 5e-6; // Radius of the cell
    vector<Particle> particles; // particles

    // Assigning the initial points onto the circle
    for(int i = 0; i < N; i++) {
        particles[i].x = x0 + R*cos(theta*i);
        particles[i].y = y0 + R*sin(theta*i);
    }

    float k = 4.3e-7; // Spring constant connecting the particles
    float m = 2e-8; // Mass of the particles

    // Calculating the initial spring force between the particles on the cell
    particles[0].Fx = -k*(particles[1].x - particles[N].x);
    particles[0].Fy = -k*(particles[1].y - particles[N].y);
    for(int i = 1; i < N-1; i++) {
        particles[i].Fx = -k*(particles[i+1].x - particles[i-1].x);
        particles[i].Fy = -k*(particles[i+1].y - particles[i-1].y);
    }
    particles[N].Fx = -k*(particles[0].x - particles[N-1].x);
    particles[N].Fy = -k*(particles[0].y - particles[N-1].y);

    // Initial velocities are given to each particle randomly from a Gaussian distribution
    random_device rdx; // Seed
    default_random_engine generatorx(rdx()); // Default random number generator
    random_device rdy; // Seed
    default_random_engine generatory(rdy()); // Default random number generator
    normal_distribution<float> distributionx(0,1); // Gaussian distribution with 0 mean and 1 variance
    normal_distribution<float> distributiony(0,1); // Gaussian distribution with 0 mean and 1 variance
    for(int i = 0; i < N; i++) {
        float xnumber = distributionx(generatorx);
        float ynumber = distributiony(generatory);
        particles[i].vx = xnumber;
        particles[i].vy = ynumber;
    }


    // Molecular dynamics simulation with velocity Verlet algorithm

    // 'Old' variables
    vector<Particle> particles_old;

    for(int i = 0; i < N; i++) {
        particles_old[i].x = particles[i].x;
        particles_old[i].y = particles[i].y;
        particles_old[i].vx = particles[i].vx;
        particles_old[i].vy = particles[i].vy;
        particles_old[i].Fx = particles[i].Fx;
        particles_old[i].Fy = particles[i].Fy;
    }

    // Sampling variables
    int sampleFreq = 2;
    int sampleCounter = 0;

    // MD variables
    float dt = 1e-7;
    float dt2 = dt*dt;
    float m2 = 2*m;
    int MdS = 1e+4; // Molecular dynamics step number

    // MD
    for(int j = 0; j < MdS; j++) {

        // Update x
        for(int i = 0; i < N; i++) {
            particles[i].x = particles_old[i].x + dt*particles_old[i].vx + dt2*particles_old[i].Fx/m2;
            particles[i].y = particles_old[i].y + dt*particles_old[i].vy + dt2*particles_old[i].Fy/m2;
        }

        // Update F
        particles[0].Fx = -k*(particles[1].x - particles[N].x);
        particles[0].Fy = -k*(particles[1].y - particles[N].y);
        for(int i = 1; i < N-1; i++) {
            particles[i].Fx = -k*(particles[i+1].x - particles[i-1].x);
            particles[i].Fy = -k*(particles[i+1].y - particles[i-1].y);
        }
        particles[N].Fx = -k*(particles[0].x - particles[N-1].x);
        particles[N].Fy = -k*(particles[0].y - particles[N-1].y);

        // Update v
        for(int i = 0; i < N; i++) {
            particles[i].vx = particles_old[i].vx + dt*(particles_old[i].Fx + particles[i].Fx)/m2;
            particles[i].vy = particles_old[i].vy + dt*(particles_old[i].Fy + particles[i].Fy)/m2;
        }

        // Copy new variables to old variables
        for(int i = 0; i < N; i++) {
            particles_old[i].x = particles[i].x;
            particles_old[i].y = particles[i].y;
            particles_old[i].vx = particles[i].vx;
            particles_old[i].vy = particles[i].vy;
            particles_old[i].Fx = particles[i].Fx;
            particles_old[i].Fy = particles[i].Fy;
        }

    }

}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
类粒子{
公众:
双x;//x位置
双y;//y位置
双vx;//x方向的速度
double vy;//y方向上的速度
double Fx;//x方向的力
双Fy;//y方向上的力
//默认构造函数
粒子()
:x(0.0)、y(0.0)、vx(0.0)、vy(0.0)、Fx(0.0)、Fy(0.0){
}
};
int main(){
常数浮点pi=3.14;
int N=30;//组成单元的“粒子”数
float theta=2*pi/N;//两个粒子之间的角度(弧度)
float x0=0;//单元格的中心[x]
浮点y0=0;//单元格的中心[y]
浮点数R=5e-6;//单元格的半径
向量粒子;//粒子
//将初始点指定到圆上
对于(int i=0;i
提前谢谢

// Assigning the initial points onto the circle
for(int i = 0; i < N; i++) {
    particles[i].x = x0 + R*cos(theta*i);
    particles[i].y = y0 + R*sin(theta*i);
}
这会显式地为您创建一个新的
粒子
,然后使用
.push_back()
方法将其添加到您的
std::vector

请注意这两行:

particles[N].Fx = -k*(particles[0].x - particles[N-1].x);
particles[N].Fy = -k*(particles[0].y - particles[N-1].y);

可能会因为完全相同的原因崩溃,因为此时您已经完全填充了[0..N-1],您还没有在插槽N中放置元素。我将让您来解决这个问题,因为解决方案与for循环中的完全相同。

嘿,例如,在将粒子添加到列表中时,您需要初始化粒子

for (int i = 0; i < N+1; i++) { // You are accessing [N] directly in your code..
    Particle p;
    p.x = x0 + R*cos(theta*i);
    p.y = y0 + R*sin(theta*i);
    particles.push_back(p);
}
for(int i=0;ivector<Particle> particles(N);  // create a vector with N particles default initialized. 
particles[0].Fx = -k*(particles[1].x - particles[N].x);