- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试为模板类树制作一个递归树打印机,以便它可以打印树而无需事先知道那里有什么类型的数据。但是我想不出我需要的语法。
现在我知道代码只会打印树的根,但我什至无法做到这一点。
想法?
编辑:
编译器错误信息如下:
test.cpp(8) : error C2079: 'tempValue' uses undefined class 'Object'
test.cpp(9) : error C2228: left of '.retrieve' must have class/struct/union
type is 'HWTreeItr<Object> *'
with
[
Object=Object
]
did you intend to use '->' instead?
test.cpp(27) : error C2664: 'print_tree' : cannot convert parameter 1 from 'HWTreeItr<Object>' to 'HWTreeItr<Object> *'
with
[
Object=char
]
and
[
Object=Object
]
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
HWTreeOriginal.h
/*====================================================================
Tree data structure
Tree points to a single TreeNode that acts as the root --> no siblings
TreeNodes points to child TreeNode and to sibling TreeNod
====================================================================*/
#ifndef HWTREE_H
#define HWTREE_H
#include <list>
using namespace std;
/* ================================================================
Class Declarations
/ ================================================================ */
template <class Object>
class HWTree;
template <class Object>
class HWTreeItr;
/* -------------------------------------------------------
TreeNode -- used internally
------------------------------------------------------- */
template <class Object>
class HWTreeNode
{
private:
HWTreeNode(const Object & element=Object(), HWTreeNode* child=NULL, HWTreeNode* sibling=NULL);
~HWTreeNode();
Object m_element;
HWTreeNode* m_first_child;
HWTreeNode* m_next_sibling;
friend class HWTree<Object>;
friend class HWTreeItr<Object>;
};
/* -------------------------------------------------------
Tree - used with TreeItr
------------------------------------------------------- */
template <class Object>
class HWTree
{
public:
// constructors
HWTree( const Object& initial_element);
~HWTree();
// accessors
HWTreeItr<Object> get_root();
//mutators
void add_child(const Object& obj, const HWTreeItr<Object>& itr);
void add_sibling(const Object& obj, const HWTreeItr<Object>& itr);
void remove_left_child(const HWTreeItr<Object>& itr);
private:
HWTreeNode<Object>* m_root;
};
/* -------------------------------------------------------
TreeItr class; maintains "current position" in Tree
------------------------------------------------------- */
template <class Object>
class HWTreeItr
{
public:
// constructor
HWTreeItr( );
// accessors
bool retrieve(Object & result) const;
// mutators
void next_sibling( );
void child();
bool isPastEnd() const;
friend class HWTreeNode<Object>;
private:
HWTreeNode<Object>* m_current;
friend class HWTree<Object>;
};
/* ================================================================
Tree Node Class Code
/ ================================================================ */
template <class Object>
HWTreeNode<Object>::HWTreeNode(const Object & element=Object(), HWTreeNode* child=NULL, HWTreeNode* sibling=NULL)
: m_element(element), m_first_child(child), m_next_sibling(sibling)
{ }
template <class Object>
HWTreeNode<Object>::~HWTreeNode()
{
if (m_first_child != NULL)
delete m_first_child;
if (m_next_sibling != NULL)
delete m_next_sibling;
}
/* ================================================================
Tree Code
================================================================ */
template <class Object>
HWTree<Object>::HWTree( const Object& initial_element)
{
m_root = new HWTreeNode<Object>(initial_element); // create TreeNode
}
template <class Object>
HWTree<Object>::~HWTree()
{
delete m_root;
}
/* ================================================================
TreeItr Code
================================================================ */
/**
* Return an iterator representing the first node in the list.
* This operation is valid for empty lists.
*/
template <class Object>
HWTreeItr<Object> HWTree<Object>::get_root( )
{
HWTreeItr<Object> itr;
itr.m_current = m_root;
return itr;
}
template <class Object>
void HWTree<Object>::add_child(const Object& obj, const HWTreeItr<Object>& itr)
{
if (itr.m_current != NULL)
{
HWTreeNode<Object>* pNew = new HWTreeNode<Object>(obj, NULL); // 1) new node without children
pNew->m_next_sibling = itr.m_current->m_first_child; // 2) hook into exising children
itr.m_current->m_first_child = pNew;
}
}
template <class Object>
void HWTree<Object>::add_sibling(const Object& obj, const HWTreeItr<Object>& itr)
{
if (itr.m_current != NULL)
{
HWTreeNode<Object>* pNew = new HWTreeNode<Object>(obj, NULL); // 1) new node without children
pNew->m_next_sibling = itr.m_current->m_next_sibling; // 2) hook into exising children
itr.m_current->m_next_sibling = pNew;
}
}
template <class Object>
void HWTree<Object>::remove_left_child(const HWTreeItr<Object>& itr)
{
return;
}
/* ================================================================
Class: Tree Iterator
/ ================================================================ */
template <class Object>
HWTreeItr<Object>::HWTreeItr( ) : m_current( NULL )
{}
template <class Object>
bool HWTreeItr<Object>::isPastEnd() const
{
if (m_current == NULL)
return true;
else
return false;
}
template <class Object>
bool HWTreeItr<Object>::retrieve(Object & result) const
{
if (m_current != NULL) // bug: _current != ____.end() but where to get a list
{
HWTreeNode<Object>* pNode = m_current;
if (pNode != NULL) // another attemp to catch crash
result = pNode->m_element;
return true;
} else
return false;
}
template <class Object>
void HWTreeItr<Object>::next_sibling( )
{
if (m_current != NULL)
m_current = m_current->m_next_sibling;
}
template <class Object>
void HWTreeItr<Object>::child()
{
if (m_current != NULL)
m_current = m_current->m_first_child;
}
#endif
测试.cpp
#include "HWTreeOriginal.h"
#include <iostream>
#include <string>
using namespace std;
void print_tree(HWTreeItr<class Object>* currentItr, int level=0)
{
Object tempValue;
currentItr.retrieve(tempValue);
cout << string( level, ' ' ) << tempValue;
}
int main()
{
HWTree<char> myTree('A');
HWTreeItr<char> myItr = myTree.get_root();
HWTreeItr<char> tempItr;
myTree.add_child('B', myItr);
myItr.child();
tempItr=myItr;
myTree.add_sibling('C', myItr);
myItr.next_sibling();
myTree.add_sibling('D', myItr);
myItr=myTree.get_root();
print_tree((HWTreeItr<char>)myItr, 0);
return 0;
}
最佳答案
首先,我必须修复它实际上是可编译的树实现。如果您已经声明了方法或函数的定义,则它们可能没有默认参数。
在这种情况下,这是您的构造函数
/*====================================================================
Tree data structure
Tree points to a single TreeNode that acts as the root --> no siblings
TreeNodes points to child TreeNode and to sibling TreeNod
====================================================================*/
#ifndef HWTREE_H
#define HWTREE_H
// #include <list> unnecessary include
// using namespace std; => Really bad never do this in a header
/* ================================================================
Class Declarations
/ ================================================================ */
template <class Object>
class HWTree;
template <class Object>
class HWTreeItr;
/* -------------------------------------------------------
TreeNode -- used internally
------------------------------------------------------- */
template <class Object>
class HWTreeNode
{
private:
HWTreeNode(const Object & element=Object(), HWTreeNode* child=NULL, HWTreeNode* sibling=NULL);
~HWTreeNode();
Object m_element;
HWTreeNode* m_first_child;
HWTreeNode* m_next_sibling;
friend class HWTree<Object>;
friend class HWTreeItr<Object>;
};
/* -------------------------------------------------------
Tree - used with TreeItr
------------------------------------------------------- */
template <class Object>
class HWTree
{
public:
// constructors
HWTree( const Object& initial_element);
~HWTree();
// accessors
HWTreeItr<Object> get_root();
//mutators
void add_child(const Object& obj, const HWTreeItr<Object>& itr);
void add_sibling(const Object& obj, const HWTreeItr<Object>& itr);
void remove_left_child(const HWTreeItr<Object>& itr);
private:
HWTreeNode<Object>* m_root;
};
/* -------------------------------------------------------
TreeItr class; maintains "current position" in Tree
------------------------------------------------------- */
template <class Object>
class HWTreeItr
{
public:
// constructor
HWTreeItr( );
// accessors
bool retrieve(Object & result) const;
// mutators
void next_sibling( );
void child();
bool isPastEnd() const;
friend class HWTreeNode<Object>;
private:
HWTreeNode<Object>* m_current;
friend class HWTree<Object>;
};
/* ================================================================
Tree Node Class Code
* Removed all default arguments from it as this is not allowed
================================================================ */
template <class Object>
HWTreeNode<Object>::HWTreeNode(const Object & element, HWTreeNode* child, HWTreeNode* sibling)
: m_element(element), m_first_child(child), m_next_sibling(sibling)
{ }
template <class Object>
HWTreeNode<Object>::~HWTreeNode()
{
if (m_first_child != NULL)
delete m_first_child;
if (m_next_sibling != NULL)
delete m_next_sibling;
}
/* ================================================================
Tree Code
================================================================ */
template <class Object>
HWTree<Object>::HWTree( const Object& initial_element)
{
m_root = new HWTreeNode<Object>(initial_element); // create TreeNode
}
template <class Object>
HWTree<Object>::~HWTree()
{
delete m_root;
}
/* ================================================================
TreeItr Code
================================================================ */
/**
* Return an iterator representing the first node in the list.
* This operation is valid for empty lists.
*/
template <class Object>
HWTreeItr<Object> HWTree<Object>::get_root( )
{
HWTreeItr<Object> itr;
itr.m_current = m_root;
return itr;
}
template <class Object>
void HWTree<Object>::add_child(const Object& obj, const HWTreeItr<Object>& itr)
{
if (itr.m_current != NULL)
{
HWTreeNode<Object>* pNew = new HWTreeNode<Object>(obj, NULL); // 1) new node without children
pNew->m_next_sibling = itr.m_current->m_first_child; // 2) hook into exising children
itr.m_current->m_first_child = pNew;
}
}
template <class Object>
void HWTree<Object>::add_sibling(const Object& obj, const HWTreeItr<Object>& itr)
{
if (itr.m_current != NULL)
{
HWTreeNode<Object>* pNew = new HWTreeNode<Object>(obj, NULL); // 1) new node without children
pNew->m_next_sibling = itr.m_current->m_next_sibling; // 2) hook into exising children
itr.m_current->m_next_sibling = pNew;
}
}
template <class Object>
void HWTree<Object>::remove_left_child(const HWTreeItr<Object>& itr)
{
return;
}
/* ================================================================
Class: Tree Iterator
/ ================================================================ */
template <class Object>
HWTreeItr<Object>::HWTreeItr( ) : m_current( NULL )
{}
template <class Object>
bool HWTreeItr<Object>::isPastEnd() const
{
if (m_current == NULL)
return true;
else
return false;
}
template <class Object>
bool HWTreeItr<Object>::retrieve(Object & result) const
{
if (m_current != NULL) // bug: _current != ____.end() but where to get a list
{
HWTreeNode<Object>* pNode = m_current;
if (pNode != NULL) // another attemp to catch crash
result = pNode->m_element;
return true;
} else
return false;
}
template <class Object>
void HWTreeItr<Object>::next_sibling( )
{
if (m_current != NULL)
m_current = m_current->m_next_sibling;
}
template <class Object>
void HWTreeItr<Object>::child()
{
if (m_current != NULL)
m_current = m_current->m_first_child;
}
#endif
现在你的主要:
#include "HWTreeOriginal.h"
#include <iostream>
#include <string>
using namespace std; // Bad practise
// This is how this needs to look like
template< typename Object >
void print_tree(HWTreeItr<Object> & currentItr, int level=0)
{
Object tempValue;
currentItr.retrieve(tempValue);
cout << string( level, ' ' ) << tempValue;
}
int main()
{
HWTree<char> myTree('A');
HWTreeItr<char> myItr = myTree.get_root();
HWTreeItr<char> tempItr;
myTree.add_child('B', myItr);
myItr.child();
tempItr=myItr;
myTree.add_sibling('C', myItr);
myItr.next_sibling();
myTree.add_sibling('D', myItr);
myItr=myTree.get_root();
// Do not use C-Casts like you did before, it did not help anyway as you noticed.
print_tree(myItr, 0);
return 0;
}
关于c++ - 尝试制作模板化递归树打印机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3908036/
我有一个代理类,它接收请求并将请求发送到另一台服务器,获取响应并将其定向回原始请求者。我使用套接字连接到服务器并使用 Printwriter 对其进行写入。像这样的事情 private Pri
我想使用 Java Swing 为医疗商店开发一个独立的应用程序。强制要求在没有打印对话框的情况下单击一下即可打印小尺寸(219 毫米至 140 毫米)的纸张。他们需要间歇性地将报告从不同的打印机打印
我是一名学生,需要创建一个 silten 打印功能,希望能够打印 PDF。这个需要基于Java。 我在 Google 上搜索并找到了一个无需对话框即可打印的代码。但如果源是 .txt 文件,它就会正确
我正在寻找 POS 打印的解决方案。 场景是: 一家餐厅目前有一个 POS 系统启动并运行,他们从店内的 iPad 上接受订单,并有一个网络设置来处理订单并在厨房的热敏打印机上自动打印出来(很酷,对吧
尝试使用以下代码示例,它在 WinForm 应用程序中运行良好,但在 VSTO 中运行不佳。是否有某种允许访问的权限? 可以设置默认打印机,但不能获取或设置打印机设置。 从插件中获取以下异常: Sys
我必须通过蓝牙将字体文件发送到我的打印机 Zebra RW420。我正在使用 Zebra Windows Mobile SDK,但无法找到任何方式将其发送和存储在打印机上。我可以通过 Label Vi
我需要创建一个“粉碎”的虚拟打印机 基本上这是我的问题。我有一个软件程序需要在保存文件之前“打印”文件。我希望能够打印到我的碎纸机,以便它保存文档,但实际上我不想打印文档。所以我需要打印到一个程序,该
我在吃 Argox 标签打印机时遇到了麻烦,只是仍然无法向她发送任何内容。型号为 Argox OS214 tt,ANPP,接受 PPLB。 使用通用类连接串行设备,我将她用于多个财务打印机和秤,附后。
我正在编写一些在 org.eclipse.swt.printing.Printer 上打印的代码。所以第一步是看看我如何测试它,但似乎架构不允许我定义自己的打印机,因为 PrinterData 和 P
我有一个标签列表,数据如下。 ['id', 'Version', 'chip_name', 'xversion', 'device', 'opt_param', 'place_effort'][1,
我正在开发一个应用程序,在该应用程序中,我通过 IP 地址和端口号从我的手机向 WiFi 打印机发送文件,这些文件是 .txt、.png、.jpg、.doc。它们应该从打印机打印出来。我尝试了以下代码
我正在尝试使用以下代码打印图像,但文档只是停留在打印作业队列中,拒绝打印。在 (windows) 打印作业队列中,我得到: DocumentName: Printing an image Status
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Print preview ZPL II commands using .NET WinForm befor
我有什么 我目前正在编写一个程序,它接受一个指定的文件并对其执行一些操作。目前它打开它,和/或将它附加到电子邮件并将其邮寄到指定地址。 文件可以是以下格式:Excel、Excel Report、Wor
我将这台 Zebra ZM400 打印机连接到网络 (192.168.1.50)。我正在尝试直接从 PHP 将内容推送到这台打印机。 这就是我的想法,但我无法做到这一点。我尝试了 file_put_c
我想显示一个列表,其中包含设备可通过 AirPrint 访问的所有打印机。 我使用 UIPrinterPickerController 让它工作。 是否有以自定义方式显示此 PickerControl
我想将任何办公文件传输到 Wi-ifi 打印机。我完全不知道如何开始。 发现没有用于无线打印的公共(public) API。 谁能分享一些意见? 提前致谢! 最佳答案 您可以首先扫描 WiFi 设备并
有什么方法可以让我在蓝牙热敏打印机上打印收据,因为我真的很难在 flutter 上找到解决方案?任何事情都有帮助,我真的很感激这些答案 最佳答案 我试过 esc_pos_bluetooth 包,但它不
为了在我的 mac 上模拟 ZPL 打印机,我在互联网上搜索了几天。最后,我有一个解决方案可以在这里发布,这样其他用户可能会发现它有帮助。我想在这里发布我的解决方案 Emulate Zebra pri
是否有 ZPL 命令来简单地重启 Zebra 打印机?到目前为止,我只能找到 ~JR 命令,这对我来说看起来不像我正在寻找的东西。我只需要一种方法来重新启动打印机,而无需重置其任何配置。 最佳答案 以
我是一名优秀的程序员,十分优秀!