Ambiguous errors when trying to compile C++ code

Bluegorilla101

When trying to compile the code below I get three errors.

  • 'iterator_category': is not a member of any direct or indirect base class of 'std::iterator_traits<_InIt>'
  • '_Iter_cat_t' : Failed to specialize alias template
  • type 'unknown-type' unexpected

I'm quite new to C++ and have gone over the code many times changing snippets but nothing helps. Any help deciphering these error messages is much appreciated.

#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;
}
Sam Varshavchik

You're using find() wrong.

find(letters[0], letters[size], rand)

Your letters is a std::vector. You're passing the first value in the vector, and the last value in the vector, to std::find.

This is actually undefined behavior, since size is not always the actual size of your letters vector. So, you'll be getting a random crash, as an extra bonus here in addition to your compilation error.

The first two parameters to std::find are iterators of a sequence to search, and not values.

This should be:

find(letters.begin(), letters.end(), rand)

Also, your overall algorithm is broken. Once letters reaches a certain size, your random number generating code will take ... a significant time to find some new letter to add to letters, that's not used already. Once letters manages to acquire all 26 characters of the alphabet, this will turn into an infinite loop. But that would be a different question...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Errors when using GCC to compile C code provided in textbook

Why there is ambiguous methods error when try to compile code?

Illegal Character when trying to compile java code

Error when trying to compile SASS code

OpenCV "not a member of 'cv'" errors while trying to compile some code

ERROR when trying to compile protoc files: file not found or had errors

Conspy: "Undefined reference" errors when trying to compile the latest version

Receiving errors when trying compile Java from command line

C - Errors when trying to make a hashtable

For loop code (Compile errors)

Many internal std library compile errors when trying to compile on MSYS2/MinGW-w64

VS Code on macos: When trying to compile code in C++, "-std=gnu++14" appears before all the flags. How to delete it?

Make Error 127 when running trying to compile code

gcc doesn't do anything when trying to compile a code

Compilation results in an error when trying to compile code containing binary ifstream

Why does the C Standard allow this code to compile without errors?

Why does this code compile without errors, up to C++17?

Why does this code compile without errors in C++17?

When I am trying to push code on Git it shows errors

No errors in the code, but no changes occur in the table when trying to update it

Link errors when trying to compile against an old STD library and windows SDK

maven compile errors when i'm trying to map entity to table in postgresql

Errors when trying to compile Nutch 2.2.1 with MongoDB, could not load definitions from sonar/ant/antlib.xml

I get linker errors when trying to compile my opengl project using gcc

Trying to compile a C program

c++ unresolved external symbol when trying to compile program

Boost thread c2064 error when trying to compile

Trying to observe a string in UserDefaults, but struggling with compile errors

Compile errors trying to install WebRTCVAD module