未定义对成员函数和变量的引用

use*_*940 2 c++

我有以下头文件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;

};
Run Code Online (Sandbox Code Playgroud)

以及MappingSingleton.cpp中的以下函数:

MappingSingleton MappingSingleton::getInstance() {
    std::call_once(MappingSingleton::init_instance_flag,&MappingSingleton::initSingleton);
    return mapping_singleton;
}
Run Code Online (Sandbox Code Playgroud)

编译器给出以下错误:

/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'
Run Code Online (Sandbox Code Playgroud)

我不明白为什么要在标题中清楚地定义这些变量/函数。我尝试阅读有关此错误的许多问题,但是没有提出的解决方案可以解决该错误。我的主要功能只是初始化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");
    }
}
Run Code Online (Sandbox Code Playgroud)

Chi*_*ter 5

因此,首先,这是错误的:

std::once_flag init_instance_flag;
Run Code Online (Sandbox Code Playgroud)

应该是这样的:

std::once_flag MappingSingleton::init_instance_flag; // maybe an init value here too
Run Code Online (Sandbox Code Playgroud)

其次,即使您声明了MappingSingleton::mapping_singletonMappingSingleton::initSingleton(),也没有针对它们的定义。最终它将看起来像这样:

// the actual definition of the variable
MappingSingleton& MappingSingleton::mapping_singleton;

// the actual implementation of the function
void MappingSingleton::initSingleton() {
   // some implementation...
}
Run Code Online (Sandbox Code Playgroud)

这是您的代码中缺少的,可能是导致您的错误的原因。

  • OP的一个问题是,它们的单例对象实际上是引用。由于在定义了引用之后就不可能绑定引用,因此最好将其实现为指针。 (2认同)