Search code examples
c++constructorqueueidentifierstd-pair

Identifier Unidentified in Default Constructor


I've been searching online a lot for how to fix this. Also tried making everything public, but I don't think that's the problem. Here's my header code:

#ifndef DEALER_HPP
#define DEALER_HPP

#include <queue>

class Dealer{
private:
    queue<pair<int, char>> deck;

public:
    Dealer(); // default constructor
    ~Dealer(); // destructor        
};

#endif

Source file:

#include "Dealer.hpp"

using namespace std;

Dealer::Dealer(){// create unshuffled deck
    const char* suitValue[4] = {"c", "d", "h", "s"};

    for (int i = 2; i <= 14; i++)
    {
        for (int j = 1; j <= 4; j++)
        {
            deck.push(pair<int, char> (i, suitValue[j])); // error on this line
        }
    }
}

I've got an error in my source file,

identifier "deck" is unidentified.

Any idea how to fix it? I've also tried using make_pair with no success. I really feel like the code I have should be working, I'm sure there's some simple mistake. Sorry, I'm having trouble chasing this down.


Solution

  • Firstly you need #include <utility> and the declaration of deck should be:

    std::queue< std::pair<int, char> > deck;
    

    This is probably the source of your error, although there should also have been an error message for this specific line.

    Moving on, the pair is a pair of int and char. But you write later:

    pair<int, char> (i, suitValue[j])
    

    suitValue[j] is a char *, not a char. So this also must generate a compiler error. I guess you want std::string instead of char and const char * in both places.

    Also, you access out of bounds in the j loop. For an array of dimension 4, the valid indices are 0 1 2 3. Not 4.