template class data member not found!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • alex

    #1

    template class data member not found!

    Code:
    =============== =============== ===========
    template <class T, int Len>
    class COciVariable
    {
    public:
    T m_Var[Len];
    };

    template <class T>
    class COciVariableEx : public COciVariable<T, 1>
    {
    public:
    void operator = ( const T & Var )
    {
    m_Var = Var;
    }
    };
    =============== =============== =============
    Platform:
    =============== =============== =============
    Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.3/specs
    Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
    --infodir=/usr/share/info --enable-shared --enable-threads=posix
    --disable-checking --with-system-zlib --enable-__cxa_atexit
    --disable-libunwind-exceptions --enable-java-awt=gtk
    --host=i386-redhat-linux
    Thread model: posix
    gcc version 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)
    =============== =============== ==============
    Error:
    =============== =============== ==============
    In file included from main.cpp:1:
    test.h: In member function `void COciVariable2Ex <T>::operator=( const
    char&)':
    test.h:35: error: `m_Var' undeclared (first use this function)
    test.h:35: error: (Each undeclared identifier is reported only once for
    each function it appears in.)


    Why? PLS help me! Thx!!!

  • Greg

    #2
    Re: template class data member not found!


    alex wrote:[color=blue]
    > Code:
    > =============== =============== ===========
    > template <class T, int Len>
    > class COciVariable
    > {
    > public:
    > T m_Var[Len];
    > };
    >
    > template <class T>
    > class COciVariableEx : public COciVariable<T, 1>
    > {
    > public:
    > void operator = ( const T & Var )
    > {
    > m_Var = Var;
    > }
    > };[/color]

    Implement operator= like so:

    void operator= ( const T & Var )
    {
    this->m_Var = Var;
    }

    Greg

    Comment

    • dkotlyarov@gmail.com

      #3
      Re: template class data member not found!

      Standard C++ says that nondependent names are not looked up in
      dependent base classes. To correct the code you're to make m_Var
      dependent. You can do it in two ways: this->m_Var or COciVariable<T,
      1>::m_Var.
      See "two-phase lookup" to get additional information.

      Comment

      • Julien Hamaide

        #4
        Re: template class data member not found!

        And as you m_Var is an array you should do this

        public:
        void operator = ( const T & Var )
        {
        this->m_Var[ 0 ] = Var;
        }
        };

        alex a écrit :[color=blue]
        > Code:
        > =============== =============== ===========
        > template <class T, int Len>
        > class COciVariable
        > {
        > public:
        > T m_Var[Len];
        > };
        >
        > template <class T>
        > class COciVariableEx : public COciVariable<T, 1>
        > {
        > public:
        > void operator = ( const T & Var )
        > {
        > m_Var = Var;
        > }
        > };
        > =============== =============== =============
        > Platform:
        > =============== =============== =============
        > Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.3/specs
        > Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
        > --infodir=/usr/share/info --enable-shared --enable-threads=posix
        > --disable-checking --with-system-zlib --enable-__cxa_atexit
        > --disable-libunwind-exceptions --enable-java-awt=gtk
        > --host=i386-redhat-linux
        > Thread model: posix
        > gcc version 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)
        > =============== =============== ==============
        > Error:
        > =============== =============== ==============
        > In file included from main.cpp:1:
        > test.h: In member function `void COciVariable2Ex <T>::operator=( const
        > char&)':
        > test.h:35: error: `m_Var' undeclared (first use this function)
        > test.h:35: error: (Each undeclared identifier is reported only once for
        > each function it appears in.)
        >
        >
        > Why? PLS help me! Thx!!!
        >[/color]

        Comment

        Working...