在C中为对象赋值

在C中为对象赋值,第1张

概述我可以被认为是编程的初学者.我试图在C中编写一个可以保存多维数据的类(例如MxN矩阵).我不想通过矢量矢量的方式来做.我编写了下面这段代码,但是当我用g编译它时,我得到了分段错误:11错误.另一方面,当我尝试使用 Xcode编译它时,它在行中给出“线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)” particles[i].x = x0 + R*cos(theta*i); 所以, 我可以被认为是编程的初学者.我试图在C中编写一个可以保存多维数据的类(例如MxN矩阵).我不想通过矢量矢量的方式来做.我编写了下面这段代码,但是当我用g编译它时,我得到了分段错误:11错误.另一方面,当我尝试使用 Xcode编译它时,它在行中给出“线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)”

particles[i].x = x0 + R*cos(theta*i);

所以,我想我得到了一种与为对象分配值相关的内存错误.

在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 loopsvector<A> B;B[i].x[j] = some value;

我知道它有点模糊,但从语法的角度来看它至少是正确的吗?

#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;        }    }}

提前致谢.

解决方法 你已经有两个很好的答案了.这是另一种变体:

如果您事先知道矢量的大小,您可以按如下方式定义:

vector<Particle> particles(N);  // create a vector with N particles default initialized.

然后你的代码的其余部分应该工作,因为粒子[i]将引用已经存在的粒子.

是的,old_particles =粒子;将复制整个矢量.无需循环中的元素copyelement.

总结

以上是内存溢出为你收集整理的在C中为对象赋值全部内容,希望文章能够帮你解决在C中为对象赋值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1216266.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存