在系统()中有一个字符串

Having a string within a system()

本文关键字:有一个 字符串 系统      更新时间:2023-10-16

如何在系统()中使用字符串。例如:(输入是字符串)

system("open -a Google Chrome" "http://www.dictionary.reference.com/browse/" + input + "?s=t");

因为当我这样做时,我收到此错误(调用"系统"没有匹配函数)。

系统cstdlib标题中可用。

函数将 c 样式字符串作为参数。 + 不会追加字符串文本。

所以试试——

std::string cmd("open -a Google Chrome");
cmd += " http://www.dictionary.reference.com/browse/" + input + "?s=t";
// In the above case, operator + overloaded in `std::string` is called and 
// does the necessary concatenation.
system(cmd.c_str());

您是否包含stdlib标题?

No matching function for call to 'system'通常发生在无法解析具有该签名的函数时。

例如:

#include <stdlib.h> // Needed for system().
int main()
{
    system("some argument"); 
    return 1;
}

并且不要忘记在将 std::string 变量作为参数传入时.c_str()它。

看:

  1. system()文档。
  2. 这个答案。
相关文章: