c++ calling a class within a class -
so had following code ran perfectly. here "rngsource" makes instance of class contained in rand.h. rng.rfloat64() calls random numbers between 0 , 1.
main.cpp
#include "rand.h" // rngsource rngsource rng; class particle{ public: double r[nd], v[nd]; particle() { (int i=0; i<nd; ++i) { r[i]=l*rng.rfloat64(); v[i]=rng.rfloat64(); } } };
but when tried separate class implementation main.cpp particle.h , particle.cpp, indicated below:
particle.h
#ifndef particle_h #define particle_h class particle{ public: double r[2], v[2]; particle(); }; #endif
particle.cpp
#include "rand.h" // rngsource #include "particle.h" particle::particle() { double ran = (double) rand()/(double)rand_max; static const double l=10; (int i=0; i<2; ++i) { r[i]=l*ran; v[i]=ran; } }
while new main.cpp looks this:
#include "rand.h" /* rngsource() */ #include "particle.h" /* particle class */ rngsource rng; int main(){ rng.rseed(getpid()*time(null)); particle p[n]; .... }
but when try compile following errors:
particle.cpp: in constructor ‘particle::particle()’: particle.cpp:20:17: error: ‘rng’ not declared in scope r[i]=l*rng.rfloat64(); //ran; ^ particle.o:(.data+0x0): multiple definition of `rng_cooked' new.o:(.data+0x0): first defined here particle.o:(.data+0x1300): multiple definition of `kn' new.o:(.data+0x1300): first defined here particle.o:(.data+0x1500): multiple definition of `wn' new.o:(.data+0x1500): first defined here particle.o:(.data+0x1700): multiple definition of `fn' new.o:(.data+0x1700): first defined here new.cpp:(.text+0x1fb1): undefined reference `rngsource::rseed(long long)' particle.o: in function `particle::particle()': particle.cpp:(.text+0x1fa2): undefined reference `rngsource::rseed(long long)' particle.cpp:(.text+0x1fb5): undefined reference `rngsource::rfloat64()' particle.cpp:(.text+0x1fda): undefined reference `rngsource::rfloat64()' collect2: error: ld returned 1 exit status
would here happen know how fix this? thank you.
three problems.
problem 1:
particle.cpp: in constructor ‘particle::particle()’: particle.cpp:20:17: error: ‘rng’ not declared in scope r[i]=l*rng.rfloat64(); //ran;
the ultra cheesy answer add extern rngsource rng;
particle.cpp above particle constructor. extern
means variable exists, storage allocated elsewhere. linker track down you. quick, dirty, , on road minimal code changes.
better solutions add extern rngsource rng;
rand.h , define rngsource rng;
in rand.cpp users or placing extern rngsource rng;
in brand new main.h
, include main.h in particle , other modules called main.
the best solution create bool init(rngsource & rng)
function in particle , grunt work constructor's doing. way if rng doesn't exist, compiler catches when try call init.
particle p[n]; (size_t index; index < n; index++) { p[index].init(rng); }
problem 2
particle.o:(.data+0x0): multiple definition of `rng_cooked' new.o:(.data+0x0): first defined here particle.o:(.data+0x1300): multiple definition of `kn' new.o:(.data+0x1300): first defined here particle.o:(.data+0x1500): multiple definition of `wn' new.o:(.data+0x1500): first defined here particle.o:(.data+0x1700): multiple definition of `fn' new.o:(.data+0x1700): first defined here
rng_cooked
, kn
, wn
, , fn
being defined in rand.h. means includes rand.h tries create own version of variables resulting in collisions when linker tries line names storage locations.
solution similar first problem: add extern
variable definitions in rand.h , define them in rand.cpp.
problem 3
new.cpp:(.text+0x1fb1): undefined reference `rngsource::rseed(long long)' particle.o: in function `particle::particle()': particle.cpp:(.text+0x1fa2): undefined reference `rngsource::rseed(long long)' particle.cpp:(.text+0x1fb5): undefined reference `rngsource::rfloat64()' particle.cpp:(.text+0x1fda): undefined reference `rngsource::rfloat64()'
no idea. passing rand.cpp gcc?
Comments
Post a Comment