将模板类转换为标头和 cpp 文件
Converting a template class to a header and cpp file
我试图将以下头文件转换为单独的头文件和.cpp文件。
template <class T>
class Rolodex {
public:
/**
* Creates a new empty Rolodex
*/
Rolodex()
{
sentinel_ = new Item;
sentinel_->value_ = T();
sentinel_->next_ = sentinel_;
sentinel_->prev_ = sentinel_;
current_ = sentinel_;
}
~Rolodex()
{
while (current_ != sentinel_) {
delete_current();
}
delete sentinel_;
}
/**
* Returns true if the Rolodex is positioned at the beginning.
*/
bool is_before_first()
{
return current_ == sentinel_;
}
/**
* Returns true if the Rolodex is positioned at the end.
*/
bool is_after_last()
{
return current_ == sentinel_;
}
/**
* Rotates the Rolodex one step forwards.
*/
void rotate_forward()
{
current_ = current_->next_;
}
/**
* Rotates the Rolodex one step backwards.
*/
void rotate_backward()
{
current_ = current_->prev_;
}
/**
* Returns the value of the current card.
*/
const T ¤t_value()
{
return current_->value_;
}
/**
* Inserts a new item after the current position and
* positions the Rolodex at the newly inserted item.
*/
void insert_after_current(const T &value)
{
Item *coming = new Item;
coming->value_ = value;
coming->next_ = current_->next_;
coming->prev_ = current_;
current_->next_->prev_ = coming;
current_->next_ = coming;
current_ = coming;
}
/**
* Inserts a new item before the current position and
* positions the Rolodex at the newly inserted item.
*/
void insert_before_current(const T &value)
{
Item *coming = new Item;
coming->value_ = value;
coming->prev_ = current_->prev_;
coming->next_ = current_;
current_->prev_->next_ = coming;
current_->prev_ = coming;
current_ = coming;
}
/**
* Deletes current item and positions the Rolodex
* at the _following_ item
*/
void delete_current()
{
Item *going = current_;
current_->prev_->next_ = current_->next_;
current_->next_->prev_ = current_->prev_;
if (going->next_ == sentinel_)
current_ = going->prev_;
else
current_ = going->next_;
delete going;
}
private:
struct Item {
T value_;
Item *next_;
Item *prev_;
};
Item *sentinel_;
Item *current_;
};
#endif /* ROLODEX_H_ */
但是,当我将它们分开时,如果我不包括
template <class T>
在头文件中,我收到一个错误,试图声明
T value;
但是如果我这样做,我会在.cpp文件中收到多个错误,说 Rolodex 不是文件类或枚举。是否有其他类型可用于声明我的值;如果是这样,我该如何修改代码以适应这一点。任何帮助都非常感谢,我超级迷路
编辑:谢谢大家的帮助,我对C++类缺乏理解,在对您提供的类似线程进行一些研究后,我解决了我的问题。
如果您不知道模板的工作原理,我建议您先阅读该主题。
您可以在单独的.cpp
文件中定义模板类成员,但还必须为要使用的所有类型实例化一个类。您可以在.h
或.cpp
文件中声明一个简单的虚拟对象。
// tmpl.h
template <typename T>
struct S {
S();
T _a_field;
}
// tmpl.cpp
template <typename T>
S<T>::S() {}
S<int> dummy;
但是,这不是建议的方法。我个人建议在单个tmpl.h
文件或另一个文件中定义所有内容,并将其包含在tmpl.h
使用模板时,完整的实现需要在标头中。不能将.cpp
文件用于方法定义,但如果您仍希望分离接口和实现。可以将.hpp
用于类定义,.inl
文件用于方法定义。在.hpp
文件的末尾,您必须包含包含该实现的.inl
文件。您可以在此处了解更多信息,为什么模板代码无法移动到.cpp
文件 https://youtu.be/6wdCqNlnMjo
相关文章:
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 命名空间中具有.h和.cpp文件的类
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 有充分的理由在h文件中使用include保护而不是cpp文件吗
- 如何在cpp文件之间切换窗口?在Qt中
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 我有两个类需要在同一 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象
- .h 和.cpp文件分离时出错,但仅使用 .h 文件时没有错误.我做错了什么?
- 库标题在标题中不可见,但在 cmake build 下.cpp文件中完全可见.为什么?
- C++ 链接到单独的.cpp文件说"multiple definitions"
- 在 cpp 文件中隐藏采用模板参数引用的方法
- 如何使用 samtools C API 构建一个简单的主.cpp文件
- 包含在.cpp文件中包含在 .h 文件时包含
- C++(.cpp文件和.h文件)拆分代码并添加一个函数,提取 - 这很容易吗?
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 如何使用线程类编译 cpp 文件?
- 使用 Powershell 命令将 cpp 文件的文件夹编译为 GNU 的 g++
- 我应该将外部标头放在 .h 文件还是.cpp文件中?
- 从另一个 cpp 文件更改结构内、映射键内的变量