好吧,剛剛起來,這是我在這裏的第一個問題,所以我道歉,如果我沒有在我的第一次去包括每一個相關的信息,但我會盡我所能。二叉樹模板,類特定函數調用
我的問題是我想要寫在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函數,但它是一個無效函數....所以,我有點失落。無論如何感謝您的時間,並請讓我知道,如果我忘記了什麼。