Android ndk gcc problem with inherited attribute

Discussion in 'C++' started by Edoardo Tagome, Aug 21, 2013.

  1. 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 ----
     
    Edoardo Tagome, Aug 21, 2013
    #1
    1. Advertising

  2. Ehm... I forgot to thank you in advance :)

    Bye


    On 21/08/2013 18:17, Edoardo Tagome wrote:
    > 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 ----
     
    Edoardo Tagome, Aug 21, 2013
    #2
    1. Advertising

  3. Edoardo Tagome

    Bo Persson Guest

    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
     
    Bo Persson, Aug 21, 2013
    #3
  4. On 21/08/2013 19:03, Paavo Helde wrote:
    > 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!
     
    Edoardo Tagome, Aug 23, 2013
    #4
    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. Mohamed Fysal

    NDK Network Development Kit - doubt

    Mohamed Fysal, Mar 17, 2005, in forum: C++
    Replies:
    1
    Views:
    381
    red floyd
    Mar 17, 2005
  2. Stef Mientki
    Replies:
    0
    Views:
    621
    Stef Mientki
    Nov 27, 2011
  3. 7stud --
    Replies:
    11
    Views:
    443
    7stud --
    Nov 9, 2007
  4. sterta bole
    Replies:
    0
    Views:
    829
    sterta bole
    Aug 27, 2012
  5. Edoardo Tagome
    Replies:
    0
    Views:
    208
    Edoardo Tagome
    Sep 17, 2013
Loading...

Share This Page