0

This probably, is more of a design issue than a technical one.

But anyways, how can I access the instance of JolloManager data members inside it's nested struct? (Accesss deck?)

#include <array>
#include <iostream>
#include <numeric> //std::iota
#include <sstream>

template<std::size_t N>
class JolloManager
{
private:
    template<int ID, int c> struct PlayerCards
    {
        int id {ID};
        int cards[c] {0};
        friend std::istream& operator >> (std::istream& is, PlayerCards& p) {
            for(auto& i : p.cards) {
                is >> i;
                deck[i-1] = p.id; //error: invalid use of non-static data member 'JolloManager<N>::deck'
            }
            return is;
        }
    };
private:
    enum PlayerId { princeID = 100, princessID = 101 };
    PlayerCards<princeID,   2> prince;
    PlayerCards<princessID, 3> princess;
    std::array<int, N> deck;
public:
    JolloManager<N>() {
        std::iota(deck.begin(), deck.end(), 1);
    };
public:
    bool Read();
    int FinalCard();
};

template<std::size_t N>
bool JolloManager<N>::Read()
{
    static std::string line;
    std::getline(std::cin, line);
    std::istringstream issline(line);

    issline >> prince;
    issline >> princess;

    if(prince.cards[0] == 0) {
        return false;
    }
    return true;
}

int main()
{
    JolloManager<52> JManager;
    JManager.Read();


    return 0;
}
Vinícius
  • 15,498
  • 3
  • 29
  • 53
  • Can't be done automagically. Must provide a pointer or reference to parent. You can crate a `PlayerCards` object without any connection to any other `JolloManager` object. Compiler can't read our minds. – Dialecticus Nov 22 '19 at 15:47
  • You could update the `deck` from `JolloManager::Read` only after reading from stream is finished. – Dialecticus Nov 22 '19 at 15:55
  • @Dialecticus my point in trying to update deck while reading was exactly so I wouldn't have to iterate twice. I guess it can't be avoided. – Vinícius Nov 22 '19 at 16:34

0 Answers0