首先感謝論壇提供的支持,同時將用C++ 0x函數編寫的函數轉換爲C++ 03。我有更多的線要轉換。在完成任務之前,我有一個主要的編譯錯誤(在VS 2008中)在下面的代碼中修復。VS 2008中的C++ 0x errror
(錯誤讀作「模板聲明在全球命名空間或類範圍只允許」)
#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;
}
我怎樣才能修復錯誤。
對於初學者來說,消除了不少宏... –
你會得到很多嘗試使用的C++ 0x在VS2008的編譯器不支持它的錯誤。 – AJG85