Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using std::pair when I try to insert integer data as value it is working well but when I try to use string data as value it is not working details below
```
#define __CONFIGSERVICE_H__
#include "toml.hpp"
#include <typeinfo>
#include <iostream>
#include <ctype.h>
#include <cstring>
#include <vector>
#include <bits/stdc++.h>
#include <boost/algorithm/string.hpp>
#include <variant>
#include "configservice.hpp"
using namespace std;

ConfigService config;

template <typename valueType, typename objectType>
void testing(const std::string &arrName, valueType &var)
{
    try
    {
        toml::value allData = toml::parse("../data.toml");
        for (const auto &obj : allData.as_array())
        {
            objectType newObj;
            for (const auto &[k, v] : obj.as_table())
            {
                std::unordered_map<std::string, variant<int, float, bool, string, vector<int>, vector<string>, vector<bool>>> newObj;
                if (v.is_array())
                {
                    for (const auto &item : v.as_array())
                    {
                        if (item.is_integer())
                        {
                            std::pair<std::string, vector<int>> object(config.ParseToString(k), item.as_integer());
                            newObj.insert(object);
                        }
                        else if (item.is_string())
                        {
                            std::pair<std::string, vector<std::string>> object(k, item.as_string());
                            newObj.insert(object);
                        }
                    }
                }
            }
        }
    }
    catch (const exception &err)
    {
        cout << err.what() << endl;
    }
}
void test()
{
    std::vector<std::unordered_map<std::string, variant<int, float, bool, string, vector<int>, vector<string>, vector<bool>>>> var;
    testing<std::vector<std::unordered_map<std::string, variant<int, float, bool, string, vector<int>, vector<string>, vector<bool>>>>, std::unordered_map<std::string, variant<int, float, bool, string, vector<int>, vector<string>, vector<bool>>>>("system.camera", var);

    for (const auto &element : var)
    {
        for (auto it = element.begin(); it != element.end(); ++it)
        {
            std::cout << it->first << '\n';
        }
    }
}

int main(int argc, char const *argv[])
{
    test();
    return 0;
}
```

Traceback
```
.
.
      |                                                                        ^~~~~~
In file included from /usr/include/c++/9/bits/stl_algobase.h:64,
                 from /usr/include/c++/9/bits/char_traits.h:39,
                 from /usr/include/c++/9/ios:40,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from /home/jocefyne/workspace/reactVision/rv_utils/ConfigService/main.cpp:2:
/usr/include/c++/9/bits/stl_pair.h:229:26: note: candidate: ‘template<class _U1, class _U2, typename std::enable_if<std::__and_<std::__is_implicitly_default_constructible<_U1>, std::__is_implicitly_default_constructible<_U2> >::value, bool>::type <anonymous> > constexpr std::pair<_T1, _T2>::pair()’
  229 |       _GLIBCXX_CONSTEXPR pair()
      |                          ^~~~
/usr/include/c++/9/bits/stl_pair.h:229:26: note:   template argument deduction/substitution failed:
In file included from /home/jocefyne/workspace/reactVision/rv_utils/ConfigService/main.cpp:3:
/home/jocefyne/workspace/reactVision/rv_utils/ConfigService/configservice.hpp:186:72: note:   candidate expects 0 arguments, 2 provided
  186 |                                 std::pair<std::string, vector<string>> object(ParseToString(k), item.as_string());
      |                                                                        ^~~~~~
make[2]: *** [CMakeFiles/ConfigService.dir/build.make:76: CMakeFiles/ConfigService.dir/main.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [CMakeFiles/Makefile2:100: CMakeFiles/ConfigService.dir/all] Error 2
make: *** [Makefile:136: all] Error 2
```
### system.camera returns 
```
[[]]
vendor_name = "Raspberry"
fps = 24
supported_res = [
1280,
720,
]
active_algos = [
"motion_detection",
]
cap_res = [
1280,
720,
]
hw_type = "usb"
broadcast_port = 4700
hw_path = "/dev/video0"
id = "cam1"

[ip]

[[ip.motion_detection]]
dilate_iteration = 2
threshold_type = 0
threshold_max = 255
threshold = 25
gaussian_blur_mat_size = 21
active = true
countour_max_area = 200
id = "cam1"
```

There is a simple example I have written and got the same error if u know about it then let me know. I am using TOML variant pairs vectors and a lot more you can check all headers.

When i use ```std::pair<std::string, vector<int>>....``` it is working but at the same time if i use ```std::pair<std::string, vector<string>>``` it is not working as i want


What I have tried:

std::variant - cppreference.com[^]
Posted
Comments
Richard MacCutchan 5-Jul-22 5:03am    
/home/jocefyne/workspace/reactVision/rv_utils/ConfigService/configservice.hpp:186:72: note:   candidate expects 0 arguments, 2 provided

You need to look at the line referenced in this error message.
Richard MacCutchan 5-Jul-22 5:14am    
"When i use ```std::pair<std::string, vector<int>>....``` it is working but at the same time if i use ```std::pair<std::string, vector<string>>``` it is not working as i want"
The following code works fine for both types:
vector<int> v1 = { 1, 3, 5 };
vector<string> v2 = { "foo", "bar", "poo" };
pair<string, vector<int>> p1("numbers", v1);
pair<string, vector<string>> p2("strings", v2);
cout << p1.first << " " << p1.second[0] << endl;
cout << p2.first << " " << p2.second[1] << endl;

You need to show your "simple" example.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900