1

I am having some problems in C++ coding. I am using 2 classes that will perform function dynamically through Templates. The nodes are also 2 structures for dynamic usage. In this Program: Child class is not inheriting/initializing the variables of Parent class.
I am using Dev C++ with GCC 11 compiler

I'm having these errors in my code:

In member function void linkedListStart<Type>::inserFirst(const Type&):
[Error] `first` was not declared in this scope.
[Error] `last` was not declared in this scope.

Here my code:

    #include <iostream>
    #include <string.h>
    using namespace std;

    struct node{
        int num;
        string name;
    };

    template <class Type>
    struct nodeType{
        Type info;
        nodeType<Type> *link;
    };


    //*****************  class linkedList   ****************
    template <class Type>
    class linkedList{
        public:
            linkedList(){
                first = nullptr;
                last = nullptr;
            }
        protected:
            nodeType<Type> *first;
            nodeType<Type> *last;
    };


    //***************  class Unordered linkedList   ***************
    template <class Type> 
    class linkedListStart: public linkedList<Type>{
        public:
            void insertFirst(const Type& newItem){
                nodeType<Type> *newNode = new nodeType<Type>;
                newNode->info = newItem;

                if(first == nullptr){
                    newNode->link = nullptr;
                    first = last = newNode; 
                }else{
                    newNode->link = first;
                    first = newNode;
                }
            }
    };


    int main(){
        node n1;
        n1.num = 5;
        n1.name = "John Doe";
        linkedListStart<node> l1;
        l1.insertFirst(n1);
        return 0;
    }

I would Highly appreciate your Help!

  • TL;DR of the dupe: Unqualified name lookup doesn't look into templated base classes. Add `this->` to refer to members of your templated base class. – Max Langhof Apr 30 '19 at 13:30
  • Oh yes it worked! Thanks @MaxLanghof for your Kind Help. I was really stuck with it. – Syed Abdullah Apr 30 '19 at 13:35

0 Answers0