Templates 二叉树模板,特定于类的函数调用
好的,请注意,这是我在这里的第一个问题,所以如果我没有在第一次尝试时包含所有相关信息,我表示歉意,但我会尽我所能 我的问题是,我试图在main()中编写一个特定函数,如果节点的“类别”与搜索的类别匹配,该函数将从节点打印数据。我可能只是在摸索语法,因为我在这方面还是相当新的。明确地说,确切的问题是,我尝试过的所有函数调用都告诉我******“没有重载函数的实例”BinTree::inOrderTraverse[with Type=CategorizedContact]”与参数列表匹配。参数类型为:(void)。对象类型为BinTree****以下是相关的main()代码:Templates 二叉树模板,特定于类的函数调用,templates,binary-tree,function-call,Templates,Binary Tree,Function Call,好的,请注意,这是我在这里的第一个问题,所以如果我没有在第一次尝试时包含所有相关信息,我表示歉意,但我会尽我所能 我的问题是,我试图在main()中编写一个特定函数,如果节点的“类别”与搜索的类别匹配,该函数将从节点打印数据。我可能只是在摸索语法,因为我在这方面还是相当新的。明确地说,确切的问题是,我尝试过的所有函数调用都告诉我******“没有重载函数的实例”BinTree::inOrderTraverse[with Type=CategorizedContact]”与参数列表匹配。参数类型为
#包括
#包括
#include//invalid_参数
使用名称空间std;
#包括“name.h”
#包括“contact.h”
#包括“address.h”
#包括“BinTree.h”
#包括“BinNode.h”
#包括“CategorizedContact.h”
#包括“Field.h”
#包括“htmlfunc.h”
使用名称空间地址信息;
作废打印菜单();
void printByCat(分类联系人和int);
int getMenuInput();
int validateMenuInput(字段输入);
字段printCategoryMenu();
字段类别选择();
int main()
{
地址:tmpAddress;
名称tmpName,tmpName2;
分类接触tmpContact、tmpContact 2、itemToRemove;
BinTree myBook;
字段tmpString1,类别为;
int menuOption=0,node=0,count=0,categoryMenuOption=0,categoryInt=0;
CategorizedContact&tmp=tmpContact2;//我只是尝试初始化
//这里有一个ref变量,用于使函数调用工作。
myBook.readFile(“address.csv”);
做
{
打印菜单();
menuOption=getMenuInput();
开关(菜单选项)
{
案例1:
cout>tmpContact;//获取其余的联系人信息
myBook.addItem(tmpContact);//将联系人添加到通讯簿
myBook.writeFile(“address.csv”,'\n');//将新联系人写入文件
打破
案例2:
不能离开,伯爵);
//过程
处理(光标->数据,计数);
计数++;
inOrderTraverse(进程,光标->右侧,计数);
}
}
在任何人建议更改InOrderTraverse(void进程(Type&,int))或重载版本之前,只需知道我需要以这种方式为我的项目实现它。
我唯一的自由是使用***printByCat(CategorizedContact,int)****,只要它仍然与inOrderTraverse兼容,就可以更改它。
因此,我希望您现在可以看到,main()printByCat()中的函数是为了从用户那里获取一个类别,然后作为inOrderTraverse(printByCat())的参数本身。但我显然犯了一个我不理解的基本错误
在这一点上,任何指导都是值得的,我并没有要求任何人为我做编码,因为我知道你反对,但我真的需要理解为什么函数调用不起作用。我猜问题源于我对引用变量缺乏经验,但我得到的错误似乎表明函数打印ByCat()作为inOrderTraverse的一个参数,它不符合参数要求,因为它不是一个空函数,但它是一个空函数…所以是的,我有点迷路了。无论如何,谢谢你的时间,如果我忘了什么,请告诉我。找到了它,显然我不能包含printbyCat()的参数当使用此函数作为inOrderTraverse()的参数时,函数调用应该是:myBook.inOrderTraverse(printByCat)
#include <iostream>
#include <string>
#include <stdexcept> //invalid_argument
using namespace std;
#include "name.h"
#include "contact.h"
#include "address.h"
#include "BinTree.h"
#include "BinNode.h"
#include "CategorizedContact.h"
#include "Field.h"
#include "htmlfunc.h"
using namespace AddressInfo;
void printMenu();
void printByCat(CategorizedContact&, int);
int getMenuInput();
int validateMenuInput(Field input);
Field printCategoryMenu();
Field categorySelection();
int main()
{
Address tmpAddress;
Name tmpName, tmpName2;
CategorizedContact tmpContact, tmpContact2, itemToRemove;
BinTree<CategorizedContact> myBook;
Field tmpString1, categoryIn;
int menuOption = 0, node = 0, count = 0, categoryMenuOption = 0, categoryInt = 0;
CategorizedContact& tmp = tmpContact2; // I was just experimenting with trying to initialize
//a ref variable here, to make the function call work.
myBook.readFile("address.csv");
do
{
printMenu();
menuOption = getMenuInput();
switch (menuOption)
{
case 1:
cout << "\t***** Add Contact *****\n\n";
categoryIn = categorySelection(); //Prints Category menu and gets input
tmpContact.setCategory(categoryIn); //Assigns category choice to tmpContact
cin >> tmpContact; //Gets the rest of the contact info
myBook.addItem(tmpContact); //Adds contact to address book
myBook.writeFile("address.csv", '\n'); //Writes new contact to file
break;
case 2:
cout << "\n\t***** Count Contacts *****\n";
count = myBook.getNumUsed();
cout << "Number of Contacts: " << count;
cout << endl << endl;
break;
case 3:
cout << "\n\t***** Print Contacts By Category *****\n";
categoryIn = printCategoryMenu(); //Prints category menu and gets choice
if (categoryIn == "All Contacts")
myBook.printAll();
categoryInt = stoi(categoryIn); // converts to int to match required function parameters
myBook.inOrderTraverse(printByCat(tmp, categoryInt));
break;
void printByCat(CategorizedContact& tmp, int categoryInt)
{
int count = 1;
switch (categoryInt)
{
case 65:
if (tmp.getCategory() == "Business")
cout << count << ". " << tmp << endl;
break;
default:
cout << "Error" << endl;
break;
}
}
#ifndef BINTREE_H
#define BINTREE_H
#include <cstdlib> // NULL
#include <string>
#include <iostream> // cout
#include <fstream>
#include <algorithm> // copy
#include "BinNode.h"
#include "CategorizedContact.h"
#include "Contact.h"
template <class Type>
class BinTree
{
public:
BinTree();
BinTree(const BinTree<Type>& source);
~BinTree();
BinTree<Type>& operator=(const BinTree<Type>& source);//assignment operator
int getNumUsed() const { return(used); }
void addItem(Type dataIn);
void printAll();
void writeFile(string fileName, char delimeter = '\n');
void readFile(string fileName);
void inOrderTraverse(void process(Type&, int));
void debugOn() { debug = true; }
void debugOff() { debug = false; }
private:
bool debug;
int used;
BinNode<Type>* root;
void inOrderTraverse(void process(Type&, int),
BinNode<Type>* cursor, int& count);
void write(BinNode<Type>* cursor, char delimeter,
ofstream& outFile);
void printInOrder(BinNode<Type>* cursor, int& count);
void free(BinNode<Type>* cursor);
void copyTree(BinNode<Type>* cursor);
BinNode<Type>* alloc(Type itemToAdd);
};
#include "BinTree.tem"
template <class Type>
void BinTree<Type>::inOrderTraverse(void process(Type&, int))
{
int count = 1;
inOrderTraverse(process, root, count);
}
template <class Type>
void BinTree<Type>::inOrderTraverse(void process(Type&, int),
BinNode<Type>* cursor, int& count)
{
if (cursor != NULL)
{
// In order traverse
inOrderTraverse(process, cursor->left, count);
// PROCESS
process(cursor->data, count);
count++;
inOrderTraverse(process, cursor->right, count);
}
}