首页 > 解决方案 > 如何从单个链表头文件在 main 中创建对象

问题描述

我试图创建一个对象,以便我可以使用基于链接列表的头文件中的函数。我在 Visual Studio 上遇到的错误是 C2955、C2133 和 C2512。

到目前为止,我所做的只是重新安排模板的去向。

//list.h
#define LIST_H
#include "node.h"

template<typename T>
class List  //single linked list
{
private:
     node<T> head; 
     node<T> tail;
     int numofNodes;

public:

    List() {   //constructor
        head = NULL;
        tail = NULL;
        //temp = NULL;
        numofNodes = 0;
    }

    /functions for add,delete,display,search,etc/
};
#endif


//main.cpp
#include "stdafx.h"
#include <iostream>
#include <string>
#include "node.h"
#include "List.h"
#include "stack.h"
#include "currency.h"
using namespace std;

int main()

{ 
    List obj;
    return 0;
}

标签: c++

解决方案


使用模板时,需要指定类型:

std::vector<float> f;
std::List<int> my_list;
std::list<double> my_list;

实例化中使用的数据类型template介于<和之间>

搜索您最喜欢的 C++ 参考以获取实例化 模板


推荐阅读