C++ 模板和函数重载 Minimalish reproducible

问题描述

Minimalish reproducible

我有两个功能:但是这样调用

pack<int>(some_vector,something_else)

some_vector 应该是这样的:

std::vector<int> some_vector;

抛出编译错误

LNK2019 main.obj 中未解析的外部符号

两个函数都已定义,使用 int 而不是 vector 调用 pack 编译得很好。
我想添加更多的包函数来接受映射、元组等(作为第一个参数)。这是解决这个问题的正确方法吗? (我对 C++ 没有经验)


template <typename T>
void pack(std::vector<T>& src,byte::container& dest,bool initial) {
    size_t n = src.size();
    size_t s = 0;
    uint8_t prefix;
    uint16_t post_prefix = n;
    bool use_post_prefix = false;
    if (n <= 15) {
        prefix = fixarray_t(n);
    }
    else if (n <= max16) {
        prefix = arr16;
        use_post_prefix = true;
    }
    else if (n <= max32) {
        prefix = arr32;
        use_post_prefix = true;
    }
    if (initial) {
        dest.clear_resize(int(n * 0.4));
    }
    dest.push_back(&prefix);
    if (use_post_prefix) {
        dest.push_back(&post_prefix);
    }
    for (uint32_t i = 0; i < n; i++) {
        pack(src[i],dest,false);
    }
}

void pack(uint64_t src,bool initial) {
    uint8_t prefix;
    if (src <= max4) {
        prefix = ufixint_t(src);
        dest.push_back(&prefix);
    }
    else if (src <= max8) {
        prefix = uint8;
        dest.push_back(&prefix);
        dest.push_back(uint8_t(src));
    }
    else if (src <= max16) {
        prefix = uint16;
        dest.push_back(&prefix);
        dest.push_back(uint16_t(src));
    }
    else if (src <= max32) {
        prefix = uint32;
        dest.push_back(&prefix);
        dest.push_back(uint32_t(src));
    }
    else if (src <= max64) {
        prefix = uint64;
        dest.push_back(&prefix);
        dest.push_back(uint64_t(src));
    }
    else {
        throw std::range_error(std::to_string(src) + " out of range!");
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)