【问题标题】:Missing variable template argument list for template with linkedlist带有链表的模板缺少变量模板参数列表
【发布时间】:2021-12-13 05:07:33
【问题描述】:

我不明白为什么在 main 的 void 函数中,调用函数时,每个 clientList 上的客户端列表都有一个错误,即“C++ 缺少变量模板参数列表”从链表。奇怪的是,除了 main 之外,我在其他类中没有错误。

template< class TYPE >
LinkedList<Client> clientList;

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

如果你想检查我的其余代码:

链表

#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;
};

列表界面

#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;

};

节点类

#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;
};

除了从模板创建链表之外我无法触及的 Main

#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");
}

【问题讨论】:

  • 什么是Client?你省略了它的声明。
  • "on each clientList" -- 我猜你写的不是你的意思。你的意思是“在每一行使用 clientList”,不是吗?然而,由于clientList 是一个模板,“每个clientList”更接近于“每个实例化 clientList”的意思。您可能不打算将clientList 用作模板,这表明语言精度的重要性。你应该写你的意思,而不是使用懒惰的短语,希望读者愿意填写缺失的单词并填写预期的单词。
  • 不应触碰客户端
  • 我不明白您所说的“不应触碰客户” 是什么意思。即使您的代码使用Client,您也不应该碰它?

标签: c++ templates linked-list nodes


【解决方案1】:

代替

template<class TYPE>
LinkedList<Client> clientList;

你想写

LinkedList<Client> clientList;

即去掉clientList声明前的template&lt;class TYPE&gt;。您不希望 clientList 成为新的 variable template,而是希望它是一个恰好具有模板实例类型的变量。

【讨论】:

  • 在哪里看到的模板 LinkedList clientList;
  • @NicolasPellerin "你在哪里看到模板 LinkedList clientList;" -- 呃,你的第一个代码块的前两行?如果您正在阅读代码,这很难错过。
  • @NicolasPellerin 这是一个单独的问题,因为LinkedList 没有定义覆盖void push(const TYPE&amp; content) 的成员函数。请注意,LinkedList::push(const Node&lt;TYPE&gt;&amp;) 是一个 不同 函数(签名不匹配)。如果您在LinkedList::push 上使用override specifier,您会得到更多信息的编译器错误
  • 它不起作用,您可以阅读新版本吗?我真的不知道是我的问题。
  • 布莱恩你在吗?
【解决方案2】:

除了 Brians 的回答,在您的“主要”代码中,删除所有“模板”。

因此:

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

必须

void showClientListState()
{
...

同样的

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

这些函数都不应该或不能依赖于任何模板参数。您的代码中唯一依赖于模板参数的是LinkedList。但是LinkedList&lt;Client&gt; ALSO 不依赖于模板参数,因为您使用Client 完全指定了它的类型。

因此,对于showClientListState 更改:

template< class TYPE >
LinkedList<Client> clientList;

void showClientListState()
{

进入

#include "Client.h"

LinkedList<Client> clientList;

void showClientListState()
{

这些只是明显的错误。要获得完整的答案,您必须提出整个问题。但不要只是上传更多代码。制作一个可重复性最低的示例。这对于提出一般性问题很重要。

【讨论】:

  • 对不起,我不太擅长模板。这是我的第一次练习。
  • 但是现在,它表明我的表达式需要一个类型是客户端,但我不知道我做错了什么
  • 您必须发布来自编译器的确切错误消息以及此处涉及的源代码行以获得更多帮助。
猜你喜欢
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 2014-09-08
  • 2016-09-18
  • 1970-01-01
  • 1970-01-01
  • 2014-04-21
  • 1970-01-01
相关资源
最近更新 更多