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 7823067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:07:03+00:00 2026-06-02T08:07:03+00:00

I’m having a hard time with templates and would like to ask some help.

  • 0

I’m having a hard time with templates and would like to ask some help.

First of all, I’m implementing a dynamic list, where the “data” of the node can be used to point whatever I need. It’s a generic list to all the application, and the way I found to make it was with templates.

Here is the .h where I implemented it:

#ifndef DYNAMICLIST_H_
#define DYNAMICLIST_H_

// **** Node ****
template <typename T>
class Node
{
public:
    Node(int _nodeID, T* _data)
    {
        nodeID = _nodeID;
        next = NULL;
        data = _data;
    }

    Node()
    {

    }

    ~Node()
    {
        if(data)
            delete data;
    }

    //sets e gets

    Node* GetNext() const
    {
        return next;
    }

    void SetNext(Node* _next)
    {
        next = _next;
    }

    int GetNodeID() const
    {
        return nodeID;
    }

    T* GetNodeData() const
    {
        return data;
    }

    void SetData(T* _data) //provavelmente não será usada
    {
        data = _data;
    }

private:

    int nodeID; //numero usado na busca
    Node* next; //ponteiro pro proximo node
    T* data;    //ponteiro pra uma imagem, uma mensagem, um som, o que quer que seja que precisar ser guardado
};





// **** DynamicList ****
class DynamicList
{
public:
    DynamicList(Node* _node)
    {
        this->SetHead(_node);
    }

    ~DynamicList()
    {
        this->DeleteList();
    }

    //sets e gets
    Node* GetHead() const
    {
        return head;
    }

    void SetHead(Node* _head)
    {
        head = _head;
    }

    Node* GetNode(int _nodeID) const
    {
        Node *Finder;
        if (!head)
            return false;
        Finder = head;
        for (; Finder; Finder = Finder->GetNext()) 
        {
            if (Finder->GetNodeID() == _nodeID) //se o nome for igual, retorna o ponteiro do node
                return Finder;
        }
        return false;
    }

    void NewNode (Node* _node) // Método para adicionar um elemento novo ao final da lista.
    {   
        if (!this->GetHead())
        {
            this->SetHead(_node);
        }
        else
        {
            Node *Finder = this->GetHead();
            while (Finder->GetNext())
                Finder = Finder->GetNext();
            Finder->SetNext(_node);
        }
    }

    //outras
    bool Delete(int _nodeID)
    {
        Node* deleter;
        Node* aux;

        if(!this->GetNode(_nodeID) ) //não há mensagem com esse Id na lista
            return false;

        if(head->GetNodeID() == _nodeID)
        {
            if (!head->GetNext()) //se não houver outro nó além da head, não deletar
            {
                delete head;
                head = NULL;
            }
            else
            {
                aux = head->GetNext(); //se head tiver a mensagem a ser deletada, head agora aponta para o nó seguinte
                delete head;
                head = aux;
                return true;
            }
        }
        else
        {
            deleter = head;

            while (deleter) //enquanto não chegar no fim
            {
                if (deleter->GetNext()) //se há um próximo node
                {
                    if (deleter->GetNext()->GetNodeID() == _nodeID) //verifica se o próximo tem a node mensagem procurada
                    {
                        aux = deleter->GetNext()->GetNext(); //caso tenha, perde o ponteiro do proximo node e pega o do seguinte
                        delete deleter->GetNext();
                        deleter->SetNext(aux);
                        return true;
                    }
                }
                deleter = deleter->GetNext(); //passa para o próximo
            }
            return false;
        }
    }

    void DeleteList()
    {
        Node* superDeleter;
        Node* aux;

        superDeleter = head;

        if(!superDeleter)
            return;
        else
        {
            while(superDeleter->GetNext()) //se há um próximo node
            {
                aux = superDeleter->GetNext(); //guarda o proximo
                delete superDeleter; //deleta o primeiro
                superDeleter = aux; //aponta para o proximo
            }
            delete superDeleter; //deleta o ultimo restante
        }
    }

private:

    Node* head;
};

#endif

My intention was to use template to make data a generic pointer to keep the class in the queue.
Check the methods in the class. Most of them use Node*, which I cursed by making it a template.
Now the Node* can’t be used without specifying the type in the declarations?
Is there a way to declare Node* so I can use it in this methods? My biggest problem is that inside the methods I can’t specify the node template, it should work with all types.

Thanks.

  • 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-02T08:07:05+00:00Added an answer on June 2, 2026 at 8:07 am

    My biggest problem is that inside the methods I can’t specify the node template, it should work with all types

    Yes, you can: just make DynamicList a template as well and then use Node<T>*. That way it will work with all types.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to run a str_replace or preg_replace which looks for certain words
We're building an app, our first using Rails 3, and we're having to build
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:

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.