传递给构造函数的形参不是类型

C++ template Passed in parameter to constructor is not a type

本文关键字:类型 形参 构造函数      更新时间:2023-10-16

我正在研究一个索引类,它有一个默认构造函数和一个lambda函数的构造函数。

每当我尝试使用参数化构造函数创建对象时,我都会得到以下错误。

   std::function<int(const std::string & key)> hash = [] (const std::string & key){return 0;};         
   PrimaryTreeIndex<int> index(hash);                                                                  
   //OK -> PrimaryTreeIndex<int> index();       
/home/prakash/index_search/PrimaryTreeIndexTest.cpp:11:37: error: ‘hash’ is not a type
         PrimaryTreeIndex<int> index(hash);

我做错了什么?默认构造函数可以工作。这是PrimaryTreeIndex类的代码。整个项目在这里https://github.com/spakai/index_search

#pragma once
#include "Index.h"
#include "IndexSearchException.h"
#include <vector>
#include <memory>
#include <map>
#include <functional>
template <typename T>
class PrimaryTreeIndexBase: public Index {
    public:
        PrimaryTreeIndexBase()
            : hash([] (const std::string & key){return 0;}) {
        }
        PrimaryTreeIndexBase(std::function<int(const std::string & key)> hash)
            :hash(hash) {
        } 
        void buildIndex(Table & table, int index_column) {}

        void buildIndex(Table & table, int index_column, int value_column) {}
        const T& exactMatch(const std::string& key) const {
            int hashed_index = hash(key);
            auto index = indexes.at(hashed_index);
            auto it = index->find(key);
            if(it == index->end()) {
                throw IndexSearchException("No match found"); 
            } else {
                return it->second;
            }
        } 
        const T& bestMatch(const std::string& key) const {
            auto index = indexes.at(hash(key));
            auto lower_bound = index->lower_bound(key);
            if(lower_bound != index->end() && lower_bound->first == key) {
                return lower_bound->second; 
            } else {
                typename std::map<std::string,T>::const_reverse_iterator rbegin(lower_bound); 
                typename std::map<std::string,T>::const_reverse_iterator rend(index->begin()); 
                for(auto it = rbegin; it!=rend; it++) {
                    auto idx = key.find(it->first);
                    if(idx != std::string::npos) {
                        return it->second;
                    } 
                }
            }
            throw IndexSearchException("No match found");
        }
        int size() const {
            auto index = indexes.at(0);
            return index->size();
        }
        std::function<int(const std::string & key)> hash;
        std::vector<std::shared_ptr<std::map<std::string,T>>> indexes = {nullptr};
};
template<typename T>
class PrimaryTreeIndex : public PrimaryTreeIndexBase<T> {
    public:
        PrimaryTreeIndex()                                                                                        
            :PrimaryTreeIndexBase<T>() {}                                                                   
        PrimaryTreeIndex(std::function<int(const std::string & key)> hash)                                        
            :PrimaryTreeIndexBase<T>(hash) {}             
};

template <>
class PrimaryTreeIndex<int>: public PrimaryTreeIndexBase<int> {
    public:
    PrimaryTreeIndex()
            :PrimaryTreeIndexBase<int>() {}
    PrimaryTreeIndex(std::function<int(const std::string & key)> hash)
        :PrimaryTreeIndexBase<int>(hash) {}
    void buildIndex(Table & table, int index_column) {
        int rowno = 0;
        for(auto currentRow : table) {
            std::string key = currentRow[index_column];
            int hashed_index = hash(key);
            if(indexes.at(hashed_index) == nullptr) { 
                indexes[hashed_index] = std::make_shared<std::map<std::string,int>>();     
            }
            auto index = indexes.at(hashed_index);
            index->emplace(currentRow[index_column], rowno++);
        }
    }
};  
template <>
class PrimaryTreeIndex<std::string>: public PrimaryTreeIndexBase<std::string> {
    public:
    PrimaryTreeIndex()
            :PrimaryTreeIndexBase<std::string>() {}
    PrimaryTreeIndex(std::function<int(const std::string & key)> hash)
        :PrimaryTreeIndexBase<std::string>(hash) {}

    void buildIndex(Table & table, int index_column, int value_column) {
            for(auto currentRow : table) {
                std::string key = currentRow[index_column];
                int hashed_index = hash(key);
                if(indexes.at(hashed_index) == nullptr) { 
                    indexes[hashed_index] = std::make_shared<std::map<std::string,std::string>>();     
                }
                auto index = indexes.at(hashed_index);
                index->emplace(currentRow[index_column], currentRow[value_column]);
            }
        }
};

问题是我的GTest: this works

TEST(PrimaryTreeIndexTestWithMultiMaps,GetSizeofIndex) {                                                      
   FileTable ft;                                                                                              
   ft.init("../csv/bnumber2.csv");                                                                            
   std::function<int(const std::string & key)> hash = [] (const std::string & key){return 0;};                
   PrimaryTreeIndex<int> index(hash);                                                                         
   index.buildIndex(ft, 0);                                                                                   
   ASSERT_THAT(index.size(), Eq(56));                                                                         
}                    

class PrimaryTreeIndexTestWithRowId : public Test {                                                           
    public:                                                                                                   
        FileTable ft;                                                                                         
       std::function<int(const std::string & key)> hash = [] (const std::string & key){return 0;};                
       PrimaryTreeIndex<int> index(hash);                                                                         
        void SetUp() override {                                                                               
            ft.init("../csv/bnumber2.csv");                                                                   
            index.buildIndex(ft, 0);                                                                          
        }                                                                                                     
};