1

I need to declare two stacks of a struct inside of it's own struct declaration. I know I could do this operation with an array as long as I reference it with a pointer inside the struct (i.e. FA *goingTo[30]; would give me an array of 30 FAs). Is there a similar way to reference a stack?

typedef struct FA
{
    std::stack<FA> goingTo;
    std::stack<FA> comingFrom;
};
Niall
  • 30,036
  • 10
  • 99
  • 142
CodeMonkey
  • 268
  • 5
  • 16
  • Do you mean mean `std::stack going_from_or_to[2]` (or better `std::array> going_from_or_to`), then use pointers, iterators, or indices to go through them? It's not clear what your asking. – o11c Oct 06 '14 at 05:33
  • Or wait, do you mean is there a way to iterate *inside* a stack? In that case, no: `std::stack` only provides access to the `top` element. If you want access to other elements, just use a `std::vector` or `std::deque` directly; `std::stack` is practically worthless. – o11c Oct 06 '14 at 05:35
  • Structs are not allowed to contain standard containers of themself. You'll have to do something different. Boost has some containers which can do this. – M.M Oct 06 '14 at 05:39
  • The first option is what I am trying to do. I apologize for the confusion. I need to do this inside of the definition of the FA as I will be having many different FA's going to each FA and coming from each FA. Ok, I guess I need to look up the other options, thank you. – CodeMonkey Oct 06 '14 at 05:39

1 Answers1

0

The stack objects that you are defining in the struct would themselves contain (possibly) multiple instances of the struct, each instance containing its own stacks which again contain more of the structs. So if you think about it, this is an infinite chain of containment. You can modify the definition (and the usage) to contain stacks of pointers to FA*. This would solve the problem.

typedef struct FA {
   std::stack<FA*> goingTo;
   std::stack<FA*> comingFrom;
};
Deepan
  • 26
  • 3