C++ LNK1120 1 unverifiable external reference. There's an error. Why is that?

Asked 6 days ago, Updated 6 days ago, 1 views

Huffman.cpp

#include "Huffman.h"

bool Node::operator()(const Node* lhs, const Node* rhs) const {
    return lhs->cnt > rhs->cnt;
}

Huffman::~Huffman() {
    std::stack<Node*> st;
    st.push(binTree);
    while (!st.empty()) {
        Node* cur = st.top();
        st.pop();
        if (cur->left)
            st.push(cur->left);
        if (cur->right)
            st.push(cur->right);
        delete cur;
    }
    binTree = nullptr;
}

void Huffman::_getCode(const std::string& input) {
    std::string code = "";
    std::map<char, std::string> m;
    std::stack<std::pair<Node*, std::string>> st;
    st.push({ binTree, "" });
    while (!st.empty()) {
        Node* cur = st.top().first;
        std::string tmp = st.top().second;
        st.pop();
        if (cur->right != nullptr) {
            st.push({ cur->right, tmp + "1" });
        }
        if (cur->left != nullptr) {
            st.push({ cur->left, tmp + "0" });
        }
        if (cur->left == nullptr && cur->right == nullptr) {
            m[cur->ch] = tmp;
        }
    }
    for (const char& c : input) {
        code += m[c];
    }
    this->code = code;
    return;
}

std::string Huffman::getCode() const {
    if (code == "") {
        std::cout << "Encoding First !\n";
    }
    return code;
}

void Huffman::encoding(const std::string& input) {
    std::cout << "Input String : " << input << "\n";
    // // count
    std::map<char, int> m;
    for (auto& c : input) {
        m[c]++;
    }
    // // push into pq
    std::priority_queue<Node*, std::vector<Node*>, Node> pq;
    for (auto& e : m) {
        pq.push(new Node(e.first, e.second));
    }
    while (pq.size() > 1) {
        Node* left = pq.top();
        pq.pop();
        Node* right = pq.top();
        pq.pop();
        Node* newNode = new Node(left, right);
        pq.push(newNode);
    }
    binTree = pq.top();
    pq.pop();
    //convert tree to huffman code
    _getCode(input);
    return;
}

std::string Huffman::decoding() const {
    if (binTree == nullptr) {
        std::cout << "Encoding First !\n";
        return "";
    }
    std::string ret = "";
    const Node* cur = binTree;
    for (const char& c : code) {
        if (c == '0') {
            cur = cur->left;
        }
        else {
            cur = cur->right;
        }
        if (cur->left == nullptr && cur->right == nullptr) {
            ret += cur->ch;
            cur = binTree;
        }
    }
    return ret;
}

Huffman.h


#ifndef HUFFMAN
#define HUFFMAN
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <iostream>

class Node {
public:
    Node() : left(nullptr), right(nullptr), cnt(0), ch(0) {}
    Node(char ch, int cnt) : left(nullptr), right(nullptr), cnt(cnt), ch(ch) {}
    Node(Node* left, Node* right) : left(left), right(right), cnt(left->cnt + right->cnt), ch(0) {}
    bool operator()(const Node* lhs, const Node* rhs) const;
    Node* left;
    Node* right;
    uint32_t cnt;
    char ch;
};

class Huffman {
private:
    Node* binTree;
    std::string code;
    void _getCode(const std::string& input);
public:
    Huffman() : binTree(nullptr), code("") {}
    ~Huffman();
    void encoding(const std::string& input);
    std::string getCode() const;
    std::string decoding() const;
};
#endif

When executed, an error appears on the first line.

c++ huffman

2022-09-20 14:37

1 Answers

LNK1120 is one of the errors that occurs during the linking process when external references are incorrect.

In conclusion, we wrote a code that calls a specific function of the library referred to as include, which was caused by the failure to link the actual code (usually a static library file) in which the function was implemented.

Compiling involves compiling multiple source code files, converting them into a target (.o file), and then linking files in a reference relationship on each source code to create a single executable file.

There was no grammar error when compiling individual source codes, but there was something missing in the process of bringing each result together.

Please refer to the static library file, which is usually in libXXX.a format, and it will be solved.


2022-09-20 14:37

If you have any answers or tips


© 2022 pinfo. All rights reserved.