【发布时间】:2017-09-18 17:24:09
【问题描述】:
问题已解决:StringVariable[position](在本例中为 Word[e])输出一个定义为 char 变量类型的值,而不是我预期的字符串变量类型。谢谢大家的帮助!
当我运行 Hangman 游戏时,出现以下错误:
错误 2 错误 C2678: 二进制 '!=' : 未找到采用 'std::string' 类型的左操作数的运算符(或没有可接受的转换)
错误 1 错误 C2678:二进制“==”:未找到采用“std::string”类型的左操作数的运算符(或没有可接受的转换)
4 IntelliSense:没有运算符“!=”与这些操作数匹配
3 IntelliSense:没有运算符“==”与这些操作数匹配
我在此错误指向的代码中进行了注释,并在与问题相关的函数中进行了复制。然后在 main() 函数中运行这些函数以简化代码。
这部分代码用于检查猜测是否等于单词中的字母。如果需要进一步解释,请告诉我。
相关代码:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//Functions
void GrabWord();
void DiscoverLet();
void guess();
//Variables
int NumL, Fl, F, count[10];
string G, Word;
//Grab Word from .txt and define NumL
void GrabWord()
{
//Grab Random Line from .txt
//Random Line >> Larray
Word = "short";
NumL = Word.size();
}
//Checks if Guess matches any letters
void DiscoverLet()
{
for(int e = 0; e < NumL; e++)
{
//Error Points to the following two comparisons
if(G == Word[e])
{
count[e] = 1;
}
else if(G != Word[e])
{
Fl++;
}
else
{
cout << "Error: DiscoverLet(), G to Word[]\n";
}
}
if(Fl == NumL)
{
F = F + 1;
}
Fl = 0;
}
【问题讨论】:
-
G应该是什么? -
你不能提交minimal reproducible example吗?
-
@Guillaume Racicot G 是用户输入的字母