gpt4 book ai didi

VS 2008 中的 C++ 0x 错误

转载 作者:太空宇宙 更新时间:2023-11-04 15:08:57 24 4
gpt4 key购买 nike

首先感谢论坛在将一个用C++ 0x函数编写的函数转换为C++03时提供的支持。我还有几行要转换。在完成任务之前,我有一个主要的编译错误(在 VS 2008 中)需要在以下代码中修复。

(错误读作“模板声明只允许在全局、命名空间或类范围内使用”)

#include "stdafx.h"

#define EMPTY(x, func)
#define ACTION(x, func) func(x)
#define IFRETURN(x, func) if (!func(x)) { return; }

#define BIN_TREE_TRAVERSAL(NAME, PRE, IN, POST) \
template <typename node_t, typename func_t> \
void NAME (node_t* proot, func_t const& func) \
{ \
if (proot) \
{ \
PRE(proot, func); \
NAME(proot->left (), func); \
IN(proot, func); \
NAME(proot->right(), func); \
POST(proot, func); \
} \
}

BIN_TREE_TRAVERSAL(inorder, EMPTY, ACTION, EMPTY)
BIN_TREE_TRAVERSAL(preorder, ACTION, EMPTY, EMPTY)
BIN_TREE_TRAVERSAL(postorder, EMPTY, EMPTY, ACTION)

#define LEFT left
#define RIGHT right

#define BIN_TREE_FIRST(NAME, CONTAINER, NEXT, ACT, L, R) \
template <typename node_t, typename func_t> \
void NAME (node_t* proot, func_t const& func) \
{ \
CONTAINER<node_t*> tovisit; \
std::vector<node_t*> visited; \
tovisit.push(proot); \
while (!tovisit.empty()) \
{ \
node_t* pnode = tovisit.NEXT(); tovisit.pop(); \
if (std::find(visited.begin(), visited.end(), pnode) \
== visited.end()) \
{ \
visited.push_back(pnode); \
ACT(pnode, func); \
if (pnode->L()) { tovisit.push(pnode->L()); } \
if (pnode->R()) { tovisit.push(pnode->R()); } \
} \
} \
}

BIN_TREE_FIRST(width_first , std::queue, front, ACTION, LEFT, RIGHT)
BIN_TREE_FIRST(width_first_if, std::queue, front, IFRETURN, LEFT, RIGHT)

BIN_TREE_FIRST(depth_first , std::stack, top, ACTION, RIGHT, LEFT)
BIN_TREE_FIRST(depth_first_if, std::stack, top, IFRETURN, RIGHT, LEFT)

#define BASETREEE_TRAVERSAL(NAME) \
template <typename func_t> void NAME (func_t const& f) \
{ bintree::NAME (m_root, [&f] (node_t* x) { f(x->n); } ); } \
template <typename func_t> void NAME (func_t const& f) const \
{ bintree::NAME (m_root, [&f] (node_t const* x) { f(x->n); } ); }

#define BASETREEE_TRAVERSAL_IF(NAME) \
template <typename func_t> void NAME (func_t const& f) \
{ bintree::NAME (m_root, [&f] (node_t* x) { return f(x->n); } ); } \
template <typename func_t> void NAME (func_t const& f) const \
{ bintree::NAME (m_root, [&f] (node_t const* x) { return f(x->n); } ); }

int _tmain(int argc, _TCHAR* argv[])
{
BASETREEE_TRAVERSAL(preorder)
BASETREEE_TRAVERSAL(inorder)
BASETREEE_TRAVERSAL(postorder)
BASETREEE_TRAVERSAL(width_first)
BASETREEE_TRAVERSAL(depth_first)


BASETREEE_TRAVERSAL_IF(width_first_if)
BASETREEE_TRAVERSAL_IF(depth_first_if)
return 0;
}

如何修复错误。

最佳答案

尝试将宏调用移到 main 之外。

不过,在某些时候,您将不得不使用 C++03 语法将所有 lambda 替换为仿函数对象。

关于VS 2008 中的 C++ 0x 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6816405/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com