0

I was hoping someone could help me out with an error I'm having using an enum class in a switch case. trying to use the traverse type to choose the path in the switch. Here is my code:

enum class TraverseType { PREORDER, INORDER, POSTORDER, BREADTHORDER };
private: 
    TraverseType traverseOrder;
public:
friend ostream& operator<<(ostream& out, const BTree& tree)
{
    TraverseType type = traverseOrder;//error
    switch (type)
    {
    case TraverseType::PREORDER:
    {

        break;
    }
    case TraverseType::INORDER:
    {

        break;
    }
    case TraverseType::POSTORDER:
    {

        break;
    }
    case TraverseType::BREADTHORDER:
    {

        break;
    }
    default:
    {
        break;
    }
    }
    return out;
}`

the error im getting is : Error C2440 'initializing': cannot convert from 'unknown' to 'TraverseType' and i have been trying to find a solution to this problem for hours now. If you need more code please let me know. Thank you for your help in advance

D. Spani
  • 13
  • 2
  • `enum class TraverseType { PREORDER, INORDER, POSTORDER, BREADTHORDER };` <-- enum ends here. The code that follows is... not in anything and it's confusing the poor compiler. And me. What are you trying to do here? What's the goal? – user4581301 Nov 22 '18 at 04:54
  • @user4581301 it’s unfinished code. I’m just trying to get it to compile, however I’m trying to use the switch to select different ways to traverse a binary tree – D. Spani Nov 22 '18 at 04:56
  • Looks like you want `enum class TraverseType { PREORDER, INORDER, POSTORDER, BREADTHORDER };class BTree { private:`note the `class BTree {` stuffed in there. – user4581301 Nov 22 '18 at 05:03
  • @user4581301 do you mean i should move "template " line because I tried that and it causes errors on its own. The answer I'm looking for is similar to the answer in stackoverflow.com/questions/9062082/… except i need the traverse type to be set in another function then used as a case in the switch – D. Spani Nov 22 '18 at 05:23
  • I think it would help to post the entire code - even better would be providing it on https://coliru.stacked-crooked.com/ or a similar online ide. – tangy Nov 22 '18 at 06:23

1 Answers1

0

Your friend function needs a reference to the class object to access its member - traverseOrder.

what is your class name? If it is BTree, then access the private member using its reference.

enum class TraverseType { PREORDER, INORDER, POSTORDER, BREADTHORDER };
private: 
    TraverseType traverseOrder;
public:
friend ostream& operator<<(ostream& out, const BTree& tree)
{
    TraverseType type = tree.traverseOrder;
    switch (type)