Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何修复使用不同类型参数调用模板函数的程序?_C++_Templates_Types_Function Call_Function Templates - Fatal编程技术网

C++ 如何修复使用不同类型参数调用模板函数的程序?

C++ 如何修复使用不同类型参数调用模板函数的程序?,c++,templates,types,function-call,function-templates,C++,Templates,Types,Function Call,Function Templates,我不知道如何调用一个模板函数,该函数假定以字符串和College对象作为main.cpp中的参数 这是我在LinkedListDT.h中的模板: #ifndef LINKED_LIST_H #define LINKED_LIST_H #include "ListNodeADT.h" template <class T> class LinkedList { private: ListNode<T> *head; int length

我不知道如何调用一个模板函数,该函数假定以字符串和College对象作为main.cpp中的参数

这是我在LinkedListDT.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;
};
\ifndef链接列表\u H
#定义链接列表
#包括“ListNodeADT.h”
模板
类链接列表
{
私人:
ListNode*头;
整数长度;
公众:
LinkedList();//构造函数
~LinkedList();//析构函数
//链表操作
void insertNode(const T&);
布尔删除节点(常数T&);
布尔搜索列表(常量T&,T&)常量;
};
这是我到目前为止为LinkedListDT.h文件中的搜索函数编写的内容:

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;
}
模板
bool LinkedList::searchList(常量S&target,T&dataOut)常量
{
bool found=false;//假设未找到目标
ListNode*pCur;
while(pCur&&pCur->getData().getCode()!=target){
/*要搜索的代码*/
发现退货;
}
这是my main.cpp中的搜索函数,它从头文件调用searchList,头文件接受用户输入的学院代码。假设使用字符串输入调用searchList,并尝试在链接列表中查找与学院代码匹配的项:

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";
}
void searchManager(const LinkedList&list)
{
字符串targetCode=“”;
学院;
库特
我如何定义一个模板类,可以是一个或两个
参数?(来自注释)

您可以通过使用可变模板来实现这一点,但这不是您在这里需要的。相反,您需要单参数模板类的模板成员函数,如下所示:

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

    template<class S>
    bool searchList(const S& target, T& result) const;
};
模板
类链接列表
{
//...
公众:
//...
模板
布尔搜索列表(常量S和目标、T和结果)常量;
};
将函数定义放在类内比较容易,但如果坚持在类外定义,则语法如下:

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

一旦你有了它,你从
main()调用它的方式
应该编译。

这个问题显示的代码不符合stackoverflow.com对a的要求。这意味着这里的任何人都不可能最终回答这个问题;但最多只能猜测。你应该让你的问题显示一个最小的示例,不超过一到两页的代码(“最小”部分),其他人可以剪切/粘贴、编译、运行和复制所述问题(“可复制”部分),完全如图所示(包括任何辅助信息,如程序输入)。有关详细信息,请参阅。@SamVarshavchik很抱歉,我刚刚编辑了这篇文章。现在,它应该包含需要回答的最低信息。函数有什么问题?你的头定义了一个只有一个参数而不是两个参数的模板类。@Eugene我无法通过传递(string,college)因为它说两个参数都必须是college对象。我不确定是否需要更改我的模板,或者是否需要更改我对main中searchList函数的调用。我只是想知道如何在我的主文件中调用searchList。这回答了我的问题。非常感谢!(您的个人资料显示您未接受任何答案。接受答案是本网站的礼仪).@drewster请阅读。如果有助于你解决问题,你应该接受答案,正如尤金所说,你把所有的问题都留给了他们,而没有接受任何答案。@Eugene和TedLyngmo,很抱歉,我试图找到一种方法将这个问题标记为已回答,但我找不到。谢谢你让我知道!