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

5
  • 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? Commented Nov 22, 2018 at 4: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 Commented Nov 22, 2018 at 4:56
  • Looks like you want enum class TraverseType { PREORDER, INORDER, POSTORDER, BREADTHORDER };class BTree { private:note the class BTree { stuffed in there. Commented Nov 22, 2018 at 5:03
  • @user4581301 do you mean i should move "template <calss P>" 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 Commented Nov 22, 2018 at 5:23
  • I think it would help to post the entire code - even better would be providing it on coliru.stacked-crooked.com or a similar online ide. Commented Nov 22, 2018 at 6:23

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.