Android ndk gcc problem with inherited attribute

E

Edoardo Tagome

Hi,
I've got another problem (as in "Inheritance and friendship" post) and
I don't know if it comes from a C++ misuse or from a compiler error.

Visual Studio 2010 builds correctly but android ndk r9 gcc returns an
error.

The error refers to the line
mInt += 1 ;
and is:
error: 'mInt' was not declared in this scope

I think I can solve changing the line in
Base<T>::mInt += 1 ;
.... but why? My original code was wrong? Are there anydownsides with the
change I've applied? Or is it a good thing to do everytime?

The code is very similar to the previuous I posted:

---- MaGoTest.hpp BEGIN ----

template <class T>
class Base
{

public:
static void Build()
{
new T() ;
}


protected:
static int mInt ;

} ;

template<class T>
int Base<T>::mInt ;


template <class T>
class Der : public Base< T >
{

public:
static void Build()
{
mInt += 1 ;
Base<T>::Build() ;
}

} ;


class A : public Der< A >
{
template <class T> friend class ::Base ;
template <class T> friend class ::Der ;

private:
A() ;

} ;

---- MaGoTest.hpp END ----

---- MaGoTest.cpp BEGIN ----

#include "MaGoTest.hpp"
#include <iostream>

A::A()
{
std::cout << "A constructor" << std::endl ;
}

---- MaGoTest.cpp END ----

---- main.cpp BEGIN ----

#include "MaGoTest.hpp"

int main( void )
{
A::Build() ;

return 0 ;
}

---- main.cpp END ----
 
B

Bo Persson

Edoardo Tagome wrote 2013-08-21 18:17:
Hi,
I've got another problem (as in "Inheritance and friendship" post)
and I don't know if it comes from a C++ misuse or from a compiler error.

Visual Studio 2010 builds correctly but android ndk r9 gcc returns an
error.

The error refers to the line
mInt += 1 ;
and is:
error: 'mInt' was not declared in this scope

I think I can solve changing the line in
Base<T>::mInt += 1 ;
... but why? My original code was wrong? Are there anydownsides with the
change I've applied? Or is it a good thing to do everytime?

Templated base classes are not searched for members, partly because they
can be specialized later and have different members. MS hasn't
implemented this rule yet.

If you don't want to explicitly name the base class everywhere, you can
also use 'this->mInt' to show that it must be a member. That works
equally well for both MSVC and gcc.

There are no downsides except for the extra typing. :)


Bo Persson
 
E

Edoardo Tagome

gcc is most probably right here. Inside templates the name lookup rules
are different and one must often qualify the names explicitly to avoid
ambiguities. VS2010 is known to implement template two-phase lookup
incorrectly and thus it will often compile such incorrect code without
complaints.

See the FAQ, especially http://www.parashift.com/c++-faq/nondependent-
name-lookup-members.html

Wow, thanks!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top