Re: Inheritance and friendship
On 8/8/2013 9:02 AM, Edoardo Tagome wrote:
Hi,
first of all I must apologize if I'm OT (and I fear I may be) but I
couldn't find a better NG (at least among the ones I have access to).
I can build this code in Visual Studio 2010 but g++ (android-ndk-r8d)
fails to build.
As you can see the problem has to do woth friendship and inheritance
B<T>
^
|
|
F<T>
^
|
|
A
---- MaGoTest.hpp BEGIN ----
template <class T>
class MaGoTestB
{
private:
T *mVar ;
public:
void M2( void ) ;
} ;
template <class T>
class MaGoTestF : public MaGoTestB< T >
{
private:
T *mVar ;
public:
void M3( void ) ;
} ;
class MaGoTestA : public MaGoTestF< MaGoTestA >
{
template <class U> friend class MaGoTestB;
//template <class U> friend class MaGoTestF;
private:
public:
void M1( void ) ;
} ;
---- MaGoTest.hpp END ----
---- MaGoTest.cpp BEGIN ----
#include "MaGoTest.hpp"
void MaGoTestA::M1( void )
{
return ;
}
template< class T >
void MaGoTestB< T >::M2( void )
{
return ;
}
template< class T >
void MaGoTestF< T >::M3( void )
{
return ;
}
---- MaGoTest.hpp END ----
The error is:
In file included from
C:/Users/edotgm/Desktop/SRC/MaGo_Sample/SRC/MaGoTest.cpp:1:0:
C:/Users/edotgm/Desktop/SRC/MaGo_Sample/SRC/MaGoTest.hpp:29:41: error:
specialization of 'MaGoTestB<MaGoTestA>' after instantiation
make: *** [obj/local/armeabi/objs-debug/MaGoMmon/MaGoTest.o] Error 1
It's not clear to me if this is a c++ problem, an unsupported feature of
g++ or if I have to enable something through command line switches in g++ .
Hope there is someone in this NG who can enlighten me abiut this issue :-)
First, your posting is *not* off-topic. It's perfectly OK, IMNSHO.
Second, I would like to make a couple of suggestions. While using mixed
case might look good and readable to you, when the names of two
templates only differ in one letter out of nine, that's difficult to
read to some of us. For the sake of posting here, naming your templates
'Base' and 'Der' would work just as nicely, and be more readable. Also,
what is the point of making unrelated template instantiation (actually,
all of them) a friend of your '...A' class? Your class has no private
members, and no instantiation attempts to access them. Also, put the
instantiations of the template's members into the header in order to
avoid unpleasant errors. Also, get rid of the 'void' between the
parentheses. If the parentheses are intended to be empty, they ought to
be *empty*.
IOW, your code I would rather see as
---- MaGoTest.hpp BEGIN ----
template <class T> class Base
{
T *mVar; // it's private, no need to say it
public:
void M2() {}
};
template <class T> class Der : public Base<T>
{
T *mVar ;
public:
void M3() {}
};
class A : public Der<A>
{
template <class U> friend class Base;
public:
void M1() {}
};
---- MaGoTest.hpp END ----
....although I don't really see the point in defining all those member
functions or the friendship between A and Base<U>. Try to put the
implementation of Base<T>::M2 and Der<T>::M3 in the class template
definition itself. You will find it necessary anyway when the linker
complains (even if you get the 'friend' declaration to compile or just
remove it).
In the future, consider that it's quite useful to let us know what
problem you're trying to solve. Perhaps the friendship between a class
derived from a derived class of some base template instantiation and all
instances of that grandfather class template is not really a good
solution for what you're trying to accomplish...
Good luck!
V
--
I do not respond to top-posted replies, please don't ask