0

There are some questions on SO that are extremely similar: How to convert pugi::char_t* to string

I have been struggling with this problem for awhile, and as a beginner I'm really not sure what else to do other than ask here so I am sorry if this counts as duplicate.

pugi::xml_node actors = source_file.child("scene").child("actors");
std::vector<const char*> actor_db;
for (pugi::xml_node actor = actors.first_child(); actor; actor =     actor.next_sibling())
{
  const char *a = *actor.child_value();
  actor_db[actor.attribute("id").as_int()] = a;
}

a value of type "pugi::char_t" cannot be used to initialize an entity of type "const char *"

The line const char *a = *actor.child_value(); is my poor attempt to try to cast the return value of const char_t* xml_node::child_value() as a const char*.

I want the value of actor.child_value() to be stored in a const char* then put inside of the vector actor_db.

pugi::char_t is a type that is either a char or wchar_t depending on the configuration. I have set it up so that pugi::char_t will be a char.

I know this is probably a simple error, but even with my extensive searching and the similar SO questions I am still unable to resolve my issue.

Community
  • 1
  • 1
Scott
  • 60
  • 6
  • that's because you need a pointer to `pugi::char_t`. Does `const char *a = actor.child_value()` work? Just note that the pointer might not be elligible for storing and you should probably duplicate it to `string` (don't know the details about this library) – Zdeslav Vojkovic Jan 18 '16 at 11:57
  • Note that you never change the size of your vector so the assignment actor_db[...] will fail. You can use std::map or resize the vector (e.g. unsigned int index = ...; if (actor_db.size() <= index) actor_db.resize(index + 1); actor_db[index] = ...;) – zeuxcg Jan 18 '16 at 15:54

1 Answers1

0

I want the value of actor.child_value() to be stored in a const char* then put inside of the vector actor_db.

No, you don't. The only thing that can be stored "in" a const char* is the location of an array of characters. It's not actually a string, but a pointer to a string. Containers of pointers are fraught with problems.

Store a std::vector<std::string> instead then use the code in the answer1 to the question you linked to in order to obtain the std::strings.

1 Which, oddly enough, I've just noticed I wrote several years ago. :D

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Thank you very much. Now I understand exactly what I *really* want to do. – Scott Jan 18 '16 at 12:55
  • This is not necessarily true. vector is safer, but vector is faster - if performance is an issue and xml_document is not destroyed until vector is, storing pointers in a vector is perfectly fine. – zeuxcg Jan 18 '16 at 15:53
  • @zeuxcg: How is it faster? You're effectively storing a pointer to dynamic memory either way. – Lightness Races in Orbit Jan 18 '16 at 16:07
  • @zeuxcg: Fair enough if there are some copies implied here — as a not-user-of-pugi I don't fully grasp the semantics at play in the original code. – Lightness Races in Orbit Jan 18 '16 at 16:08
  • It's faster because when you create a std::string from a const char*, you frequently allocate memory on heap and copy the string data. – zeuxcg Jan 18 '16 at 18:17
  • @zeuxcg: That's only a problem if you can't just put the data there to begin with. – Lightness Races in Orbit Jan 18 '16 at 18:25
  • Even if your data is stored as std::string you usually have to copy that string. Anyway, pugixml works with pointers to character data, so in this case there's a performance difference - of course, whether it is important depends on the application. – zeuxcg Jan 23 '16 at 06:28
  • @zeuxcg: _"Even if your data is stored as std::string you usually have to copy that string"_ Only if you've designed your program wrong... – Lightness Races in Orbit Jan 23 '16 at 15:21