This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

C++ template class CCS4.1

hi,

When I desigen myself program, I meet the following question. Hope somebody can give me a help. Thanks. The software is CCS4.1 and DSP C5509A processor.

In my test project, there three files:

//First,

//stack.hpp

template<typename T, int size>
class Stack
{
private:
   T stack_[size];
   int top_;
 
public:
   Stack()
   {
      top_ = 0;
   }
 
   void push(T item)
   {
      if (top_ == size)
         { /* overflow */ }
      stack_[top_++] = item;
   }
 
   T pop()
   {
      if (top_ == 0)
         { /* underflow */ }
      return stack_[--top_];
   }
};

///////////////////////////////////////////////////////

//Second,

//stack1.hpp

#ifndef STACK1_HPP_
#define STACK1_HPP_

#include "stack.hpp"

template <typename T, int size>
class Stack1 :Stack<T, size>
{
 public:
 Stack1()
   {
      top_ = 0;
   }
 void pro()
 {
  top_=0;
 }
}
#endif /*STACK1_HPP_*/

//////////////////////////////////////////////////////////////////////////////

//Third,

//main.cpp

#include "stack1.hpp"
 
int main()
{
   Stack1<int, 100>  mainstack;
   mainstack.push(100);
   return 0;
}

the errors:

"..\stack1.hpp", line 12: error: identifier "top_" is undefined
"..\stack1.hpp", line 16: error: identifier "top_" is undefined
"../main.cpp", line 4: error: expected a ";"

 

Sometimes I also want to use "class" to instead of "typename ".

This only is a simple test program. Because this method can give me much convenient for my whole program.

I do not know it is that CCS do not support this method or it is incorrect using C++ template .

Hope someone can give me a help. Thanks.

  • Stack1 inherits Stack but the variable top_ is a private member of Stack so when you try to access it in Stack1, it is not visible at that point so the compiler tells you it is undefined.  Note the compiler is telling you it is undefined with respect to the available identifiers at the point where identifier lookup is being done, that being the scope of the Stack1 class.

    Also, inherited classes are by default inherited as private classes so even if the variable were declared public, it would still not be available during name lookup in the Stack1 class member functions.

    To correct your code, you should declare the top_ variable as protected or public and you should inherit the Stack class as protected or public.

    Exampletemplate< typename T, int size > class Stack
    {
      private:
        T stack_[size];
      protected:
        int top_;
      public:
        Stack( ) { top_ = 0; }
        void push(T item) { if (top_ < size) stack_[top_++] = item; }
        T pop() { return ( top_ > 0 ) ? stack_[--top_] : OVERFLOW_ERROR_VALUE; }
    };

    template < typename T, int size . class Stack1 : public Stack< T, size >
    {
      public:
        void pro() { top_=0; }
    };

    I have simplified your classes somewhat with respect to over/under flow but you should get the idea.

    Note that you don't need the constructor for Stack1 since the constructor for Stack will be called when a Stack1 type is instantiated by default.  Thus setting top_ to zero again in the Stack1 constructor was a bit redundant.

    Jim Noxon

  • hi Jim,

    Thanks for your help. With your help two errors have been  solved. In order to make the "top_" to be available, I correct my code as your help.

    However,there is also a error -  the "top_" variable is also invisible.

    template< typename T, int size > class Stack
    {
      private:
        T stack_[size];
      public:
        int top_;
        Stack( ) { top_ = 0; }
        void push(T item) { if (top_ < size) stack_[top_++] = item; }
    };

    #include "stack.hpp"
    template < typename T, int size > class Stack1 : public Stack< T, size >
    {
      public:
        void pro() { top_=0; }
    };

    Error:

    "..\stack1.hpp", line 8: error: identifier "top_" is undefined

    I select this way of programming because most parts of my code need to use this way programming. Of course all code has the same error.

    Hope your help.Thanks.

    Wanli Zhu

  • You can change the line in question to either

    void pro( ) { this->top_ = 0; }

    or

    void pro( ) { Stack< T, size >::top_ = 0; }

    The reason is that simply accessing the top_ member of the base class did not provide any distinction of the template parameters thus the actual template type was unknown at this point (seems like it should have been though).  Anyway, by using either the this reference or the fully qualified ID, the compiler can determine exactly what the template parameters are making the name lookup successful.

    There is nothing wrong with your way of programming.  You are doing exactly what C++ and Object Oriented Programming was intended to be used as.

    Jim Noxon