c ++在类中调用类

时间:2015-06-03 23:04:08

标签: c++ class header-files

所以我有以下完美运行的代码。 在这里" rngSource"创建rand.h中包含的类的实例。 rng.rFloat64()调用0到1之间的随机数。

的main.cpp

#include "rand.h"   // rngSource

rngSource rng;

class particle{
public:
    double r[nd], v[nd];
    particle()
    {
            for (int i=0; i<nd; ++i)
            {
                    r[i]=L*rng.rFloat64();
                    v[i]=rng.rFloat64(); 
            }
     }
};

但是当我尝试将类实现从main.cpp分离到particle.h和particle.cpp时,如下所示:

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;
    for (int i=0; i<2; ++i)
    {
            r[i]=L*ran;
            v[i]=ran;            
    }
}

虽然新的main.cpp看起来像这样:

#include "rand.h"    /* rngSource() */
#include "particle.h"   /* particle class */

rngSource rng;

int main(){
    rng.rseed(getpid()*time(NULL));
    particle p[N];
    ....
}

但是当我尝试编译时,我得到以下错误:

particle.cpp: In constructor ‘particle::particle()’:
particle.cpp:20:17: error: ‘rng’ was not declared in this 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 to `rngSource::rseed(long long)'
particle.o: In function `particle::particle()':
particle.cpp:(.text+0x1fa2): undefined reference to `rngSource::rseed(long long)'
particle.cpp:(.text+0x1fb5): undefined reference to `rngSource::rFloat64()'
particle.cpp:(.text+0x1fda): undefined reference to `rngSource::rFloat64()'
collect2: error: ld returned 1 exit status

这里有人会碰巧知道如何解决这个问题吗? 谢谢。

1 个答案:

答案 0 :(得分:0)

三个问题。

问题1:

particle.cpp: In constructor ‘particle::particle()’:
particle.cpp:20:17: error: ‘rng’ was not declared in this scope
      r[i]=L*rng.rFloat64(); //ran;

超级俗气的答案是将extern rngSource rng;添加到粒子构造函数上方的particle.cpp。 extern表示此变量存在,但其存储在其他位置分配。链接器将为您跟踪它。这很快,很脏,只需最少的代码更改就可以让你回到正轨。

更好的解决方案是将extern rngSource rng;添加到rand.h,然后在rand.cpp中为所有用户定义rngSource rng;,或将extern rngSource rng;放入全新的main.h并添加main中的main.h和main调用的其他模块。

最好的解决方案可能是在粒子中创建一个bool init(rngSource & rng)函数,并对构造函数当前正在进行的工作进行咕噜声。这样,如果rng不存在,编译器会在你尝试调用init时捕获它。

particle p[N];
for (size_t index; index < N; index++)
{
    p[index].init(rng);
}

问题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_cookedknwnfn几乎肯定是在rand.h中定义的。这意味着当链接器尝试将名称与存储位置对齐时,包含rand.h的每个人都会尝试创建这些变量的自己版本,从而导致冲突。

解决方案类似于第一个问题:将extern添加到rand.h中的变量定义中,并在rand.cpp中定义它们。

问题3

new.cpp:(.text+0x1fb1): undefined reference to `rngSource::rseed(long long)'
particle.o: In function `particle::particle()':
particle.cpp:(.text+0x1fa2): undefined reference to `rngSource::rseed(long long)'
particle.cpp:(.text+0x1fb5): undefined reference to `rngSource::rFloat64()'
particle.cpp:(.text+0x1fda): undefined reference to `rngSource::rFloat64()'

不知道。你确定你正在将rand.cpp传递给gcc吗?