我正在开发用于分子动力学模拟的相对基本的代码,并且我决定第一次尝试在某些OOP中进行混合。我的课程P
包含粒子的基本字段(质量,位置和速度),我将为诸如Lennard-Jones之类的特定潜在函数生成派生类。
我在使用setSpecies
函数时遇到了问题;我的目标是能够传递化学式或物种名称并自动设置质量。但是,我似乎无法更改m
的值,除了像将其设置为7一样:
P.h
#ifndef P_H_ #define P_H_ #include <string> #include <vector> using namespace std; struct P { static int N; // total number of particles int n; //particle number double m; //mass void setSpecies(string species); P(); }; #endif
P.cpp
#include "P.h" #include <algorithm/string/predicate.hpp> #include <iostream> using boost::algorithm::iequals; using namespace std; void P::setSpecies(string species){ if (boost::iequals(species,"argon") || boost::iequals(species,"Ar")){ m = 6.6335209e-26; //no good } else m = 0.0; // no good m = 7.0; // this one works }; P::P() { m = 0.0; n = N++; };
即使试图在main()中重新分配值也无济于事:
P atom[10]; for (int i = 0; i < 10; i++){ atom[i].setSpecies("Ar"); \\ no good atom[i].m = 4.0; \\ no good }
我的猜测是它必须对对象位于数组中或者我正在使用
boost
进行某些处理,但是我没有得到任何编译或运行时错误。我想念什么?
我正在开发用于分子动力学模拟的相对基本的代码,并且我决定第一次尝试在某些OOP中进行混合。我的班级P包含基本字段(质量,位置和速度)...
如果忽略类并增强代码,归结为: