嘗試編譯下面的代碼時,出現三個錯誤。嘗試編譯C++代碼時出現模糊錯誤
- '的iterator_category':沒有任何直接或間接的基類 '的std :: iterator_traits < _Init>'
- '_Iter_cat_t' 中的一個成員:無法專門別名模板
- 類型「unknown-鍵入'意想不到的
我對C++很陌生,已經改變了代碼多次改變片段,但沒有什麼幫助。任何幫助破譯這些錯誤消息非常感謝。
#include "../../std_lib_facilities.h"
class Puzzle {
public:
vector<char> letters;
Puzzle(int my_size);
void generate(void);
void enter_letters(void);
void feedback(Puzzle puzzle);
private:
int size = 4;
};
Puzzle::Puzzle(int my_size)
{
size = my_size;
}
//Generate size unique letters for the letters array.
void Puzzle::generate(void)
{
for (int i = 0; i < size; ++i) {
char rand = randint(26) - 1 + 'a';
while ((find(letters[0], letters[size], rand) != letters[size])) {
rand = randint(26) - 1 + 'a';
}
letters[i] = rand;
}
}
//Let the user enter size unique letters.
void Puzzle::enter_letters(void)
{
cout << "Enter four different letters seperated by spaces:\n";
for (int i = 0; i < size; ++i) {
char letter;
cin >> letter;
letters[i] = letter;
}
}
//Tell the user how many bulls and cows they got.
void Puzzle::feedback(Puzzle puzzle)
{
int cows = 0, bulls = 0;
for (int i = 0; i < size; i++) { //input
for (int j = 0; j < size; ++j) { //puzzle
if (i == j && letters[i] == puzzle.letters[j]) {
++bulls;
break;
}
else if (letters[i] == puzzle.letters[j]) {
++cows;
break;
}
}
}
cout << "Bulls: " << bulls << "\nCows: " << cows << "\n";
}
//Seed the random function.
void seed(void)
{
int sum = 0;
cout << "Enter a random string of characters:\n";
string str;
cin >> str;
for (char& c : str)
sum += c;
srand(sum);
}
int main()
{
constexpr int GAME_SIZE = 4;
seed();
Puzzle puzzle(GAME_SIZE);
puzzle.generate();
Puzzle input(GAME_SIZE);
input.enter_letters();
while (puzzle.letters != input.letters) {
input.feedback(puzzle);
input.enter_letters();
}
cout << "Congragulations, you did it!\n";
keep_window_open();
return 0;
}
什麼是'std_lib_facilities.h'? – SurvivalMachine
@SurvivalMachine我正在閱讀這本書,Programming:Principles and Practice Using C++,它需要你使用這個頭文件。我認爲這樣做可以讓我不必擔心需要使用哪些庫。如果你有興趣,你可以在這裏找到它(http://www.stroustrup.com/Programming/std_lib_facilities.h)。 – Bluegorilla101
'(find(letters [0],letters [size],rand)'這是對std :: find的調用嗎?如果是這樣,那就是你的問題,'char's不是迭代器,使用'letters.cbegin )'和'letters.cend()',而不是 – Pandatyr