简体   繁体   English

从'char'到'const char *'的无效转换

[英]Invalid conversion from 'char' to 'const char *'

I am having trouble loading a char into one of my functions. 我在将char加载到我的函数之一时遇到麻烦。 This is the source code which I started out with. 这是我开始使用的源代码。

main.cpp 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? 我最初是制作IOF标头的,所以我可以快速便捷地从文件中接收文本,但是对于这一部分,我是否必须手动完成所有ifstream的工作? Or is there a workaround? 还是有解决方法?

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

cout << intro << "> ";

cin >> load;

string loadedFile = LoadFileToString( load );

You're trying to pass a char to LoadFileToString which takes in a char* . 您正在尝试将一个char传递给LoadFileToString ,它采用char* I think what you intend to do is this 我想你打算做的是

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 string instead of a char since the LoadFileToString expects a (C-style) string to be passed in; 请注意,由于LoadFileToString希望传入(C样式)字符串,因此我将输入读取为string而不是char 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. 通过string读取输入后,可以通过调用string::c_str()函数获得C样式的字符串。

Never try to muzzle the compiler. 切勿试图套用编译器。 Those warnings are invaluable. 这些警告是无价的。 Obviously, you want a std::string instead of a char , where you define load. 显然,您要使用std::string而不是char定义负载。

you get the const char* from a std::string by calling c_str() . 您可以通过调用c_str()std::string获得const char*

Though, there is no reason your LoadFileToString() function should not instead take a const std::string& . 不过,没有理由让您的LoadFileToString()函数不应该采用const std::string&

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 您正在尝试调用传递变量load作为参数的函数

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. 但是在这种情况下,代码也将无效,因为指针指向的是非零结尾的字符串,即程序的行为是不确定的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM