How could I call a base class virtual function from the derived class ? (C++)

  • C/++/#
  • Thread starter walnutTree
  • Start date
  • #1

Main Question or Discussion Point

Dear All,
I have got a subclass (Panda), which inherits a Print function from the base class (Bear).
I'd like to be able to call the base class's function from the subclass.
But my complier gives me the following error message:

In member function `virtual void Panda::print(char)':
expected primary-expression before "char"


I use DEV-CPP.
Heres is my code causing the problem:

Code:
class Bear{
      public:
             virtual void print(char x)
             {
                 std::cout<<"bear"<<std::endl; 
             }
      };
class Panda : public Bear{
    public:
             void print(char x)
             {
                Bear::print(char x);
                std::cout<<"panda"<<std::endl;  
             }  
      };
Could you tell me what's the wrong?
Any ideas would be appreciated!
 

Answers and Replies

  • #2
297
2
Look very closely at this line:
Code:
                Bear::print(char x);
It's a function call, so more like this:
Code:
                Bear::print(x);
If you're like me, you've slapped yourself in the forehead and groaned by now. :wink:
 
  • #3
thanks for clearing things up, it was a rookie mistake...
and the slapping took place as well:)
 

Related Threads on How could I call a base class virtual function from the derived class ? (C++)

Replies
3
Views
2K
Replies
5
Views
15K
  • Last Post
Replies
7
Views
3K
Replies
5
Views
787
Replies
4
Views
37K
Replies
4
Views
9K
Replies
8
Views
4K
  • Last Post
Replies
2
Views
2K
Replies
2
Views
2K
Replies
1
Views
16K
Top