簡體   English   中英

C++ 模板 - 找不到匹配的重載函數,無法推導出“T”的模板參數

[英]C++ Template - no matching overloaded function found, could not deduce template argument for 'T'

我正在嘗試實現“比較”功能。 根據輸入參數,比較目標可以是 2 個類之一。 這就是為什么我想使用模板來簡化代碼維護。 我是模板的新手,無法弄清楚發生了什么。

在此處輸入圖片說明

class Compare_Output
{
public:
    static Directory* empty_dir;
    static File* empty_file;
    enum COMPARE_TYPE { FILE, SUB_DIR };

    Directory* dir1;
    Directory* dir2;
    int level;
    std::pair<std::string,std::string> dir_name;
    std::vector<Compare_Output> sub_dir_compare;
    std::vector<File_Compare_Result> list;

    Compare_Output(Directory* dir1, Directory* dir2, int level);
    template<class T>
    void compare(int type, int level);

    Compare_Output& operator=(const Compare_Output&) = default;
};

class Directory
{
public:
    fs::path path;
    Directory* parent;
    std::vector<Directory*> sub_dir;
    std::vector<File*> files;

    Directory(const fs::path path, Directory* parent = NULL);
    Directory& operator=(const Directory&) = default;
    std::string getDirName(void);
    static void compare(Directory& dir1, Directory& dir2, std::vector<Compare_Result> &result);
    static void getAllFiles(Directory& dir, std::vector<std::pair<File*, int>>& result, int level, bool include_dir); // use for root directory only, thus make it static
};

template<class T>
void Compare_Output::compare(int type, int level)
{
    int i = 0, j = 0;
    std::vector<T*> t1, t2;
    if (type == FILE)
    {
        t1 = dir1->files;
        t2 = dir2->files;
    }
    else if (type == SUB_DIR)
    {
        t1 = dir1->sub_dir;
        t2 = dir2->sub_dir;
    }
    while (!(i == t1.size() && j == t2.size()))
    {
        // avoid the out of bound error
        if (i == t1.size() && i != 0)
            i--;
        if (j == t2.size() && j != 0)
            j--;

        T* item1 = t1.at(i);
        T* item2 = t2.at(j);

        if (*item1 == *item2)
        {
            list.push_back({ true, item1, item2 });
            i++;
            j++;
        }
    }
}

模板並不像您認為的那樣工作。 要擁有您想要的界面,您需要兩個函數(如果您願意,助手可以是private ),如下所示:

template<class T>
void Compare_Output::compare_helper(std::vector<T*> t1, std::vector<T*> t2, int level)
{
    int i = 0, j = 0;
    while (!(i == t1.size() && j == t2.size()))
    {
        // avoid the out of bound error
        if (i == t1.size() && i != 0)
            i--;
        if (j == t2.size() && j != 0)
            j--;

        T* item1 = t1.at(i);
        T* item2 = t2.at(j);

        if (*item1 == *item2)
        {
            list.push_back({ true, item1, item2 });
            i++;
            j++;
        }
    }
}

void Compare_Output::compare(int type, int level)
{
    if (type == FILE)
    {
        compare_helper(dir1->files, dir2->files, level);
    }
    else if (type == SUB_DIR)
    {
        compare_helper(dir1->sub_dir, dir2->sub_dir, level);
    }
}

您的方法不起作用的原因是該type僅在運行時已知,但T需要在編譯時設置,因此如果您在聲明的位置聲明t1t2則它們不能具有正確的類型。

請注意,關於您的代碼的其他方面在我看來仍然是錯誤的。 這足以修復您的問題所涉及的編譯器錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM