0

Minimalish reproducible

I have two functions: but calling like this:

pack<int>(some_vector, something_else)

some_vector would be like this:

std::vector<int> some_vector;

throws a compilation error:

LNK2019 Unresolved external symbol in main.obj

Both the functions are defined, and calling pack with an int instead of a vector compiles just fine.
I want to add more pack functions to accept maps, tuple's etc (as first argument). Is this the correct way of going about this? (I am not that experienced with 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, byte::container& dest, 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!");
    }
}
t348575
  • 674
  • 8
  • 19
  • Those are declarations. Where have you implemented the functions? – Stephen Newell Dec 26 '20 at 18:38
  • Ok I'll include the implementations as well – t348575 Dec 26 '20 at 18:38
  • If I see `template` and `unresolved external symbol` appearing together in a c++ question, the most probable reason is that [this](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) applies as an answer. You can edit your question in case that it doesn't. – πάντα ῥεῖ Dec 26 '20 at 18:39
  • You should just give us the entire contents of a .cpp file I can throw into my compiler that reproduces the error, don't you think? Complete with #includes. Make it a stand-alone, self-contained repro of the problem. – Wyck Dec 26 '20 at 18:41
  • @Wyck I've added a minimal reproducible – t348575 Dec 26 '20 at 18:47

0 Answers0