c++pugixml

How do i pass a char into find_child_by_attribute for pugixml


i'm trying to pass a value from a file into tileNode.find_child_by_attribute("tileset","firstgid",container[0]).attribute("source").as_string(). it should return a string for an image path.

#include <iostream>
#include <vector>
#include "SFML/Graphics.hpp"
#include "../hpp/pugixml.hpp"

int main()
{
    std::vector <char> container = {};
    pugi::xml_document document;
    pugi::xml_parse_result result = document.load_file("./room/default.tsx");
    pugi::xml_node tileNode = document.child("map").child("tiledata");
    std::string gridRawData = document.child("map").child("layer").child("data").text().as_string();
    int z = 0;
    int y = 0;
    //read map key from file
    for(int x = 0; x < gridRawData.size(); x++)
    {
        if(document.child("map").child("layer").child("data").child_value()[x] != '\n' &&
            document.child("map").child("layer").child("data").child_value()[x] != ',')
        {
           container.push_back(gridRawData[x]);
        };
    }




    std::cout<<tileNode.find_child_by_attribute("tileset","firstgid",container[0]).attribute("source").as_string();

    return 0;
};```

it gives error:
```it gives error
||=== Build: Release in projectrpg (compiler: GNU GCC Compiler) ===|
C:\Users\Nyanners\Desktop\gamedevhell\projectrpg\projectrpg\cpp\main.cpp||In function 'int main()':|
C:\Users\Nyanners\Desktop\gamedevhell\projectrpg\projectrpg\cpp\main.cpp|17|warning: comparison between signed and unsigned integer expressions [-Wsign-compare]|
C:\Users\Nyanners\Desktop\gamedevhell\projectrpg\projectrpg\cpp\main.cpp|29|error: invalid conversion from '__gnu_cxx::__alloc_traits<std::allocator<char> >::value_type {aka char}' to 'const char_t* {aka const char*}' [-fpermissive]|
C:\Users\Nyanners\Desktop\gamedevhell\projectrpg\projectrpg\hpp\pugixml.hpp|648|note:   initializing argument 3 of 'pugi::xml_node pugi::xml_node::find_child_by_attribute(const char_t*, const char_t*, const char_t*) const'|
C:\Users\Nyanners\Desktop\gamedevhell\projectrpg\projectrpg\cpp\main.cpp|14|warning: unused variable 'z' [-Wunused-variable]|
C:\Users\Nyanners\Desktop\gamedevhell\projectrpg\projectrpg\cpp\main.cpp|15|warning: unused variable 'y' [-Wunused-variable]|
||=== Build failed: 1 error(s), 3 warning(s) (0 minute(s), 2 second(s)) ===|```



Solution

  • So taking a wild guess at what you actually mean I'm going to plump for this

    char attr_value[] = { container[0], '\0' };
    cout << tileNode.find_child_by_attribute("tileset","firstgid",attr_value).attribute("source").as_string();
    

    The first line converts the character in container[0] into the null terminated string that find_child_by_attribute requires.