0

I have a class that reads a file and stores the values found in a map, in memory. I'v declared a member function using templates, so that I can extract any value from a string.

ConfigReader.h

class ConfigReader
{
    private:
        More Code...
    public:
        ...
        template<typename vType>
        vType GetValue(const char * key);
};

ConfigReader.cpp

...

template<typename vType>
vType ConfigReader::GetValue(const char * key)
{
    stringstream buffer(GetStringValue(key));

    vType extrValue;
    buffer >> extrValue;

    if(!buffer)
        throw invalid_argument("Failed to extract the value!");

    return extrValue;
}

Main.cpp

int main()
{
    ...

    try{
        cout << "Encryption: " << boolalpha << config.GetValue<bool>("use_enc") << endl;

    }catch(invalid_argument err){
        cerr << "Error: " << err.what() << endl;

    }
}

I get the following link error: 1>Main.obj : error LNK2019: unresolved external symbol "public: bool __thiscall ConfigReader::GetValue<bool>(char const *)" (??$GetValue@_N@ConfigReader@@QAE_NPBD@Z) referenced in function _main

1>..\Visual Studio 2017\Projects\...\Debug\FilePhraser.exe : fatal error LNK1120: 1 unresolved externals

Wich means that the function GetValue<bool>() hasn't been found.

Could you please explain me why the linker can't find that function ?

Kolin Verdum
  • 71
  • 1
  • 8

1 Answers1

0

Because you haven't instantiated a function. Append vType ConfigReader::GetValue<bool>(const char * key); into the ConfigReader.cpp, and linker will find it.

A.N.
  • 278
  • 2
  • 13