对成员函数和变量的未定义引用
Undefined reference to member function and variable
我有以下头文件 MappingSingleton.h:
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include<iterator>
#include <sstream>
#include <thread>
#include <mutex>
#include "SOPExpr.h"
class MappingSingleton {
private:
static MappingSingleton& mapping_singleton;
MappingSingleton();
public:
static unsigned int wireID;
static bool is_init;
std::vector<SOPExpr> m_hex_to_SOP_4;
std::vector<int> m_hex_to_line_number_4;
std::vector<SOPExpr> m_hex_to_SOP_3;
std::vector<int> m_hex_to_line_number_3;
std::vector<SOPExpr> m_hex_to_SOP_2;
std::vector<int> m_hex_to_line_number_2;
void loadLUTNHexToStringMapping(std::vector<SOPExpr>& m_hex_to_SOP, std::vector<int> &m_hex_to_line_number, short int LUT_size);
std::string getQTPrims(std::string& LUT_init_string);
static MappingSingleton getInstance();
private:
static void initSingleton();
static std::once_flag init_instance_flag;
int getExprIndex(std::string &key, short lut_size);
int wire_id;
};
以及 MappingSingleton.cpp 中的以下函数:
MappingSingleton MappingSingleton::getInstance() {
std::call_once(MappingSingleton::init_instance_flag,&MappingSingleton::initSingleton);
return mapping_singleton;
}
编译器给出以下错误:
/grid/cva/p4_02/hisham/gcc/ua/Framework/Roman/src/MappingSingleton.cpp:37: undefined reference to `MappingSingleton::initSingleton()'
/grid/cva/p4_02/hisham/gcc/ua/Framework/Roman/src/MappingSingleton.cpp:37: undefined reference to `MappingSingleton::init_instance_flag'
/grid/cva/p4_02/hisham/gcc/ua/Framework/Roman/src/MappingSingleton.cpp:38: undefined reference to `MappingSingleton::mapping_singleton'
我不明白为什么看到我在标题中清楚地定义了这些变量/函数。我尝试阅读有关此错误的许多问题,但没有一个建议的解决方案可以解决该错误。我的主函数只是初始化映射单例。
MappingSingleton.cpp具有以下实现:
#include "MappingSingleton.h"
#include "HelperFunction.h"
#include <regex>
#include <cmath>
#include "WireNameReplacement.h"
bool MappingSingleton::is_init=false; //this is static in the header file
std::once_flag init_instance_flag;
MappingSingleton::MappingSingleton() {
wire_id=0; //used for avoiding duplicate wires.
//Only one mapping generator at a time. Singleton design pattern.
//Load mappings for the three LUTS
loadLUTNHexToStringMapping(m_hex_to_SOP_4,m_hex_to_line_number_4,4);
loadLUTNHexToStringMapping(m_hex_to_SOP_3,m_hex_to_line_number_3,3);
loadLUTNHexToStringMapping(m_hex_to_SOP_2,m_hex_to_line_number_2,2);
}
MappingSingleton MappingSingleton::getInstance() {
std::call_once(MappingSingleton::init_instance_flag,&MappingSingleton::initSingleton);
return mapping_singleton;
}
void MappingSingleton::loadLUTNHexToStringMapping(std::vector<SOPExpr>& m_hex_to_SOP, std::vector<int> &m_hex_to_line_number, short int LUT_size){
//Read in Vivado Hex key of each SOP for LUT 4
std::ifstream iFS("TextAndPythonFiles/LUT" +std::to_string(LUT_size)+"/lutListHex");
std::string temp_str;
m_hex_to_line_number.resize(pow(2,pow(2,LUT_size))); // 2^(2^4) for 4-LUT
int tempInt=0;
std::stringstream ss;
while (std::getline(iFS, temp_str, 'n')) {
m_hex_to_line_number[hexToInteger(temp_str)]=tempInt;
++tempInt;
}
//Read In the SOP Strings from a file. Reserve vector space first though to avoid reallocating vector.
//m_hex_to_SOP.resize(pow(2,pow(2,LUT_size)));
std::ifstream iFS2("TextAndPythonFiles/LUT"+std::to_string(LUT_size)+"/combinedTrimmedFinal.vg");
int tempInt2=0;
std::string current_SOP;
while (std::getline(iFS2, temp_str, 'n')) {
if(temp_str.empty()) //Blank line implies new module
{
if(!current_SOP.empty()) current_SOP.pop_back(); //Erase last new line. Not necessary.
SOPExpr expr(current_SOP);
m_hex_to_SOP.push_back(expr);
current_SOP.clear();
continue;
}
current_SOP.append(temp_str);
current_SOP.push_back('n');
tempInt2++;
}
// createHexVec(line_num_to_SOP,m_hex_to_line_number); Reviewers please ignore
std::string test="Q_OAI222 g514(.A0 (n_5), .A1 (n_0), .B0 (n_4), .B1 (n_1), .C0 (n_2), .C1 (n_3), .Z (n_6));n Q_INV g517(.A (i0), .Z (n_5));Q_INV g518(.A (i2), .Z (n_4));";
//std::cout<<findOutputWire(test);
}
std::string MappingSingleton::getQTPrims(std::string& vivado_LUT_string)
{
//find LUT Size
std::smatch match;
std::regex re("LUT([0-9])");
regex_search(vivado_LUT_string, match, re);
int LUT_size=std::stoi(match.str(1));
std::string key= extractHexKey(vivado_LUT_string);
int expr_index;
std::string qt_prims;
std::deque<std::string> d;
switch(LUT_size){
case 2: expr_index= getExprIndex(key,2);
qt_prims= m_hex_to_SOP_4[expr_index].getGateLevelNetList();
d=findWireNames(vivado_LUT_string);
qt_prims=replaceWireNames(d,vivado_LUT_string,qt_prims,2);
replaceAllInternalWires(qt_prims,wire_id);
return qt_prims;
case 3: expr_index= getExprIndex(key,3);
qt_prims= m_hex_to_SOP_4[expr_index].getGateLevelNetList();
d=findWireNames(vivado_LUT_string);
qt_prims= replaceWireNames(d,vivado_LUT_string,qt_prims,3);
replaceAllInternalWires(qt_prims,wire_id);
return qt_prims;
case 4: expr_index= getExprIndex(key,4);
qt_prims= m_hex_to_SOP_4[expr_index].getGateLevelNetList();
d=findWireNames(vivado_LUT_string);
qt_prims=replaceWireNames(d,vivado_LUT_string,qt_prims,4);
replaceAllInternalWires(qt_prims,wire_id);
return qt_prims;
default:
throw std::invalid_argument("Invalid LUT Init Val");
}
return "Invalid LUT Init Val";
}
int MappingSingleton::getExprIndex(std::string& key, short int LUT_size){
switch(LUT_size){
case 2: return m_hex_to_line_number_2[hexToInteger(key)];
case 3: return m_hex_to_line_number_3[hexToInteger(key)];
case 4: return m_hex_to_line_number_4[hexToInteger(key)];
default:
throw std::invalid_argument("Invalid LUT Init Val");
}
}
所以,首先,这是错误的:
std::once_flag init_instance_flag;
应该是这个:
std::once_flag MappingSingleton::init_instance_flag; // maybe an init value here too
其次,即使你声明了MappingSingleton::mapping_singleton
和MappingSingleton::initSingleton()
,你也没有对它们的定义。它最终将看起来像这样:
// the actual definition of the variable
MappingSingleton& MappingSingleton::mapping_singleton;
// the actual implementation of the function
void MappingSingleton::initSingleton() {
// some implementation...
}
代码中缺少它,并且是可能导致错误的原因。
所以这个
std::once_flag init_instance_flag;
应该是这个
std::once_flag MappingSingleton::init_instance_flag;
另外两个项目initSingleton()
和mapping_singleton
并且没有在我能看到的任何地方定义
头文件中的内容是声明而不是定义。你也需要一个定义。
我也会把mapping_singleton
改成一个指针。鉴于参考的限制,很难看出它作为参考有任何好处。
更新
大概您已经mapping_singleton
引用,以便您只能在需要时创建。像这样的东西
MappingSingleton& MappingSingleton::mapping_singleton;
void MappingSingleton::initSingleton()
{
mapping_singleton = *new MappingSingleton();
}
但这不起作用,因为您无法分配给引用。引用在初始化时被绑定,以后无法重新绑定。上面的赋值将新创建的 MappingSingleton 复制到引用已绑定到的 MappingSingleton 对象(该对象不存在)。
相反,以下作品
MappingSingleton* MappingSingleton::mapping_singleton = nullptr;
void MappingSingleton::initSingleton()
{
mapping_singleton = new MappingSingleton();
}
MappingSingleton& MappingSingleton::getInstance() {
std::call_once(MappingSingleton::init_instance_flag,&MappingSingleton::initSingleton);
return *mapping_singleton;
}
请注意,我将getInstance
的返回类型更改为引用。在那里有一个非引用并不违法,但这意味着当你调用getInstance
时,你的单例对象将被复制,这反而违背了拥有单例的目的。
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 对 Scalar ::Scalar() 的未定义引用
- 对复制 CTOR 和 CTOR 的未定义引用
- 对显式实例化的模板函数的未定义引用
- TensorRT (C++ API) 对"createNvOnnxParser_INTERNAL"的未定义引用
- 2个模板化类的非模板友元函数未定义引用错误
- 编译 libfluid 样本控制器时对"event_base_del_virtual"的未定义引用
- 获取对function_name的未定义引用
- 对 'std::thread::_M_start_thread CMake 的未定义引用进行基准测试
- 对结构方法的未定义引用
- 使用内联函数 c++ 的未定义引用
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 对 DLOPEN 的未定义引用
- QT C++中对全局变量的未定义引用
- 快速数学导致对"__pow_finite"的未定义引用
- 对 boost::system::d etail::system_category_instance 的未定义引用,从
- OpenCV 3.4.3 中对 'cv::String::d eallocate()' 错误的未定义引用