Menu

`elemNo' undeclared (first use this function)

2008-10-19
2012-09-26
  • Hatim Palitanawala

    Hi All,

    This is the first time I am using Dev-Cpp, prior to this I had used VC++ version 6 and IBM mainframes compiler. The code extract below worked fine in the later two, but its giving me an error message for a variable which was defined in the base class saying the variable is undeclared... I have modified the standard headers as per the instructions in the Dev-Cpp FAQ, it did away with most of the errors except this one which remains elusive.
    

    199 C:\Projects\CPP\compiler\LRPAR\H\llist.h `elemNo' undeclared (first use this function)

    ////////////////////////////////////////////////////////////////////////////////
    template <class Type>
    class List {
    protected:
    Link<Type> first;
    Link<Type>
    last;
    int elemNo;
    public:
    friend class LIterator<Type>;

        List() { first = last = 0; elemNo = 0; }
        ~List();
        virtual int  Add      (const Type Object);
        virtual int  Match    (const Type Object) const;
        inline  int  No       () const {return elemNo;}
        inline  bool Empty    () const {return (!first);}
        inline  Type GetFirst () const;
        inline  Type GetLast  () const;
        virtual Type operator [] (int Index) const;
        virtual bool operator == (const List&lt;Type&gt;&amp; Target);
        virtual void operator += (const List&lt;Type&gt;&amp; Target);
        virtual int  operator += (const Type Object) {return this-&gt;Add(Object); }
    

    };

    ////////////////////////////////////////////////////////////////////////////////
    template <class Type>
    class ListSorted: public List<Type>
    {
    public:
    virtual int Add(const Type Object);
    };

    ////////////////////////////////////////////////////////////////////////////////
    template <class Type>
    int ListSorted<Type>::Add(const Type Object)
    {
    Link<Type>* NewObject = new Link<Type>(Object, 0);

    if (elemNo == 0)
    {
        first = last = NewObject;
        last-&gt;next = NULL;
        elemNo++;
        return 0;
    }
    

    ..........

    Kindly let me know in case more information is required.

    Regards,
    Hatim

     
    • cpns

      cpns - 2008-10-19

      Template support in GCC 3.x is not too hot, which is probably the cause of your problems. I think that you need to explicitly inline ListSorted<Type>::Add.

      > I have modified the standard headers as per the instructions in the Dev-Cpp FAQ

      You did what!? Post a link to the instruction. You should not have to modify any headers. However whatever you did it does not seem to be the issue here.

      > 199 C:\Projects\CPP\compiler\LRPAR\H\llist.h `elemNo' undeclared (first use this function)

      Post the complete text from the "Compile Log" tab, not one line from the "Compiler" tab.

      The error occurred at line 199, so this is obviously just a fragment. Unfortunately we cannot tell which is line 199! Also being a fragment it is uncompilable, so we cannot reproduce or verify your finding. Staring at the code is not helping.

      Start by posting the whole log, and identifying line - it may provide a clue for us to start with.

      Some other unrelated points (ignore if you wish):

      1) The inline keyword is redundant for functions defined within the class definition - your choice however.

      2) Using Initialiser lists is potentially more efficient that 'body initialisation'. eg:

      3) List() : first(0), last(0), elemNo(0){ }

      4) The C++ STL already provides a <list> template class.

      Clifford

       
    • Hatim Palitanawala

      Thanks Clifford. I prefixed "this->" on elemNo and it seems to work fine. But now I am facing some problems where I have used templates. Working on this more to see if I can solve the problem myself.

      I shall work on using the STL as suggested by you. Thanks once again!

       

Log in to post a comment.