Why wont this compile:

This is a discussion on Why wont this compile: within the C++ Programming forums, part of the General Programming Boards category; What is wrong with this code: Code: #include <iostream> using namespace std; class AbstractClass { public: virtual void Func(); virtual ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    3

    Why wont this compile:

    What is wrong with this code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    class AbstractClass
    {
        public:
            virtual void Func();
            virtual void Func(int x) = 0;
    };
    
    class DerivedClass : public AbstractClass
    {
        virtual void Func(int x);
    };
    
    void AbstractClass::Func()
    {
        Func(3);
    }
    
    void DerivedClass::Func(int x)
    {
        cout << " x is " << x << "\n";
    }
    
    int main(void)
    {
        DerivedClass d;
        d.Func();
    }
    When I compile it it says no matching function call to DerivedClass::Func().

    If I change the following it works:
    Code:
    int main(void)
    {
        AbstractClass *d = new DerivedClass;
        d->Func();
        delete d;
    }
    Or, if I change the name Func(int x) to Foo(int x) it works. Why cant I overload Func with a pure virtual function?

    Thanks,
    Bob

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,620
    It may seem confusing, but function pointers a.k.a virtual functions aren't considered inherited from the child class even though they are! You'll need to interprete the derived class as its base in order to use a not overlapped virtual function ( overlapped by child ).
    Devoted my life to programming...

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    virtual is really a red herring here. Even if you had
    Code:
    class A {
        public:
            void foo();
    };
    
    class B : public A {
        public:
            void foo(int x);
    };
    
    //later
    B bar;
    bar.foo(); //won't work
    The "new" foo in class B will hide the "old" foo in class A.

  4. #4
    Registered User godly 20's Avatar
    Join Date
    Jan 2011
    Location
    Italy
    Posts
    29
    ....

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,620
    Quote Originally Posted by godly 20 View Post
    ....
    Some parts of C++ can be confusing, but that's how it is and we have to (cope?) with them.
    Devoted my life to programming...

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This is name hiding. To make the base classes Func visible in the derived class:

    Code:
    class DerivedClass : public AbstractClass
    {
    public:
        using AbstractClass::Func;
        virtual void Func(int x);
    };
    You might also consider renaming the overloads.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C and C++ compile speed
    By swgh in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-02-2007, 01:37 PM
  2. Compile as you type
    By Rocketmagnet in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 12-07-2006, 12:36 PM
  3. How to compile mfc libs from platform sdk
    By tjcbs in forum Windows Programming
    Replies: 6
    Last Post: 11-19-2006, 07:20 AM
  4. Compile crashes certain windows
    By Loduwijk in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2006, 08:05 PM
  5. How can I compile C or C++ with Visual Studio .NET?
    By Dakkon in forum C Programming
    Replies: 8
    Last Post: 02-11-2003, 01:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21