【问题标题】:Class Template Issues类模板问题
【发布时间】:2019-11-09 16:24:00
【问题描述】:

所以我正在使用链接列表,并且当它们使用 int 时,我已经获得了所有功能。我已经开始调整程序以遵循模板,以便它可以与其他给定的数据类型(例如 double)一起使用,但这已经破坏了整个程序,我不确定为什么,如果你知道它为什么会导致这种情况以及如何解决将不胜感激。

#include <iostream>
#include "List.h"
using namespace std;
int main()
{
   List<int> linkedList;
   linkedList.insertByPos(0,16);
   linkedList.insertByPos(1,17);
   linkedList.insertByPos(2,18);
   linkedList.printList();

   linkedList.reverse();
   cout << endl;
   linkedList.printList();

   cout << "The position is " << linkedList.search(17) << endl;
   linkedList.insertByPos(3,19);
   linkedList.printList();
   linkedList.RemoveByPos(2);
   cout << endl;
   linkedList.printList();
    return 0;
}

#ifndef LIST_H
#define LIST_H
template <class T>
class List
{
private:
    struct Node
    {
        T data;
        Node* next;
    };
    Node* head;
    Node* curr;
    Node* temp;
public:
    List<T>();
    List(const List<T>& other);
    void printList();
    void reverse();
    int search(T value);
    void insertByPos(int pos, T value);
    void RemoveByPos(int pos);

};

#endif // LIST_H


#include "List.h"
#include <iostream>
using namespace std;
template <class T>
List<T>::List()
{
    head = nullptr;
    curr = nullptr;
    temp = nullptr;
}
template <class T>
List<T>::List(const List& other)
{
    cout << "copy constructor called:\n";
    if(other.head == nullptr) return;
    Node* dummyHead = new Node;
    curr = dummyHead;
    Node* othcurr = other.head;
    for(; othcurr!=nullptr; othcurr = othcurr->next)
    {
        curr->next = new Node;
        curr = curr->next;
        curr->data = othcurr->data;
        curr->next = nullptr;
    }
    head = dummyHead->next;
    delete dummyHead;
}
template <class T>
void List<T>::printList()
{
    curr = head;
    while(curr != nullptr)
    {
        cout << curr->data << endl;
        curr = curr->next;
    }
}
template <class T>
void List<T>::reverse()
{
    Node* next = nullptr;
     curr = head;
    while(curr != nullptr)
    {
        next = curr->next;
        curr->next = temp;
        temp = curr;
        curr = next;
    }
    head = temp;
    temp = nullptr;
}
template <class T>
int List<T>::search(T value)
{
    int counter = 0;
    curr = head;
    while(curr != nullptr && curr->data != value)
    {
        curr = curr->next;
        counter++;
    }
    if(curr == nullptr)
    {
        return -1;
    }
    return counter;
}
template <class T>
void List<T>::insertByPos(int pos, T value)
{
    curr = head;
    Node* n = new Node;
    n->data = value;
    int counter = 0;
    int bounds = 0;
    while(curr != nullptr)
    {
        bounds++;
        curr = curr->next;
    }
    if(bounds <= pos)
    {
       pos = bounds;
    }
    curr = head;
    while(counter != pos)
        {
            counter++;
            temp = curr;
            curr = curr->next;
        }
        n->next = curr;
                if(counter == 0)
                {
                    head = n;
                }
                else
                {
                    temp->next = n;
                }
    temp = nullptr;
}
template <class T>
void List<T>::RemoveByPos(int pos)
{
   Node* delPtr = nullptr;
   curr = head;
   temp = head;
   int counter = 0;
   int bounds;
     while(curr != nullptr)
    {
        bounds++;
        curr = curr->next;
    }
    if(pos >= bounds)
    {
        cout << "Bad pos value";
    }
    else
        {
                curr = head;
   while(counter != pos)
   {
       counter++;
       temp = curr;
       curr = curr->next;
   }
   delPtr = curr;
   curr = curr->next;
   temp->next = curr;
      if(counter == 0)
   {
       head = head->next;
       temp = nullptr;
   }


   delete delPtr;
        }

}

【问题讨论】:

  • 你得到什么样的错误?它不编译吗?如果是这样,请添加编译器错误消息。还是它编译但在运行时崩溃?如果是这样,请添加您获得的任何诊断信息。还请添加代码以显示您如何使用您的列表。
  • 我在 'linkedList' 之前缺少模板参数|并且未在此范围内声明“linkedList”

标签: c++ class templates


【解决方案1】:

需要在“列表”后加“T”

List::List() void -> List<T>::List()

void List::printList() -> void List<T>::printList()

【讨论】:

  • 我已将它添加到所有函数中,现在当我在 main 中声明我的对象时收到一个错误,表明它没有在此范围内声明。
【解决方案2】:

由于模板存在多个问题。首先,当你实例化 linkedList 时,你必须告诉编译器 T 应该有哪种类型。例如通过写作

List<int> linkedList;

出于同样的原因,你必须改变

List<T>::List(const List& other)
// to 
List<T>::List(const List<T>& other)

此外,您不能(很容易,有一些方法)将模板化函数的定义放在 cpp 文件中。您必须将它们放在头文件中。更多阅读 https://isocpp.org/wiki/faq/templates#templates-defn-vs-decl 你的代码在编译后会抛出链接器错误。

【讨论】:

  • 当我指定我的 LinkedList 对象时,它不再识别我声明的任何函数你知道这是为什么吗?
  • 我从上面复制了您的代码,将 List.cpp 放在 List.hpp 中,在 List 声明之后并更改了两行并且它起作用了:(在声明中): List( ); -> 列表(); (在定义中): List::List(const List& other) -> List::List(const List& other)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多