C++ 从“char”到“const char *”的无效转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22798912/
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 00:13:27  来源:igfitidea点击:

Invalid conversion from 'char' to 'const char *'

c++fstream

提问by NukingDragons

I am having trouble loading a char into one of my functions. This is the source code which I started out with.

我无法将字符加载到我的一个函数中。这是我开始使用的源代码。

main.cpp

主程序

#include <IOF\IOF.h>

int main()
{

char load;
string intro = LoadFileToString( "Intro.txt" );

cout << intro << "> ";

cin >> load;

string loadedFile = LoadFileToString( load );

cout << loadedFile;

cin.get();
cin.ignore();

return 0;

}

IOF.h

IOF.h

#ifndef IOF_H
#define IOF_H

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

string LoadFileToString( const char * filePath )
{

string fileData;

ifstream loadFile( filePath, ios::in );

if ( loadFile.is_open() )
{

    string line;

    while ( getline( loadFile, line ) )
    {

        fileData += line + "\n";

    }

    loadFile.close();

}

return fileData;

}

#endif

And this is what my compiler tells me.(I am using MinGW)

这就是我的编译器告诉我的。(我正在使用 MinGW)

H:\Programming\Dropbox\C++\Basic Text Editor\main.cpp: In function 'int main()':
H:\Programming\Dropbox\C++\Basic Text Editor\main.cpp:13:45: error: invalid conversion from 'char' to 'const char*' [-fpermissive]
string loadedFile = LoadFileToString( load );
                                         ^
In file included from H:\Programming\Dropbox\C++\Basic Text Editor\main.cpp:1:0:
H:\Programming\pocketcpp\MinGW\include/IOF\IOF.h:10:8: error:   initializing argument 1 of 'std::string LoadFileToString(const char*)' [-fpermissive]
string LoadFileToString( const char * filePath )

I have tried referencing load, it compiles fine, but when I run the program and try to load a text file, it crashes. I have also tried adding a cast to it

我试过引用负载,它编译得很好,但是当我运行程序并尝试加载文本文件时,它崩溃了。我也试过给它添加演员表

string loadedFile = LoadFileToString( ( const char * ) load );

but that gave me this error:

但这给了我这个错误:

H:\Programming\Dropbox\C++\Basic Text Editor\main.cpp: In function 'int main()':
H:\Programming\Dropbox\C++\Basic Text Editor\main.cpp:13:57: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
  string loadedFile = LoadFileToString( ( const char * ) load );

I originally made the IOF header so I would have a fast and easy way of receiving text from a file, but am I going to have to do all of the ifstream stuff manually for this part? Or is there a workaround?

我最初制作了 IOF 标头,因此我可以快速简便地从文件中接收文本,但是我是否必须为此部分手动执行所有 ifstream 操作?或者有解决方法吗?

回答by legends2k

char load;
string intro = LoadFileToString( "Intro.txt" );

cout << intro << "> ";

cin >> load;

string loadedFile = LoadFileToString( load );

You're trying to pass a charto LoadFileToStringwhich takes in a char*. I think what you intend to do is this

您正在尝试将 a 传递charLoadFileToString一个接受 a char*。我想你打算做的是这个

string intro = LoadFileToString( "Intro.txt" );    
cout << intro << "> ";
string load;
cin >> load;    
string loadedFile = LoadFileToString( load.c_str() );

Notice that I'm reading the input into a stringinstead of a charsince the LoadFileToStringexpects a (C-style) string to be passed in; instead you're reading and trying to pass a character to it. Once the input is read through a string, a C-style string can be obtained by calling string::c_str()function.

请注意,我将输入读入 astring而不是 a,char因为LoadFileToString期望传入一个(C 样式)字符串;相反,您正在阅读并尝试将字符传递给它。通过 a 读取输入后string,可以通过调用string::c_str()函数获得 C 风格的字符串。

回答by Deduplicator

Never try to muzzle the compiler. Those warnings are invaluable. Obviously, you want a std::stringinstead of a char, where you define load.

永远不要试图压制编译器。这些警告是无价的。显然,您需要 astd::string而不是 a char,您可以在其中定义负载。

you get the const char*from a std::stringby calling c_str().

您可以通过调用const char*从 a获取。std::stringc_str()

Though, there is no reason your LoadFileToString()function should not instead take a const std::string&.

但是,您的LoadFileToString()函数没有理由不采用const std::string&.

回答by Vlad from Moscow

The error message is clear enough.

错误信息已经足够清楚了。

The function declared as

函数声明为

string LoadFileToString( const char * filePath );

that is its parameter has type const char *

那是它的参数有类型 const char *

You are trying to call the function passing variable load as an argument

您正在尝试调用传递变量负载作为参数的函数

string loadedFile = LoadFileToString( load );

However the variable load is declared as having type char

但是变量 load 被声明为具有类型 char

char load;

So there is a type mismatch.

所以存在类型不匹配。

You could for example to write

你可以例如写

string loadedFile = LoadFileToString( &load );

but in this case the code would be also invalid because the pointer referes to non zero-terminating string that is the behaviour of the program will be undefined.

但在这种情况下,代码也是无效的,因为指针指向非零终止字符串,即程序的行为将是未定义的。