簡體   English   中英

將參數傳遞給構造函數的C ++模板不是類型

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

我正在處理一個具有默認構造函數和采用lambda函數的構造函數的Index類。

每當我嘗試使用參數化構造函數創建對象時,都會出現以下錯誤。

   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上:這可行

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);                                                                          
        }                                                                                                     
};     

暫無
暫無

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

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