0

I am trying to access function of a class in switch case but identifier not found error is coming.Here is the example.

class menu {
    switch(a) {
        case 1:
            tej t;
            t.do_something
            break;
    }
};
class tej:public menu {
    public:
        void do_something() {
            body of function
        }
};
Marc B
  • 356,200
  • 43
  • 426
  • 500

1 Answers1

0

There are a few things missing from your code:

  • Have you forgotten the parentheses after do_something?
  • Also, are you missing a function somewhere? You can't put a switch statement directly in the class.
  • You can't declare variables directly in the switch statement. You need an extra set of braces for that.
class menu { 
public:
    void do_switch(int a) { // Note function
        switch(a) { 
        case 1:
            { // You need an extra set of braces if you intend to declare variables
                tej t; 
                t.do_something(); // <-- N.B. Parentheses! 
            }
            break;
        } 
    } // Note extra brace to close function
}; 

class tej:public menu { 
    public: 
        void do_something() { 
            // body of function 
        } 
}; 

I highly recommend that you pick up a good introductory C++ book, as the things you're getting wrong are quite fundamental to the language. It's less frustrating that way.

Community
  • 1
  • 1
In silico
  • 51,091
  • 10
  • 150
  • 143