0

I cannot figure out how I would call a template function that is suppose to take a string and College object as the parameters in my main.cpp.

This is my template in LinkedListADT.h:

#ifndef LINKED_LIST_H
#define LINKED_LIST_H

#include "ListNodeADT.h"

template <class T>
class LinkedList
{
private:
    ListNode<T> *head;
    int length;

public:
    LinkedList();   // constructor
    ~LinkedList();  // destructor

    // Linked list operations
    void insertNode(const T &);
    bool deleteNode(const T &);
    
    bool searchList(const T &, T &) const;
};

This is what I wrote so far for the search function in my LinkedListADT.h file:

template <class T, class S>
bool LinkedList<T, S>::searchList(const S &target, T &dataOut) const
{
    bool found = false;  // assume target not found
    ListNode<T> *pCur;
    while (pCur && pCur->getData().getCode() != target){
    /*Code to search*/

    return found;
}

This is the search function in my main.cpp that calls searchList from the header file which takes a user inputted college code. It is suppose to call searchList using the string input and try to find a match with a college code in the linked list:

void searchManager(const LinkedList<College> &list)
{
    string targetCode = "";
    College aCollege;

    cout << "\n Search\n";
    cout <<   "=======\n";
    
    while(toupper(targetCode[0]) != 'Q')
    {
        cout << "\nEnter a college code (or Q to stop searching) : \n";
        cin >> targetCode;

        if(toupper(targetCode[0]) != 'Q')
        {
            if(list.searchList(targetCode, aCollege))
                /*Code to display college*/
            else
                cout << "Not Found";
        }
    }
    cout << "___________________END SEARCH SECTION _____\n";
}

I am sure this is not the way to write my template function in the header file because this would also change the template for the other template functions (insert, delete, etc). I would appreciate any suggestions on a way to correctly write it. Thanks everyone!

drewster
  • 83
  • 1
  • 8
  • This question's shown code does not meet stackoverflow.com's requirements for a [mre]. This means it's unlikely that anyone here can conclusively answer the question; but only guess at the most. You should [edit] your question to show a minimal example, no more than one or two pages of code (the "minimal" part), that everyone else can cut/paste, compile, run, and reproduce the described issue (the "reproducible" part) ***exactly as shown*** (this includes any ancillary information, like the input to the program). See [ask] for more information. – Sam Varshavchik Oct 27 '20 at 02:21
  • @SamVarshavchik Sorry about that, I just edited the post. Now it should contain the minimum information needed to be answered. – drewster Oct 27 '20 at 02:32
  • What is wrong with your function ? – Eugene Oct 27 '20 at 02:32
  • Your header defines a template class that has only one parameter, and not two. – Sam Varshavchik Oct 27 '20 at 02:35
  • @Eugene I can't call the template searchList function in my main by passing a (string, college) because it says both parameters need to be College objects. I'm not sure if my template needs to be changed or if my call to the searchList function in main needs to be changed. I just wanted to know how to call searchList in my main file. – drewster Oct 27 '20 at 02:55
  • @SamVarshavchik How would I define a template class that could be one or two parameters? I found a post here: [link](https://stackoverflow.com/questions/3986765/c-template-multiple-types#:~:text=%2414.5.,definition%20or%20class%20template%20definition.) that looks similar to the problem I have but I am not sure how I would implement it exactly into my code. Thank you for the help! – drewster Oct 27 '20 at 02:59
  • You don't. C++ does not work this way. A template class always has a fixed list of parameters. C++11 introduced variadic templates that can take any number of parameters, from 0 to infinity. If you need a template with either one or two parameters you will need to define two completely separate templates. – Sam Varshavchik Oct 27 '20 at 11:11

1 Answers1

1

How would I define a template class that could be one or two parameters? (from a comment)

You could do this by using variadic templates, but that's not what you need here. Instead, you need a template member function of the single-argument template class, like this:

template <class T>
class LinkedList
{
    //...
public:
    //...

    template<class S>
    bool searchList(const S& target, T& result) const;
};

It is easier to place the function definition inside the class, but if you insist on defining it outside, here is the syntax:

template<class T>
template<class S>
bool LinkedList<T>::searchList(const S& target, T& result) const
{
    //...
}

Once you have it, the way you are calling it from main() should compile.

Eugene
  • 6,194
  • 1
  • 20
  • 31