c++ - 如何从单个链表头文件在 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;
}
解决方案
使用模板时,需要指定类型:
std::vector<float> f;
std::List<int> my_list;
std::list<double> my_list;
实例化中使用的数据类型template
介于<
和之间>
。
搜索您最喜欢的 C++ 参考以获取实例化 模板。
推荐阅读
- reactjs - 在 node_modules 文件夹中找不到文件
- python - 当“代码”列超过四个时,在熊猫数据框中创建一个新行
- python - 为什么在连接两个数据帧时concat会向日期添加时间
- laravel - 在 PHPUnit 中调用未定义的方法 decodeResponseJson()
- android - 在底部导航中动态更改片段目的地
- node.js - 在不匹配的 peerDependencies 上强制 npm 安装失败
- javascript - 单击按钮并发送命令以在 xterm.js 终端上执行
- c# - 如何返回一个字符串并在另一个静态 void 中使用它?
- java - org.jboss.msc.service.ServiceNotFoundException:服务服务 jboss.ejb.default-resource-adapter-name-service 未找到
- python - 多进程计算后数据重置