2

我正在尝试编写一个具有配置和密钥文件的程序。配置文件读取输入到密钥文件中的内容,根据需要解析和执行密钥值。

我收到一个错误:警告:无法通过“...”传递非 POD 类型“struct std::string”的对象;调用将在运行时中止。

我在这一行收到错误:

snprintf(command, 256, "tar -xvzf %s %s", destination, source);
system(command);

更多代码尝试更好地解释:

std::string source = cfg.getValueOfKey<std::string>("source");
std::string destination = cfg.getValueOfKey<std::string>("destination");
int duration = cfg.getValueOfKey<int>("duration");
int count, placeHolder, placeHolderAdvanced;
count = 1;
char command[256];

snprintf(command, 256, "tar -xvzf %s %s", destination, source);
system(command);

//Creates folder 1.
snprintf(command, 256, "mkdir %i", count);
system(command);

//Removes the last folder in the group.
snprintf(command, 256, "rm -rf %i", duration);
system(command);

关于我做错了什么或我应该在哪里寻找的任何建议?

谢谢!

4

3 回答 3

11

snprintf一无所知std::string。在这种情况下,它需要以空字符结尾的 C 字符串,即指向以char空字符结尾的字符序列开头的指针。std::string您可以通过其c_str()方法获取对象持有的底层空终止字符串:

snprintf(command, 256, "tar -xvzf %s %s", destination.c_str(), source.c_str());
于 2013-08-19T17:29:36.450 回答
5

使用c_str()成员函数。

snprintf(command, 256, "tar -xvzf %s %s", destination.c_str(), source.c_str());

这将返回一个指向数组的指针,该数组包含一个表示字符串对象当前值的 C 字符串。

于 2013-08-19T17:29:40.573 回答
1

来吧 !我们在 21 世纪,回到浪潮的顶端:

#include <sstream>
...
stringstream command;
command << "tar -xvzf " << destination << " " << source;
于 2015-04-28T16:55:51.767 回答