简体   繁体   中英

Passing parameters from non-template to template function?

I am having some trouble passing parameters for a template function trough a non-template function.

Let's supose I have this:

template <class T>
void A(Array <T> &A) {
    cout << "here";
}


void menu(Array<myType>& fooList) { // my type specified class type, created by me. 
    cout << "enter option ";
    cin >> a;

    switch {
       case 1: A(fooList); break;
    }
}

I hope the example is clear enough. Is this possible or not? Can I the parameters pass from a non-template function to a template function?

EDIT: - the actual code since the prototype i tried to build wasn't helpful.

domain.h

template <class T>
void printAll(Array <T> &DBst, Array <T> &DBas){
    for(int i=0; i<DBst.lenght; i++){
        DBst.M[i].printStudent();
        std::cout<<" ___ ";
        DBas.M[i].printAssgn();
        std::cout<<std::endl;
    }
}

controller.h

template <class T>
void _printAll(Array <T> &DB1, Array <T> &DB2){
    cout<<"List of students with their Assignment!"<<endl;
    printAll(DB1, DB2);
    cout<<"done!"<<endl;
}

menu.cpp

void Menu::mainMenu(Array <Student> &DBst,Array <Assignment> &DBas){
showMainMenu();
int ret = Menu::intInputHandler();

while(ret){
    switch(ret){
    case 1:studentMenu(DBst,DBas);break;
    case 2:assignmentMenu(DBst,DBas);break;
    case 3:statsMenu(DBst,DBas);break;
    case 4:_printAll(DBst,DBas);break; //// ERROR ***
    case 0:/*exit*/;break;
    default:cout<<"Wrong option selected!";break;
    }
    }

    }

menu.h

class Menu{

public:
    void showMainMenu();
    void showStudentMenu();
    void showAssignmentMenu();
    void showStatisticsMenu();
    string stringInputHandler();
    int intInputHandler();
    void mainMenu(Array <Student> &,Array <Assignment> &);
    void studentMenu(Array <Student> &,Array <Assignment> &);
    void assignmentMenu(Array <Student> &,Array <Assignment> &);
    void statsMenu(Array <Student> &,Array <Assignment> &);

};

* ..\\Menu.cpp:90:29: error: no matching function for call to '_printAll(Array<Student>&, Array<Assignment>&)'

Well your _printAll(DBst,DBas); are array of two different types; and _printAll can take only two arrays of same type. You need to define a function with

template <class T1, class T2>
void printAll(Array <T1> &DBst, Array <T2> &DBas) 

and a corresponding _printAll

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM