多文件 C++ 链表

标签 c++ file linked-list

我对使用多文件有点陌生。我有这个非常简单的链表代码,但是当我调试时,它“停止工作”。

这个问题我之前遇到过好几次了。我想知道我的“链表”代码有问题吗?还是多文件组织有问题?

如有任何帮助,我们将不胜感激。

======================================

//linkedListMAIN.cpp

#include "linkedlist.cpp"

void main()
{
linkList<int> l;

l.append(5);
l.traverse();
}

======================================

//linkedList.h    

#include<iostream>
using namespace std;

template <class T>
class linkList
{
private:
struct node
{
    T data;
    node *next;
};
node *head;
node *tail;
int noOfEl;
public:
linkList()
{
    noOfEl = 0;
    head=tail=NULL;
}

void traverse();
int length();
void insertAt(T, int);
T delAt(int);
void append(T);
void clear();
};

======================================

//linkedList.cpp    
#include "linkedlist.h"

template <class T>
void linkList<T>:: traverse()
{
node<T> *current=head;

if(head == NULL)
{
    cout<<"List empty."<<endl;
}

while(current != NULL)
{
    cout<<current->data;
    current = current->next;
}
}


template <class T>
void linkList<T>::append(T data)
{
node< *newNode= new node;

newNode->next = NULL;
tail->next = newNode;
tail = newNode;

noOfEl++;  
 }

最佳答案

您不应在 inkedListMAIN.cpp 中包含 .cpp,而应包含 header (.h)。此外,除非您使用的是 c++11,否则您必须将模板类的类定义放在 header 中。

关于多文件 C++ 链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14417678/

相关文章:

c++ - 如何使存储数组的二进制搜索稳定

java - 将文件读入 Spring Controller 。使用相对路径

php - 在 PHP 中存储文件结构

linux - 根据文件名对 Bash 中的 271,568 个文件进行排序

c - 向链表插入节点的函数

C++ Qt : bitwise operations

c++ - 在OpenGL中绘制多个三角形

c - 没有温度的链表反向

c++ - 如何将数字转换为 0.mynumber

c - 如何在一个循环中声明多个不同大小的数组?