Trouble with insert() and push_back() in C++

Help
Anonymous
2013-08-28
2013-09-17
  • Anonymous

    Anonymous - 2013-08-28

    Hello,

    I have been using libjson for a few months now with great success. Up until this point, I have been mostly reading JSON and generating documentation from it. Recently, it has come up that I am going to have to modify the JSON tree directly. Now, I have coded the appropriate functions, however any calls to insert() or push_back() fail at the linking step. Below are the error messages I receive:

    Using insert():

    /src/main.cpp:436: undefined reference to `JSONNode::insert(JSONNode::iterator, JSONNode const&)'
    

    Using push_back():

    /usr/local/include/libjson/_internal/Source/JSONNode.h:716: undefined reference to `internalJSONNode::push_back(JSONNode const&)'
    

    The offending lines are super simple, and I have tried every possible combination of parameters and JSONOptions.h defines that I can think of. For completeness, the offending lines (only one is used at a time):

    toNode.insert(toNode.end(), *sourceIter);
    toNode.push_back(*sourceIter);
    

    I have tried every combination of consts and non-consts that are possible. I notice that this error occurs after all compilation has completed and linking is being handled. What really worries me is that the error in push_back() is actually within one of the libjson source files. As I mentioned before, all of the other library functions that I have been using still work without issue. I am definitely open to the idea that I am doing it wrong, but I am stumped as to what that wrong may be. My JSONOptions.h file (comments removed for brevity):

    #ifndef JSON_OPTIONS_H
    #define JSON_OPTIONS_H
    
    //#define JSON_LIBRARY
    //#define JSON_STRICT
    //#define JSON_DEBUG
    #define JSON_ISO_STRICT
    #define JSON_SAFE
    //#define JSON_STDERROR
    //#define JSON_PREPARSE
    //#define JSON_LESS_MEMORY
    //#define JSON_UNICODE
    #define JSON_REF_COUNT
    #define JSON_BINARY
    #define JSON_EXPOSE_BASE64
    #define JSON_ITERATORS
    #define JSON_STREAM
    //#define JSON_MEMORY_CALLBACKS
    //#define JSON_MEMORY_MANAGE
    //#define JSON_MEMORY_POOL 524288
    //#define JSON_MUTEX_CALLBACKS
    //#define JSON_MUTEX_MANAGE
    //#define JSON_NO_C_CONSTS
    //#define JSON_OCTAL
    #define JSON_WRITE_PRIORITY MED
    #define JSON_READ_PRIORITY HIGH
    //#define JSON_NEWLINE "\r\n"
    //#define JSON_INDENT "    "
    #define JSON_ESCAPE_WRITES
    #define JSON_COMMENTS
    //#define JSON_WRITE_BASH_COMMENTS
    //#define JSON_WRITE_SINGLE_LINE_COMMENTS
    //#define JSON_ARRAY_SIZE_ON_ONE_LINE 2
    #define JSON_VALIDATE
    #define JSON_CASE_INSENSITIVE_FUNCTIONS
    //#define JSON_INDEX_TYPE unsigned int
    //#define JSON_BOOL_TYPE char
    //#define JSON_INT_TYPE long
    //#define JSON_NUMBER_TYPE double
    //#define JSON_STRING_HEADER "../TestSuite/StringTest.h"
    //#define JSON_UNIT_TEST
    //#define JSON_NO_EXCEPTIONS
    #define JSON_DEPRECATED_FUNCTIONS
    #define JSON_CASTABLE
    #define JSON_SECURITY_MAX_NEST_LEVEL 128
    #define JSON_SECURITY_MAX_STRING_LENGTH 33554432
    #define JSON_SECURITY_MAX_STREAM_OBJECTS 128
    
    #endif
    

    I have tried a number of different build strategies with the various options to try to resolve the error, all to no avail. My application is being built on a native Linux box, using libjson revision 7.6.1. Please let me know if there are any additional details that I may provide.

    Thank you for any assistance.

     
  • Jovkin

    Jovkin - 2013-09-17

    libjson was a big help to me so far in many projects. Today I stumbled upon a weird behavior that I cannot explain.

    This C++ code snippled fills a JSON array with values from a database and prints the result with write() and write_formatted():


    JSONNode dnode(JSON_ARRAY);
    dnode.set_name("data");

    for (int i = 0; i < data.size()/num_cols; i++) {

    JSONNode element = libjson::parse(data[num_cols*i+2]);
    element.push_back(JSONNode("__ID__",data[num_cols*i]));
    dnode.push_back(element);
    

    }

    cout << dnode.write() << endl;
    cout << dnode.write_formatted() << endl;


    Both output results are significantly different with a missing property "ID" for write():


    [{"item":"texttext"},{"item":"12345678"},{"item":"input"}]

    {
    "ID" : "165",
    "item" : "texttext"
    },
    {
    "ID" : "166",
    "item" : "12345678"
    },
    {
    "ID" : "167",
    "item" : "input"
    }


    Anyone has an explanation?

     

Log in to post a comment.