Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7992501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:34:51+00:00 2026-06-04T13:34:51+00:00

I am trying to create a LinkedList class with polymorphism and inheritance from Stack

  • 0

I am trying to create a LinkedList class with polymorphism and inheritance from Stack and Queue header files. This is the first time I’ve tried to use templates but keep getting errors like “class template has already been defined” or “abstract class type is not allowed”. How do I implement a template since I’m obviously doing it incorrectly?

// LinkedList.h File
#include "Stack.h"
#include "Queue.h"
#include "Node.h"

using namespace std;

template <typename T>
class LinkedList : public Queue<T>, public Stack<T>
{
public:
    LinkedList();
    ~LinkedList(void);

protected:
    Node<T> *first;
    Node<T> *last;
    int numItems;
};

// LinkedList.cpp File
#include "LinkedList.h"

using namespace std;

template <typename T>
class LinkedList
{
    LinkedList()
    {
        first = NULL;
        last = NULL;
        numItems = 0;
    }

    LinkedList::~LinkedList(void)
    {
        while (first != NULL)
        {
            Node* cur = first;
            first = first->next;
            delete cur;
        }
    }

    LinkedList::clear() {}    
    LinkedList::size() {}

    // Stack Functions
    LinkedList::push(T item) {}    
    LinkedList::pop() {}    
    LinkedList::top() {}

    // Queue Functions
    LinkedList::enqueue(T item) {}    
    LinkedList::dequeue() {}    
    LinkedList::peek() {}
}

// Stack.h File (Queue is the same except push/pop/top = enqueue/dequeue/peek)
#pragma once

template <typename T> class Stack
{

public:

  virtual ~Stack() {}

  virtual int size() = 0;
  virtual void clear() = 0;
  virtual void push(T item) = 0;
  virtual T pop() = 0;
  virtual T top() = 0;

};

Updated Code

// LinkedList.h File
#pragma once
#include "Stack.h"
#include "Queue.h"
#include "Node.h"

template <typename T>
class LinkedList : public Queue<T>, public Stack<T>
{
public:
    LinkedList();
    ~LinkedList(void);

    void clear();
    int size();

    void push(T item);
    T pop();
    T top();

    void enqueue(T item);
    T dequeue();
    T peek();


protected:
    Node<T> *first;
    Node<T> *last;
    int numItems;
};

// LinkedList.cpp File
#include "LinkedList.h"

class LinkedList
{
    LinkedList::LinkedList()
    {
        first = NULL;
        last = NULL;
        numItems = 0;
    }

    LinkedList::~LinkedList(void)
    {
        while (first != NULL)
        {
            Node* cur = first;
            first = first->next;
            delete cur;
        }
    }

    LinkedList::clear(){}
    LinkedList::size(){}

    // Stack Functions
    void LinkedList::push(T item){}
    T LinkedList::pop(){}
    T LinkedList::top(){}

    // Queue Functions
    void LinkedList::enqueue(T item){}
    T LinkedList::dequeue(){}
    T LinkedList::peek(){}
}


// Stack.h File (Queue is the same except push/pop/top = enqueue/dequeue/peek)
#pragma once

template <typename T> class Stack
{

public:

  virtual ~Stack() {}

  virtual int size() = 0;
  virtual void clear() = 0;
  virtual void push(T item) = 0;
  virtual T pop() = 0;
  virtual T top() = 0;

};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:34:53+00:00Added an answer on June 4, 2026 at 1:34 pm

    I see

    // LinkedList.cpp File
    

    Template definitions have to be visible on every translation unit, you cannot place their definitions in a cpp file and expect it to work (like it does for regular classes). Definitions for all template functions should be present at a header file.

    Also, the compiler error is correct. You are writing definitions for two LinkedList classes. I assume you were looking for:

    LinkedList::LinkedList(){ ... }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I trying create a class derivated from System.Web.UI.Page and in override Render i set
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
Trying to create a background-image slideshow and am getting this error... This is the
I'm trying to create a linked list class in Eclipse but I can't get
I am trying to create an API for chaining tasks. First, I designed a
I'm basically trying to create a linked list from a text file and add
I created a singly LinkedList class with ruby. Everything went well till trying to
I'm quite sure this a simple issue, but I am trying to create a
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
I am trying create a delegate representation of constructor by emitting a Dynamic Method,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.