简体   繁体   English

模板类编译错误

[英]Template Class Compiling errors

When I compile I keep getting errors about my template in my linked list class. 编译时,我在链接列表类中不断收到有关模板的错误。 For a project I'm doing we have to make a linked list and basically just modify it with different functions but I have no idea how to get it to work with my template and to run the functions in my main(). 对于我正在做的项目,我们必须制作一个链表,基本上只是用不同的函数对其进行修改,但是我不知道如何使它与模板一起使用并在main()中运行这些函数。 So here is the code. 所以这是代码。

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

  }

so I have all my functions in the header file because that's the format they asked us to do it in. My main is as follows: 所以我将所有函数都保存在头文件中,因为这是他们要求我们使用的格式。我的主要内容如下:

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

}

So basically I don't know how to run the functions in my header file in my main and it compile correctly. 因此,基本上我不知道如何在我的main头文件中运行这些函数,并且编译正确。 The errors its giving me are related to the LinkedList part with the std::string. 它给我的错误与带有std :: string的LinkedList部分有关。 So I'm just not sure how to initialize that the correct way to get the functions to work. 因此,我只是不确定如何初始化使函数正常工作的正确方法。 I'm not so worried now if the code for the functions are right, I will do the debugging and figure that out I just want to be able to test my code but it won't compile! 我现在不担心这些函数的代码是否正确,我将进行调试并弄清楚,我只是想能够测试我的代码,但无法编译! If anyone could steer me in the right direction that would be awesome. 如果有人可以引导我朝正确的方向前进,那将是非常棒的。 Thanks. 谢谢。

ERRORS: 错误:

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>          ]
~List() 
{
    clear();
}

should be: 应该:

~LinkedList() 
{
    clear();
}

The compiler errors pretty clearly point you to the problem: 编译器错误很明显地指出了问题所在:

~List() 
{
    clear();
}

Your type is named LinkedList , not List . 您的类型名为LinkedList ,而不是List

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM