Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > problem with template class and overloaded methods

Reply
Thread Tools

problem with template class and overloaded methods

 
 
gg
Guest
Posts: n/a
 
      08-10-2005
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<cha r>,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<cha r>,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<cha r>,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<cha r>,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<cha r>,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<cha r>,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<cha r>,std::allocator<char>
>)"; did you forget to override it?

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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-10-2005
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
 
Reply With Quote
 
 
 
 
gg
Guest
Posts: n/a
 
      08-10-2005
Thanx, the following worked -

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring a template class with two template params a friend in anon-template class A L C++ 1 08-25-2010 07:25 AM
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
Non-template class from a template base class with pure virtual methods vilarneto@gmail.com C++ 2 03-25-2007 08:19 PM
Why doesn't Ruby allow for overloaded methods within a class Wes Gamble Ruby 10 01-12-2007 09:23 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM



Advertisments