問題迎刃而解:StringVariable [位置](在此情況下字[E])輸出被定義爲char變量類型,而不是我的預期它字符串變量類型的值。感謝大家的幫助!字符串比較版本(C++)
當我跑我的劊子手遊戲,我得到以下錯誤:
Error 2 error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion)
Error 1 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion)
4 IntelliSense: no operator "!=" matches these operands
3 IntelliSense: no operator "==" matches these operands
我在這個錯誤點,並在相關問題的功能複製的代碼註釋。然後函數在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應該是什麼? –
你不能提出[mcve]嗎? – IInspectable
@Guillaume Racicot G是用戶輸入的字母 –