【发布时间】:2016-01-08 04:31:38
【问题描述】:
我正在将我的应用程序转换为能够用作库。为此,我想提供将字符串向量传递给库的默认运行例程的可能性。
我遇到的问题实际上是创建char**。这是我当前的实现,在源代码中注释掉了,因为它不起作用:
IceTea* IceTea::setupCli(vector<string> strings) {
int argc=strings.size(), i=0;
char* argv[argc];
vector<string>::iterator it;
for(it=strings.begin(); it != strings.end(); ++it) {
argv[i++] = (char*)it->c_str();
}
// Pass the char** to the original method.
return this->setupCli(argc, argv);
}
我得到的错误:
src/IceTea.cpp:132:18: error: no matching member function for call to 'setupCli'
return this->setupCli(argc, argv);
~~~~~~^~~~~~~~
src/IceTea.h:44:13: note: candidate function not viable: no known conversion from 'char *[argc]' to 'const char **' for 2nd argument
IceTea* setupCli(int, const char**);
^
src/IceTea.cpp:124:17: note: candidate function not viable: requires single argument 'strings', but 2 arguments were provided
IceTea* IceTea::setupCli(vector<string> strings) {
【问题讨论】:
-
char* argv[argc];VLA 不是标准 c++ -
第二个
setupCli()函数是怎么声明的? -
你能显示两个参数的函数签名吗?
-
只要声明
const char* argv[argc];就可以了。 -
(char*)it->c_str()很危险。不建议抛弃const。