传递给构造函数的形参不是类型
C++ template Passed in parameter to constructor is not a type
我正在研究一个索引类,它有一个默认构造函数和一个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);
}
};
相关文章:
- 非类型引用形参/实参
- 如何确保迭代器模板形参与模板类的模板形参具有相同的数据类型
- 如何在编译时通过模板形参默认值的名称/指针获取函数的类型
- 模板类的不同返回类型取决于类的形参
- 指向函数的指针vs作为模板非类型形参的函数
- 具有不同值类型模板形参的模板类的数组或向量
- 为什么模板非类型形参指针和引用实参需要是全局的
- 可以使用MemberFunction类型作为模板形参吗?
- c++ 03:是否有一种方法可以使一个类型每次被包含在模板形参中时都会编译成不同的类型?
- 将非const引用使用auto-keyword声明的lambda作为实参传递给std::函数形参类型
- 为什么标准不允许在模板形参列表中初始化依赖于常量的类型
- 在模板演绎中保持函数指针形参的完整类型
- 使用传入的字符串形参来访问类型中的内容
- 不能在模板专门化定义中将一个类的成员类型定义用作模板形参
- 使用函数返回值作为默认模板非类型形参
- 扩展非类型形参包来定义带有非类型形参的内部类模板是否合法?
- c++模板非类型形参的类型演绎
- 为静态成员函数模板非类型形参
- 带有不同类型的非类型形参的c++可变模板
- 非类型形参取决于它后面的实参