Home > front end >  Missing variable template argument list for template with linkedlist
Missing variable template argument list for template with linkedlist

Time:10-28

I don't understand why in a void function in the main I have an error on the client list that is "C missing variable template argument list" on each clientList when calling a function from linked list. The strange part is That I don't have errors in my others class except main.

template< class TYPE >
LinkedList<Client> clientList;

void showClientListState()
{
    cout << "Premier client: ";
    clientList.front().writeClientInfo();
    cout << "Dernier client: ";
    clientList.back().writeClientInfo();
    cout << "\n\n";
}

If you want to check the rest of my code:

LinkedList

#pragma once
#include "List.h"


template< class TYPE >
class LinkedList : public List<TYPE>        

{
public:
    LinkedList()
    {
        this->first = nullptr;
        this->last = nullptr;
        this->nbElements = 0;
    }
    ~LinkedList()
    {
        while (!isEmpty())
        {
            pop();
        }
    }

    void push(const Node<TYPE>& content)
    {
        Node<TYPE>* ptrClient = new Node<TYPE>(content);

        if (isEmpty()) {
            this->first = ptrClient;
            this->last = ptrClient;
        }
        else
        {
            last->setNext(ptrClient);
            last = ptrClient;
        }
        nbElements  ;
    }
    
    void pop()
    {
        if (isEmpty()) {
            throw EmptyList();
        }

        Node<TYPE>* tempNodel;
        tempNodel = first;

        if (first == last)
        {
            first = last = nullptr;
        }
        else
        {
            first = first->getNext();
        }
        delete tempNodel;
        nbElements--;
    }


    Node<TYPE>& front()
    {
        if (isEmpty())
        {
            throw EmptyList();
        }
        return *first->getContent();
    }

    Node<TYPE>& back()
    {
        if (isEmpty())
        {
            throw EmptyList();
        }
        return *last->getContent();
    }

    
    bool isEmpty() const
    {
        return (first == nullptr && last == nullptr);
    }

    int size() const
    {
        return nbElements;
    }

private:
    LinkedList(const ClientList&);
    Node<TYPE>* first;
    Node<TYPE>* last;
    int nbElements;
};

List interface

#pragma once
#pragma once
#include "EmptyList.h"
template< class TYPE >
class List
{
public:

    // Ajoute un élément à la fin de la file. 
    // Postconditions : nbElements devra être incrémenté de 1.
    virtual void push(const TYPE& content) = 0;

    // Enlève un élément au début de la file.
    // Précondition: nbElements > 0. Postcondition: nbElements sera décrémenté de 1.
    virtual void pop() = 0;

    // Retourne l’élément au début de la file.
    // Précondition: nbElements > 0.
    virtual TYPE& front() = 0;

    // Retourne l’élément à la fin de la file.
    // Précondition: nbElements > 0.
    virtual TYPE& back() = 0;

    // Retourne true si la file est vide ou false sinon.
    virtual bool isEmpty() const = 0;

    // Retourne le nombre d’éléments dans la file.
    virtual int size() const = 0;

};

node class

#pragma once
template< class TYPE >
class Node
{
public:
    Node(const TYPE& content)
    {
        setContent(content);
        setNext(nullptr);
    }

    ~Node()
    {
        delete content;
    }

    Node* getNext()
    {
        return this->next;
    }

    void setNext(Node* next)
    {
        this->next = next;
    }

    //Retourne le contenu de cet élément.
    TYPE* getContent()
    {
        return this->content;
    }

    //Change la contenu de cet élément.
    void setContent(const TYPE& content)
    {
        this->content = new TYPE(content);
    }

private:
    Node* next = nullptr;

    TYPE* content = nullptr;
};

And The Main that I can't touch except create linked list from template

#include <iostream>
#include <vld.h>
#include "LinkedList.hpp"
#include "Client.h"

using namespace std;

LinkedList<Client> clientList;

template< class TYPE >
void showClientListState()
{
   cout << "Premier client: ";
   clientList.front().writeClientInfo();
   cout << "Dernier client: ";
   clientList.back().writeClientInfo();
   cout << "\n\n";
}

template< class TYPE >
void manageClientAdd(const Client& client)
{
   cout << "Ajout d'un client\n";
   clientList.push(client);
   showClientListState();
}
template< class TYPE >
void manageClientRemove()
{
   cout << "Retrait d'un client\n";

   if (clientList.isEmpty())
   {
       cout << "La liste était déja vide\n\n";
       return;
   }

   clientList.pop();

   if (clientList.isEmpty())
       cout << "La liste est maintenant vide\n\n";
   else
       showClientListState();
}
template< class TYPE >
void main()
{
   setlocale(LC_ALL, "fr-CA");
   cout << "\nUtilisation de la liste de clients.\n\n";
   
   Client client1(1, "Télesphore", "LeGamer");
   Client client2(2, "Herménégide", "LaVedette");
   Client client3(3, "Leopoldine", "LaSportive");
   


   Client client4(4, "Amidala", "LaPrincesse");

   manageClientAdd(client1);
   manageClientAdd(client2);
   manageClientAdd(client3);
   manageClientAdd(client4);

   for (int i =0; i < 5; i  )
       manageClientRemove();

   system("Pause");
}

CodePudding user response:

Instead of

template<class TYPE>
LinkedList<Client> clientList;

you want to write

LinkedList<Client> clientList;

That is, remove the template<class TYPE> before the declaration of clientList. You don't want clientList to be a new variable template, you want it to be one variable that happens to have a type that's an instance of a template.

CodePudding user response:

In addition to Brians answer, in your "Main" code, remove all "template< class TYPE >"s.

Thus:

template< class TYPE >
void showClientListState()
{
...

must be

void showClientListState()
{
...

and the same for

template< class TYPE >
void manageClientAdd(const Client& client)
{
...
template< class TYPE >
void manageClientRemove()
{
template< class TYPE >
void main()
{

None of those functions should or can depend on any template parameter. The only thing in your code depending on a template parameter is LinkedList directly. But LinkedList<Client> ALSO does not depend on a template parameter because you fully specified its type using Client.

Thus, for showClientListState change:

template< class TYPE >
LinkedList<Client> clientList;

void showClientListState()
{

into

#include "Client.h"

LinkedList<Client> clientList;

void showClientListState()
{

Those are just the obvious errors. For a full answer, you must produce the entire problem. But don't just upload more code. Produce a minimally reproducible example. This is important to ask questions in general.

  • Related