C++ LNK2005 已在 B_calculating.obj 中定义

C++ LNK2005 already defined in B_calculating.obj

错误:

LNK2005 "private: static char const * const boost::json::key_value_pair::empty_" (?empty_@key_value_pair@json@boost@@0QBDB) already defined in B_calculating.obj
LNK2005 "private: static struct boost::json::object::table boost::json::object::empty_" (?empty_@object@json@boost@@0Utable@123@A) already defined in B_calculating.obj
LNK2005 "private: static struct boost::json::array::table boost::json::array::empty_" (?empty_@array@json@boost@@0Utable@123@A) already defined in B_calculating.obj

等等... 这些错误大约有一百个,它们都与 boost json

有关

在问这个问题之前,我几乎花了一整天的时间阅读和搜索这方面的资料。似乎我已经尝试了所有方法,但错误仍然存​​在。我有几个文件:

main.cpp:

#include <stdlib.h>
#include <iostream>
#include <mysql/jdbc.h>
#include <boost/json/src.hpp>
#include <boost/json.hpp>
#include <typeinfo>
#include <string>
#include <vector>
#include <math.h>
#include <B_calculating.h>
#include <Level.h>
#include <string>
using namespace std;

//here using B_calculating
//Unfortunately, you won't be able to fully reproduce the example, as it requires data from the database and so on.
....

B_calculating.h:

#pragma once
#include <vector>
#include <boost/json/src.hpp>
#include <boost/json.hpp>
#include <mysql/jdbc.h>
#include <Level.h>

class B_calculating
{
public:
    std::vector<Level> depth;
    sql::ResultSet* sql_result_query;
    const int parameter_value;
    const float step;
    B_calculating(sql::ResultSet* sql_result, int parameter, float input_step);
    void start_processing();
private:
    void request_processing(boost::json::value const& jv);
    void set_level_and_custom_size(Level& l, float zero_level_price);
    void calculate_levels_and_custom_size();
};

B_calculating.cpp:

#include <Q_calculating.h> 
using namespace std;
// here I define functions from .h file

Level.h 具有相似的结构并且工作正常。

boost/json/src.hpp中有线索header:

This file is meant to be included once, in a translation unit of the program.

您应该只在 one cpp file 中包含 boost/json/src.hpp。您应该将其从 B_calculating.h 中删除,仅将其包含在 main.cpp.