problem with template class and overloaded methods

Discussion in 'C++' started by gg, Aug 10, 2005.

  1. gg

    gg Guest

    I am facing some problems with following program. I am using aCC
    version 03.27 on HP-UX11. The command line I use to compile is -

    aCC -AA -I. TestTempMethods.C

    Can anybody pls suggest how to resolve the warnings and errors.

    #include <iostream>
    using namespace std;

    #include <string.h>

    template < class T >
    class X
    {
    public:
    virtual void foo ( );
    protected:
    virtual void foo ( int );
    virtual void foo ( string ) = 0;
    };

    template < class T >
    void X < T > :: foo ( )
    {
    foo ( 1 );
    foo ( "a" );
    }

    template < class T >
    void X < T > :: foo ( int i )
    {
    cout << " int " << i << endl;
    }

    class Y : public X < int >
    {
    public:
    protected:
    virtual void foo ( string s );
    };

    void Y::foo ( string s )
    {
    cout << " string " << s << endl;
    }

    int main ( void )
    {
    Y y;
    y.foo ( );
    }

    The above version gives the following errors.

    Error 182: "TestTempMethods.C", line 44 # "int main()" cannot access
    protected member "void

    Y::foo(std::basic_string<char,std::char_traits<char>,std::allocator<char>
    >)".

    y.foo ( );
    ^^^^^^^^^
    Warning 652: "TestTempMethods.C", line 29 # Virtual function "void
    X<int>::foo()" is hidden by "void

    Y::foo(std::basic_string<char,std::char_traits<char>,std::allocator<char>
    >)"; did you forget to override it?

    class Y : public X < int >
    ^^^^^^^
    Warning 652: "TestTempMethods.C", line 29 # Virtual function "void
    X<int>::foo(int)" is hidden by "void

    Y::foo(std::basic_string<char,std::char_traits<char>,std::allocator<char>
    >)"; did you forget to override it?

    class Y : public X < int >
    ^^^^^^^


    Just changing the Y class, gives the following errors -

    class Y : public X < int >
    {
    public:
    protected:
    using X::foo;
    virtual void foo ( string s );
    };

    Error 182: "TestTempMethods.C", line 45 # "int main()" cannot access
    protected member "void X<int>::foo()".
    y.foo ( );
    ^^^^^^^^^
    Warning 652: "TestTempMethods.C", line 29 # Virtual function "void
    X<int>::foo()" is hidden by "void

    Y::foo(std::basic_string<char,std::char_traits<char>,std::allocator<char>
    >)"; did you forget to override it?

    class Y : public X < int >
    ^^^^^^^
    Warning 652: "TestTempMethods.C", line 29 # Virtual function "void
    X<int>::foo(int)" is hidden by "void

    Y::foo(std::basic_string<char,std::char_traits<char>,std::allocator<char>
    >)"; did you forget to override it?

    class Y : public X < int >
    ^^^^^^^

    Moving the using declaration order gives the following errors -

    class Y : public X < int >
    {
    public:
    protected:
    virtual void foo ( string s );
    using X::foo;
    };

    Error 182: "TestTempMethods.C", line 45 # "int main()" cannot access
    protected member "void X<int>::foo()".
    y.foo ( );

    Moving the using declaration to public section gives the following
    warnings -

    class Y : public X < int >
    {
    public:
    using X::foo;
    protected:
    virtual void foo ( string s );
    };


    Warning 652: "TestTempMethods.C", line 29 # Virtual function "void
    X<int>::foo()" is hidden by "void

    Y::foo(std::basic_string<char,std::char_traits<char>,std::allocator<char>
    >)"; did you forget to override it?

    class Y : public X < int >
    ^^^^^^^
    Warning 652: "TestTempMethods.C", line 29 # Virtual function "void
    X<int>::foo(int)" is hidden by "void

    Y::foo(std::basic_string<char,std::char_traits<char>,std::allocator<char>
    >)"; did you forget to override it?

    class Y : public X < int >
    ^^^^^^^
    gg, Aug 10, 2005
    #1
    1. Advertising

  2. gg wrote:
    > I am facing some problems with following program. I am using aCC
    > version 03.27 on HP-UX11. The command line I use to compile is -
    >
    > aCC -AA -I. TestTempMethods.C
    >
    > Can anybody pls suggest how to resolve the warnings and errors.
    >
    > #include <iostream>
    > using namespace std;
    >
    > #include <string.h>


    You don't need <string.h>, you need <string>.

    >
    > template < class T >
    > class X
    > {
    > public:
    > virtual void foo ( );
    > protected:
    > virtual void foo ( int );
    > virtual void foo ( string ) = 0;
    > };
    >
    > template < class T >
    > void X < T > :: foo ( )
    > {
    > foo ( 1 );
    > foo ( "a" );
    > }
    >
    > template < class T >
    > void X < T > :: foo ( int i )
    > {
    > cout << " int " << i << endl;
    > }
    >
    > class Y : public X < int >
    > {
    > public:
    > protected:
    > virtual void foo ( string s );


    This function _hides_ the rest of the functions inherited from X<int>.
    Add:

    public:
    using X<int>::foo;

    > };
    >
    > void Y::foo ( string s )
    > {
    > cout << " string " << s << endl;
    > }
    >
    > int main ( void )
    > {
    > Y y;
    > y.foo ( );
    > }
    >
    > The above version gives the following errors.
    > [...]


    V
    Victor Bazarov, Aug 10, 2005
    #2
    1. Advertising

  3. gg

    gg Guest

    Thanx, the following worked -

    class Y : public X < int >
    {
    protected:
    virtual void foo ( string s );
    public:
    using X<int>::foo;
    };
    gg, Aug 10, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. christopher diggins
    Replies:
    16
    Views:
    745
    Pete Becker
    May 4, 2005
  2. Replies:
    2
    Views:
    555
  3. A L
    Replies:
    1
    Views:
    510
    Alf P. Steinbach /Usenet
    Aug 25, 2010
  4. Wes Gamble
    Replies:
    10
    Views:
    149
    Per Velschow
    Jan 12, 2007
  5. Kenneth McDonald
    Replies:
    5
    Views:
    312
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page