1

The function getdata() works fine without the template but when I put the template in I get this error

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2019 unresolved external symbol "public: char const * __thiscall json_map::getdata<char const *>(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??$getdata@PBD@json_map@@QAEPBDV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main    banking 

struct declaration in header file:

struct json_map {

    std::map<std::string, std::any> data;

    template<typename A>
    A getdata(std::string key);

};

function declaration:

template<typename A>
A json_map::getdata(std::string key)
{
    return std::any_cast<A>(data[key]);
}

function use:

map1.getdata<std::string>("walid");
Walid Zein
  • 31
  • 2
  • You can't define templated functions in a separate `*.cpp` file, that would [require forward declaration which is limited in its own right](https://stackoverflow.com/questions/115703/storing-c-template-function-definitions-in-a-cpp-file). Try moving the definition in the header file itself. – Ruks Jul 10 '20 at 04:11
  • Is the function definition in a separate file? Templated functions should be in the header, visible to all files where it's used – LHLaurini Jul 10 '20 at 04:12
  • I moved the function definition to the header file and it worked thank you all – Walid Zein Jul 10 '20 at 04:23
  • you can do something like this `#include "implimentatoin.inc"` at the of the template header file. – foragerDev Jul 10 '20 at 08:54

0 Answers0