【问题标题】:Template Class Compiling errors模板类编译错误
【发布时间】:2014-03-15 17:16:50
【问题描述】:

当我编译时,我在我的链表类中不断收到关于我的模板的错误。对于我正在做的一个项目,我们必须制作一个链表,基本上只是用不同的函数修改它,但我不知道如何让它与我的模板一起工作并在我的 main() 中运行函数。所以这里是代码。

#pragma once

template <typename ItemType>

class LinkedList {

private:

    struct Node {
    ItemType info;
    Node* next;
    Node* prev;
  };


  Node* head;
  Node* tail;
  int size;

public:
  LinkedList() :

    size(0),
    head(NULL),
    tail(NULL)

  { }
  ~List() 
   {
        clear();
   }
  void clear()
  {
    Node* n = tail;
    while(n != NULL)
    {
        Node *temp = n;
        n = n->prev;
        delete temp;
    }
    head = NULL;
    tail = NULL;
  }
  void print()
  {
      int count = 0;
      Node* temp = head;
      while(temp != NULL)
      {
         cout << "node " << count << ": " << temp->info << endl;
         temp = temp->N;
         count++;
      }
  }
  void insert(int index, const ItemType& item) 
  {
     int count = 0;
     Node* n = head;
     while(n != NULL)
    {
        if(count == index)
        {
            break;
        }
        n = n->next;
        count++;
    }
    if(n == NULL)
    {
        return;
    }

    Node* p = new Node;
    p->info = item;
    p->next = n->next;
    p->prev = n;
    n->next = p;

  }

所以我的所有函数都在头文件中,因为这是他们要求我们使用的格式。我的主要内容如下:

#include <iostream>
#include <string>
#include <fstream>

#include "LinkedList.h"

using namespace std;


int main(int argc, char* argv[])
{
    string cmd;
    int index;
    string item;
    LinkedList<string> list; // I don't know if this is how its suppose to be done.


    while(cin >> cmd)
    {
        if (cmd == "clear")
        {
            cout << "clear" << endl;        
        }
        if (cmd == "insert")
        {
            cin >> index;
            cin >> item;
            list.insert(index, item);       
        }           
        if (cmd == "print")
        {
            cout << "print" << endl;
        }
    }



    system("pause");
    return 0;

}

所以基本上我不知道如何在我的主文件中运行我的头文件中的函数并且它可以正确编译。它给我的错误与带有 std::string 的 LinkedList 部分有关。所以我只是不确定如何初始化正确的方法以使功能正常工作。如果函数的代码是正确的,我现在并不担心,我会进行调试并弄清楚我只想能够测试我的代码但它不会编译!如果有人能引导我朝着正确的方向前进,那就太棒了。谢谢。

错误:

1>  project5.cpp
1>c:\users\marsh\documents\cs\project5\project5\linkedlist.h(28): error C2523: 'LinkedList<ItemType>::~List' : destructor tag mismatch
1>          c:\users\marsh\documents\cs\project5\project5\linkedlist.h(133) : see reference to class template instantiation 'LinkedList<ItemType>' being compiled
1>c:\users\marsh\documents\cs\project5\project5\linkedlist.h(28): error C2523: 'LinkedList<ItemType>::~List' : destructor tag mismatch
1>          with
1>          [
1>              ItemType=std::string
1>          ]
1>          c:\users\marsh\documents\cs\project5\project5\project5.cpp(15) : see reference to class template instantiation 'LinkedList<ItemType>' being compiled
1>          with
1>          [
1>              ItemType=std::string
1>          ]

【问题讨论】:

  • 请发布错误。
  • 好的,我发布了错误。
  • 您的意思是说temp = temp-&gt;next 而不是temp = temp-&gt;N;?在你的print 函数中?
  • 是的,我最近也修复了这个问题

标签: c++ templates compiler-errors linked-list


【解决方案1】:
~List() 
{
    clear();
}

应该是:

~LinkedList() 
{
    clear();
}

【讨论】:

  • 为了彻底,您能否将我对 OP 问题的评论纳入其中?
【解决方案2】:

编译器错误非常清楚地指出了问题所在:

~List() 
{
    clear();
}

你的类型被命名为LinkedList,而不是List

【讨论】:

  • 哇...要热爱生活,谢谢伙计。现在让我的头撞到墙上几次。
  • 我也遇到了同样的问题,不得不用谷歌搜索,发现是错字:/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多