Search code examples
c++nuget-packagefstreamjsoncpp

Getting an unresolved external symbol when using jsoncpp NuGet package


I have this simple piece of code where I am using the jsoncpp version 0.6.0.1 NuGet package to read from a json file in VS2013.

#include <iostream>
#include <fstream>
#include "json\json.h"
using namespace std;

Json::Value readJson(){
    Json::Value root;
    ifstream rootfile("restore.json");
    rootfile >> root;
    return root;
}

void main(){

    Json::Value root = readJson();
    cout << "Hello" << endl;
    system("pause");

}

I keep getting the error Error 1 error LNK2019: unresolved external symbol "class std::basic_istream<char,struct std::char_traits<char> > & __cdecl Json::operator>>(class std::basic_istream<char,struct std::char_traits<char> > &,class Json::Value &)" (??5Json@@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV12@AAVValue@0@@Z) referenced in function "class Json::Value __cdecl readJson(void)" (?readJson@@YA?AVValue@Json@@XZ)

The problem is with the operator>>. So I uncommented rootfile >> root and the error went away. But I do not understand why that line is a problem. I added the jsoncpp.lib and it's path to the linker. I am not sure what part I am missing now.


Solution

  • The problem was that with the jsoncpp version 0.6.0.1 there is no Json::operator>> function in the reader file, as a result of which I was getting the above error. I replaced rootfile >> root with the following code and that solved my problem.

    Json::Reader reader;
    bool ok = reader.parse(rootfile, root, true);