我无法使用模板编译此代码
I cannot compile this code using template
我正在尝试编译用c++编写的代码。
我在fifo_list.h
中有这段代码template <class T>
class FIFO_LIST
{
public:
class list_type {
public:
T data;
list_type *next;
void *operator new(size_t num_bytes)
{
block_pool mem_pool;
void *mem_addr = mem_pool.pool_alloc( num_bytes );
return mem_addr;
} // new
}; // class list_type
private:
list_type *list;
public:
/** define a handle type to abstract the list_type type */
typedef list_type *handle
handle first(void)
{
return list;
} // first
}
和header queue.h:
#include "fifo_list.h"
template <class T>
class queue : protected FIFO_LIST<queueElem<T> *>
{
public:
queueElem<T> *queueStart()
{
handle h = first();
queueElem<T> *elem = get_item( h );
return elem;
}
}
当我尝试编译时,我有这些错误信息:
include/queue.h: In member function ‘queueElem<T>* queue<T>::queueStart()’:
include/queue.h:100: error: ‘handle’ was not declared in this scope
include/queue.h:100: error: expected ‘;’ before ‘h’
include/queue.h:101: error: ‘h’ was not declared in this scope
我错在哪里?
@Piotr Skotnicki, @Barry我已经这样修改了代码
queueElem<T> *queueStart()
{
//handle h = first();
typename FIFO_LIST<queueElem<T> *>::handle h = first();
queueElem<T> *elem = get_item( h );
return elem;
} // queueStart
现在我有这样的错误:
include/queue.h:101: error: there are no arguments to ‘first’ that depend on a template parameter, so a declaration of ‘first’ must be available
由于某些原因,我找不到一个很好的副本…
handle
是与相关的名称。非限定查找不会在基类中找到依赖的名称,因此必须对其进行限定:
typename FIFO_LIST<queueElem<T> *>::handle h = first();
同样,由于first
也来自基类,因此需要限定:
typename FIFO_LIST<queueElem<T> *>::handle h = FIFO_LIST<queueElem<T> *>::first();
尽管您可以通过简单地使用this->
:
typename FIFO_LIST<queueElem<T> *>::handle h = this->first();
这是两阶段模板实例化的一个已知问题(这就是我不喜欢它的原因)。
要修复你的代码,使用以下命令:
typename FIFO_LIST<queueElem<T> *>::handle h = this->first();
相关文章:
- 在Linux for Windows上编译C++代码时出错
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在程序中编写脚本来编写和编译代码
- 未定义的引用 .. 使用 OpenCV 编译 C++ 代码时,从命令行
- 在macos上编译代码的未解析符号
- 使用个人C++库编译代码时,与头文件一起使用时会中断
- 使用指针编译代码后,.cpp文件将变为随机字符
- VS Express 无法正确编译代码(?
- 如何在Ubuntu中使用Visual Studio代码编译C++代码
- 使用 Android NDK 使用 clang++ 编译C++代码时对"_Unwind_Resume"的未定义引用
- 无法使用两个包装不同下一层的ssl_stream编译代码
- 编译代码时"[Warning] extra tokens at end of"
- 无法编译代码,因为它已在 C++11 中弃用
- 如何在 Azure 应用服务中使用 cl.exe 编译 C++ 代码并生成可执行文件
- 如何调试编译 c++ 代码的 bazel?
- 为什么在使用转换构造函数编译代码时需要 const 复制构造函数?
- 编译 C++ 代码后尺寸较大
- 使用用 C++ 和 DDS 编写的 CMAKE 编译代码
- 编译代码时如何不制作二进制文件?
- 如何使用所有其他文件信息来编译代码,例如HAAR级联训练的权重的XML文件