c++ - 如何使用结构体、类型定义和模板?
问题描述
我正在尝试实现以下代码,但由于 ,我不断收到错误typedef
,有人可以帮忙吗?
template<class T>
struct box{
T data;
box *link;
};
typedef box* boxPtr;
错误是:
Use of class template 'box' requires template arguments
解决方案
写作
template<class T>
struct box{
T data;
box *link;
typedef box<T>* boxPtr;
};
和
int main()
{
box<int>::boxPtr use_of_a_bad_idea;
}
是一种方法,但是将指针类型伪装成对象类型会导致内存泄漏:根据经验不要这样做。这是解决这个问题的最好方法。
推荐阅读
- angular - 改变组件对象,影响服务对象
- python - 在 Azure Pipelines 上使用 Python 运行 Selenium:安全性
- google-chrome - 谷歌浏览器 DNS 缓存指的是旧网站
- c++11 - std::put_time 对默认语言环境实际上做了什么?
- c++ - C++ 模板化环形缓冲区实现:如何分别声明和初始化?
- typescript - 如何使用 Typescript、模块和自定义服务器正确设置 NextJS?
- mysql - MySQL DATE_ADD 函数;如何使用另一个列值作为创建表的日期
- assertj - 用什么来代替 AssertJ isEqualToIgnoringGivenFields?
- sql - 用于获取该电影 ID 的电影 ID 和每个性别的人数的 SQL 查询
- swift - 如何设置 NSWindowDelegate 来检测 windowDidEnterFullScreen 和类似事件?