Help with creatng a templated function to accept JSON arrays? #4437
-
I'm currently trying to create a function within a namespace that allows me to convert a passed in nlohmann/json.hpp object that is an array to a C++ array of typename T. Here is what I have done so far in my own JsonConvertible.cpp: ` using json = nlohmann::json; // namespace JsonConvertible
}; When I attempt to use the namespace in my main.cpp, I get an error "no instance of function template "jc::jsonToArray" matches the argument list..." then gives me a list of everything in nlohmann::json_abi_v3_all_3::basic_json. Here is some sample code: `#include <"iostream"> int main() {
}` I will admit my understanding of templates is pretty basic. I'm just trying to use this library with a simple program of mine, and I've just been spending some time on what I can do with it. Any help would be greatly appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Your major problem is that you aren't doing anything to specify the type of the array you're reading in You have a variety of other errors that may just be that you haven't finished filling things in, such as allocating a single element of type Some of those are fixed here: |
Beta Was this translation helpful? Give feedback.
Your major problem is that you aren't doing anything to specify the type of the array you're reading in
jsonToArray
. Since you are not taking in any parameters that have anything to do with the typeT
, you need to specify it as injc::jsonToArray<int>(...
.You have a variety of other errors that may just be that you haven't finished filling things in, such as allocating a single element of type
T
rather than an array ofT
, you aren't filling in the array, and you aren't doing anything with the returned array.Some of those are fixed here:
https://www.godbolt.org/z/9d3qPTbWW