C++ snprintf "cannot pass objects of non-POD type"
C++ snprintf "cannot pass objects of non-POD type"
我正在尝试编写一个具有配置和密钥文件的程序。配置文件读取输入到密钥文件中的任何内容,根据需要解析和执行密钥值。
我收到一个错误:警告:无法通过"..."传递非 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);
关于我做错了什么,或者我应该在哪里寻找的任何建议?
谢谢!
snprintf
对std::string
一无所知。在这种情况下,它需要以 null 结尾的 C 字符串,即指向char
的指针,这些指针是以 null 字符结尾的字符序列的开头。可以通过其 c_str()
方法获取由 std::string
对象持有的基础 null 终止字符串:
snprintf(command, 256, "tar -xvzf %s %s", destination.c_str(), source.c_str());
使用c_str()
成员函数。
snprintf(command, 256, "tar -xvzf %s %s", destination.c_str(), source.c_str());
这将返回一个指向数组的指针,该数组包含一个表示字符串对象的当前值的 C 字符串。
加油!我们身处21世纪,回到浪潮之巅:
#include <sstream>
...
stringstream command;
command << "tar -xvzf " << destination << " " << source;
相关文章:
- 如何处理 c++ 中类实现中的"invalid use of non-static data member"?
- 收到错误"invalid use of non-static data member 'stu::n' "
- 出现这种错误的原因是什么"invalid use of non-static data member "
- 抑制非平凡无用的警告"control may reach end of non-void function"
- 如何修复库中的"invalid use of non-static member function" 导入另一个库
- 在C++中传递对象会产生'invalid use of non-static member'错误
- 为什么当我尝试在类中创建数组时"invalid use of non-static data member"我会收到错误?
- 我在C++年写了一个findClosestPrime(),但Xcode抱怨"Control may reach end of non-void function"
- 收到此警告" Control may reach end of non-void function "
- 即使我已经涵盖了所有情况,也会收到"control reaches end of non-void function"警告
- 布尔函数"Control reaches end of non-void function"错误
- C++编译器错误"cannot be thread-local because it has non-POD type""
- 困惑为什么"control may reach end of non-void function"
- 当调用带有警告"control reaches end of non-void function"的函数时,(实际上)会发生什么?
- 访问父类的成员"Invalid use of non-static data member" C++
- CopyMemory Non-POD
- "invalid use of non static member function" 这是什么?
- "invalid initialization of non-const reference of type..." 克隆() 函数错误
- C++类模板和嵌套结构。 "Invalid use of non-static data member..."
- C++ snprintf "cannot pass objects of non-POD type"