ao 和 bo 中的重复符号 _FOO - 这不是 C++ 中的常见原因;

发布于 2024-12-29 15:01:53 字数 2093 浏览 1 评论 0原文

首先,感谢您提供本网站上的所有精彩信息。这始终是我解决编程问题的第一站。不幸的是,我找不到其他关于类似问题的帖子中提到的解决方案似乎都有效。

我习惯于编写非常大的模拟,但最近遇到了一个我以前从未遇到过的错误。为了调试,我已将代码减少到几行,但我仍然遇到问题。也就是说:

当我单击构建(使用 QTcreator 和 C++)时,出现以下错误:

“:: error: randoms.o 和 main.o 中的重复符号 _GENERATOR”

“:: error:collect2:ld returned 1 exit status”

通过阅读其他帖子,我知道这往往是两次 #include-ing 的结果,但我还没有这样做。

以下是生成此错误的一些代码的示例:

该代码由 randoms.h、randoms.cpp 和 main.cpp 组成

randoms.h:

#ifndef RANDOMS_H
#define RANDOMS_H

#include <iostream>
#include <iomanip>
#include <ctime>
#include <boost/random.hpp>
#include <stdio.h>
#include <stdlib.h>

const double price = 127.4;

boost::mt19937 GENERATOR(static_cast<unsigned> (std::time(0)));
int randIntRange(int low, int high);
double randDoubleRange(double low, double high);
void d_Range(std::vector<double> *range, double low, double high, double breaks);

#endif // RANDOMS_H

randoms.cpp:

#include"randoms.h"

int randIntRange(int low, int high) {
    boost::uniform_int<> dist(low, high);
    boost::variate_generator<boost::mt19937&, boost::uniform_int<> > range(GENERATOR, dist);
    return range();
}

double randDoubleRange(double low, double high) {
    boost::uniform_real<> dist(low, high);
    boost::variate_generator<boost::mt19937&, boost::uniform_real<> > range(GENERATOR, dist);
    return range();
}

void d_Range(std::vector<double> *range, double low, double high, double breaks) {
    double interval;
    interval = double(high - low)/double(breaks);
    for (double i=low; i < high; i+=interval) {
        range->push_back(i);
    }
}

main.cpp:

#include"randoms.h"

int main() {

    double a = 156;
    double b = 14256;
    std::cout << a << " divided by " << b << " close to " << ((a/b)*randDoubleRange(0.9,1.1)) << std::endl;

    return 0;
}

也许我只是在做一些非常愚蠢的事情,但我无法弄清楚为什么任何东西都会被重复。任何帮助将不胜感激。

提前致谢

First of all, thanks for all of the great info on this site. It's always my first port of call for programming problems. Unfortunately I couldn't find none of the solutions mentioned in other posts about similar problems seemed to work.

I have am used to coding very large simulations but recently got an error that I hadn't come across before. I've reduced the code down to a matter of lines - for the sake of debugging and am stil getting the problem. That is:

When I click build (Using QTcreator and C++), I get the following error:

":: error: duplicate symbol _GENERATOR in randoms.o and main.o"

":: error: collect2: ld returned 1 exit status"

Form reading other posts I know that this tends to be the result of #include-ing things twice but I have not done this.

Here is an example of some code that generates this error:

The code consists of randoms.h, randoms.cpp and main.cpp

randoms.h:

#ifndef RANDOMS_H
#define RANDOMS_H

#include <iostream>
#include <iomanip>
#include <ctime>
#include <boost/random.hpp>
#include <stdio.h>
#include <stdlib.h>

const double price = 127.4;

boost::mt19937 GENERATOR(static_cast<unsigned> (std::time(0)));
int randIntRange(int low, int high);
double randDoubleRange(double low, double high);
void d_Range(std::vector<double> *range, double low, double high, double breaks);

#endif // RANDOMS_H

randoms.cpp:

#include"randoms.h"

int randIntRange(int low, int high) {
    boost::uniform_int<> dist(low, high);
    boost::variate_generator<boost::mt19937&, boost::uniform_int<> > range(GENERATOR, dist);
    return range();
}

double randDoubleRange(double low, double high) {
    boost::uniform_real<> dist(low, high);
    boost::variate_generator<boost::mt19937&, boost::uniform_real<> > range(GENERATOR, dist);
    return range();
}

void d_Range(std::vector<double> *range, double low, double high, double breaks) {
    double interval;
    interval = double(high - low)/double(breaks);
    for (double i=low; i < high; i+=interval) {
        range->push_back(i);
    }
}

main.cpp:

#include"randoms.h"

int main() {

    double a = 156;
    double b = 14256;
    std::cout << a << " divided by " << b << " close to " << ((a/b)*randDoubleRange(0.9,1.1)) << std::endl;

    return 0;
}

Perhaps I'm just doing something really silly but I can't figure out why anything would be duplicated. Any help would be much appreciated.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷爱 2025-01-05 15:01:53

有这个全局变量

boost::mt19937 GENERATOR(static_cast<unsigned> (std::time(0)));

您在头文件中 ,它包含在两个文件中。也就是说,它被定义了两次。您可能希望将声明放在标头中

extern boost::mt19937 GENERATOR;

,将定义放在其中一个 cpp 文件中。就像处理函数一样。

You have this global

boost::mt19937 GENERATOR(static_cast<unsigned> (std::time(0)));

in the header file which is included in two files. That is, it is defined twice. You probably want to put the declaration in header

extern boost::mt19937 GENERATOR;

and the definition in one of the cpp files. just like you do with functions.

情归归情 2025-01-05 15:01:53

这实际上只是定义同一个变量两次的情况,与 boost 无关。就像这样:

hello1.cxx

int hello;

main.cxx

int hello;
int main (){return 1}

有关如何处理的信息,请参阅 Michael 的答案。

It's really just the case of defining the same variable twice and has nothing to do with boost. It's just like this:

hello1.cxx

int hello;

main.cxx

int hello;
int main (){return 1}

See the answer by Michael for how to deal with it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文