我的代码抛出模板错误 (C++)
My code is throwing template errors (C++)
几天
来我一直在反对这段代码。 它抛出一个:
template argument deduction/substitution failed:
第 119、124,129、162 行错误。(此处标有//错误 )
我仍然是编码方面的超级N00B,因此任何帮助将不胜感激。提前感谢!!
/*********************
Hangman v2
The classic game of hangman using multiple functions
(from chaper 5 C++ book)
**************************/
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
using namespace std;
//GLOBALS!!
string THE_WORD; //secret word player is tryign to guess
string soFar; //word string ei: "G-ESS"
const int MAX_WRONG = 8; // maximum number of incorrect guesses allowed
char guess; //character guessed by player
int wrong; //incorrect guesses
//Functions used (declarations)
string f_soFar();
void f_used();
int main();
vector<string> used_ch; ///used characters
string getWord()
{
vector<string> words; // collection of possible words to guess
words.push_back("GUESS");
words.push_back("HANGMAN");
words.push_back("DIFFICULT");
words.push_back("ENTERPRISE");
words.push_back("GALAXY");
words.push_back("POMEGRANATE");
words.push_back("CONTROLLER");
srand(static_cast<unsigned int>(time(0))); //Seed based on time
random_shuffle(words.begin(), words.end()); //randomly picks from words vector
string secretWord;
secretWord = words[0]; // word to guess
THE_WORD = secretWord;
cout<<"New Secret word has been chosen!!"<<endl;
cout<<THE_WORD<<endl;
return THE_WORD;
}
void check(char guess)
{
cout << "nnEnter your guess:nn->";
cin >> guess;
guess = toupper(guess); //make uppercase since secret word in uppercase
while (find (used_ch.begin(), used_ch.end(), guess) != used_ch.end())
{
cout << "nYou've already guessed " << guess << endl;
cout << "Enter another guess: ";
cin >> guess;
guess = toupper(guess);
if (THE_WORD.find(guess) != string::npos)
{
cout << "That's right! " << guess << " is in the word.n";
// update used_ch to include newly guessed letter
for (unsigned int i = 0; i < THE_WORD.length(); ++i)
{
if (THE_WORD[i] == guess)
{
used_ch.push_back(&guess);
soFar[i] = guess;
cout<<"Guess again!"<<endl;
}
}
}
else
{
cout << "Sorry, " << &guess << " isn't in the word.n";
++wrong;
cout<<"Try again!"<<endl;
}
}
}
void f_used()
{
for(unsigned int i = 0; i < used_ch.size(); i++)
{
cout<<used_ch[i]<<", ";
}
}
void replay()
{
char choice;
cout<<"Would you like to play again?nY/Nnn->";
cin>>choice;
if(choice == 'Y' || choice == 'y')
{
cout<<"OK!!!! Here we go...."<<endl;
used_ch.clear();
main();
}
else if(choice == 'N' || choice == 'n')
{
cout<<"OK, thanks for playing!!";
}
else //ERROR HERE
{
cout<<"Invalid Entry. please try again...";
replay();
}
} //ERROR HERE
int main()
{
// set-up
getWord(); //ERROR HERE //should get word from word()
//cout<<"The word is "<<THE_WORD<<endl; //TESTING PURPOSES!!
while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
{
//STATUS
cout << "nnYou have " << (MAX_WRONG - wrong);
cout << " incorrect guesses left.n";
cout << "nYou've used the following letters:n";
f_used();
cout << "nSo far, the word is:n" << soFar << endl;
check(guess);
}
// shut down
if (wrong == MAX_WRONG)
{
cout << "nYou've been hanged!";
replay();
}
else
{
cout << "nYou guessed it!";
cout << "nThe word was " << THE_WORD << endl;
replay();
}
} //ERROR HERE
首先,让我们看一下完整的编译器错误:
c++ foo.cc -o foo
In file included from foo.cc:7:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream:38:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:216:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__locale:15:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:439:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:846:22: error: invalid operands to binary expression
('std::__1::basic_string<char>' and 'int')
if (*__first == __value_)
~~~~~~~~ ^ ~~~~~~~~
foo.cc:64:12: note: in instantiation of function template specialization 'std::__1::find<std::__1::__wrap_iter<std::__1::basic_string<char> *>, char>' requested here
while (find (used_ch.begin(), used_ch.end(), guess) != used_ch.end())
^
1 error generated.
现在,让我们创建一个简化的测试用例:
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
std::vector<std::string> used_ch;
char guess = 'c';
find(used_ch.begin(), used_ch.end(), guess);
}
现在,我们看看问题是什么:
您正在尝试在字符串向量中查找char
。但是,没有需要字符串和字符的operator==
重载。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 错误:未在此范围内声明'reverse'