我得到这些错误,因为我无法将const char *正确转换为std :: string ....
包含在./../Header Files / MainGame.h:5:0中的文件中, 来自./MainGame.cpp:2, 来自main.cpp:1:./../ Header Files /../源文件/ Hangman.cpp:在成员函数中'void Hangman :: checkIfItEquals(std :: string)':./../Header Files /../ Source Files / Hangman.cpp:55:41:错误:从'char'转换为无效 'const char *'[-fpermissive] std :: string charStr = string( it) ^在/usr/include/c++/4.8/string:52:0中包含的文件中, 来自/usr/include/c++/4.8/bits/locale_classes.h:40, 来自/usr/include/c++/4.8/bits/ios_base.h:41, 来自/usr/include/c++/4.8/ios:42, 来自/usr/include/c++/4.8/ostream:38, 来自/usr/include/c++/4.8/iostream:39, 来自./../Header Files / MainGame.h:2, 来自./MainGame.cpp:2, 来自main.cpp:1:/usr/include/c++/4.8/bits/basic_string.h:490:7:错误:初始化 'std :: basic_string< _CharT,_Traits的参数1, _Alloc> :: basic_string(const _CharT ,const _Alloc&)[with _CharT = char; _Traits = std :: char_traits; _Alloc = std :: allocator]'[-fpermissive] basic_string(const _CharT * __,const _Alloc& __a = _Alloc()); ^在./../Header Files / MainGame.h:5:0中包含的文件中, 来自./MainGame.cpp:2, 来自main.cpp:1:./../Header Files /../源文件/ Hangman.cpp:56:9:错误:预期','或';'在'if'之前 if(guessLet.compare(charStr)== 0) ^
这是代码....如果其他代码与问题有任何关系,我会全力以赴。
Hangman.cpp:
#include<vector>
#include "../Header Files/Hangman.h"
Hangman::Hangman()
{
}
void Hangman::run()
{
this -> mainGame();
this -> mainGameLoop();
}
void Hangman::mainGame()
{
dictonary.init();
randomString = dictonary.randomItem();
cout << "RANDOM STRING : " << randomString << endl;
stringToVec();
}
void Hangman::stringToVec()
{
for(std::string::iterator it = randomString.begin(); it != randomString.end(); ++it)
{
randStringVec.push_back(*it);
}
}
void Hangman::mainGameLoop()
{
try{
cout << " \n Guess A Letter (MUST BE A LETTER OR EXIT) \n";
std::cin >> userGuessLetter;
if(userGuessLetter.length() > 1)
throw "Too Big Of a letter";
}
catch(const char* exc)
{
if(true)
{
exit(1);
}
}
cout << "You typed " << userGuessLetter << endl;
}
void Hangman::checkIfItEquals(string guessLet)
{
for(std::vector<char>::iterator it = randStringVec.begin(); it!=randStringVec.end(); it++)
{
std::string charStr = string(*it)
if(guessLet.compare(charStr) == 0)
{
cout << "GOOD JOB.. THERE IS A SIMMILAR LETTER!" << endl;
break;
}
}
}
Hangman::~Hangman()
{
}
Hangman.h:
#pragma once
#include<iostream>
#include<string>
#include<vector>
#include "../Source Files/Drawer.cpp"
#include "../Source Files/Dictonary.cpp"
using std::cout;
using std::endl;
using std::string;
class Hangman
{
public:
Hangman();
~Hangman();
void run();
private:
Drawer hgmnDraw;
Dictonary dictonary;
std::vector<char> randStringVec;
string randomString;
string userGuessLetter;
void mainGame();
void drawHangMan(int attempts, int mode);
void checkIfItEquals(string guessLet);
void stringToVec();
void mainGameLoop();
int attemptsMadeBad = 0, attemptsMadeGood = 0;
bool correctGs;
};
修改
在BenjaminLindley之后评论下面的问题我做了编辑..并且删除了一个错误,但是仍然有2个相同的错误:
包含在./../Header Files / MainGame.h:5:0中的文件中, 来自./MainGame.cpp:2, 来自main.cpp:1:./../ Header Files /../源文件/ Hangman.cpp:在成员函数中'void Hangman :: checkIfItEquals(std :: string)':./../Header Files /../ Source Files / Hangman.cpp:55:41:错误:从'char'转换为无效 'const char *'[-fpermissive] std :: string charStr = string( it); ^在/usr/include/c++/4.8/string:52:0中包含的文件中, 来自/usr/include/c++/4.8/bits/locale_classes.h:40, 来自/usr/include/c++/4.8/bits/ios_base.h:41, 来自/usr/include/c++/4.8/ios:42, 来自/usr/include/c++/4.8/ostream:38, 来自/usr/include/c++/4.8/iostream:39, 来自./../Header Files / MainGame.h:2, 来自./MainGame.cpp:2, 来自main.cpp:1:/usr/include/c++/4.8/bits/basic_string.h:490:7:错误:初始化 'std :: basic_string&lt; _CharT,_Traits的参数1, _Alloc&gt; :: basic_string(const _CharT ,const _Alloc&amp;)[with _CharT = char; _Traits = std :: char_traits; _Alloc = std :: allocator]'[-fpermissive] basic_string(const _CharT * __,const _Alloc&amp; __a = _Alloc());
基本上我没有正确地将char const *转换为string:
for(std::vector<char>::iterator it = randStringVec.begin(); it!=randStringVec.end(); it++)
{
std::string charStr = string(*it);
if(guessLet.compare(charStr) == 0)
{
cout << "GOOD JOB.. THERE IS A SIMMILAR LETTER!" << endl;
break;
}
}
正如你在第二行看到的那样,我是类型转换但是根据这个错误不能正常工作:
In file included from ./../Header Files/MainGame.h:5:0,
from ./MainGame.cpp:2,
from main.cpp:1:
./../Header Files/../Source Files/Hangman.cpp: In member function ‘void Hangman::checkIfItEquals(std::string)’:
./../Header Files/../Source Files/Hangman.cpp:55:41: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
std::string charStr = string(*it);
^
In file included from /usr/include/c++/4.8/string:52:0,
from /usr/include/c++/4.8/bits/locale_classes.h:40,
from /usr/include/c++/4.8/bits/ios_base.h:41,
from /usr/include/c++/4.8/ios:42,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from ./../Header Files/MainGame.h:2,
from ./MainGame.cpp:2,
from main.cpp:1:
/usr/include/c++/4.8/bits/basic_string.h:490:7: error: initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ [-fpermissive]
basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
答案 0 :(得分:1)
实际上,您的目标似乎是将单个char
转换为std::string
。
您无法使用单个std::string
初始化char
。最后发生的事情是,编译器会尝试将char
提升为const char *
,与错误描述完全相同。
你想要的是std::string
构造函数,它接受一个字符和一个计数:
std::string charStr = string(1, *it);
请参阅链接here并查看构造函数2)