【问题标题】:invalid operands of types 'const char*' and 'const char [93]' to binary [duplicate]'const char *'和'const char [93]'类型的无效操作数到二进制[重复]
【发布时间】:2015-06-03 17:40:50
【问题描述】:
char b = (char)i;
        char *text = "function make_page("+b+"){"
"var url = 'http://www.gigasena.com.br/loterias/mega-sena/resultados/resultado-mega-sena-'+"+b+"+'.htm';"
"var page = require('webpage').create();"
"var fs = require('fs');"
"page.open(url, function () {"
    "page.evaluate(function(){"
""
    "});"
    "page.render('results/export-'+"+b+"+'.png');"
    "fs.write('results/'+"+b+"+'.html', page.content, 'w');"
    "phantom.exit();"
"});"
"}"
"make_page("+b+");";

【问题讨论】:

  • 你不能添加字符串文字...
  • 您在此处以数学方式将字符添加到内存地址(至少您想这样做)。不要将 C 或 C++ 与例如混淆。 Java,并决定选择一种语言(C“或”C++),然后我们可以向您展示一些正确的东西。

标签: c++ c


【解决方案1】:

您不能在 C++ 中添加字符串文字。如果你使用 std::string 对象,那么你可以这样做:

int i = 'a';
char b = (char)i;
std::string text = std::string("function make_page(") + b + "){"
    "var url = 'http://www.gigasena.com.br/loterias/mega-sena/resultados/resultado-mega-sena-'+" + b + "+'.htm';"
    "var page = require('webpage').create();"
    "var fs = require('fs');"
    "page.open(url, function () {"
    "page.evaluate(function(){"
    ""
    "});"
    "page.render('results/export-'+" + b + "+'.png');"
    "fs.write('results/'+" + b + "+'.html', page.content, 'w');"
    "phantom.exit();"
    "});"
    "}"
    "make_page(" + b + ");";

【讨论】:

    【解决方案2】:

    您不能在两个 c 字符串上使用 operator+。你可以使用这个:

    http://en.cppreference.com/w/cpp/string/byte/strcat

    或者你可以这样做:

    char b;
    std::string text = std::string("your text here") + b + std::string("more text etc");
    //then use text.c_str() if you need const char*
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-19
      • 2014-09-30
      • 1970-01-01
      • 2020-06-23
      • 2023-03-21
      • 1970-01-01
      相关资源
      最近更新 更多