2014-12-06 54 views
0

好吧,刚刚起来,这是我在这里的第一个问题,所以我道歉,如果我没有在我的第一次去包括每一个相关的信息,但我会尽我所能。二叉树模板,类特定函数调用

我的问题是我想要写在main()中的特定函数,如果它们的“类别”与搜索的类别相匹配,它将从节点打印出数据。我可能只是在摸索语法,因为我在这方面还很新。要明确,确切的问题是,我试过的所有函数调用告诉我*****“重载函数没有实例”BinTree :: inOrderTraverse [with Type = CategorizedContact]“与参数列表匹配。 :(无效)对象类型是二叉树*****下面是相关的main()代码:

#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; 
    } 
} 

这是未完成的,并且概率甚至没有设计正确,但我不能告诉,直到我设法让函数调用工作。 最后,这里是与我的inOrderTraverse .h和.tem文件有关的代码。

#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" 

而就相关.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); 
    } 
} 

在任何人建议改变InOrderTraverse(无效处理(&类型,INT)),或重载的版本,只知道我需要为我的项目实施这种方式。 我拥有的唯一自由是使用printByCat(CategorizedContact,int)****,只要它仍然与inOrderTraverse兼容,就可以更改它。 所以,我希望你现在可以看到,main()printByCat()中的函数是为了从用户中取一个类别,然后作为inOrderTraverse(printByCat())的一个参数。但我显然犯了一个根本的错误,我不明白。

在这一点上任何指导将不胜感激,我没有要求任何人为我编码,因为我知道你反对,但我真的只需要了解为什么函数调用不起作用。我猜这个问题源于我缺乏参考变量的经验,但我得到的错误似乎建议将printByCat()作为inOrderTraverse的参数使用,它不符合参数要求,因为它不是一个void函数,但它是一个无效函数....所以,我有点失落。无论如何感谢您的时间,并请让我知道,如果我忘记了什么。

回答

0

发现它是什么,显然我不能包含printbyCat()在使用此函数作为inOrderTraverse()的参数时的参数,所以函数调用应该只是:myBook.inOrderTraverse(printByCat)。