【发布时间】:2018-04-03 15:44:32
【问题描述】:
谁能告诉我为什么这段代码会产生“No matching function for call”错误?我相信语法是正确的,我真的不知道为什么它不起作用。我想用传递给函数的 unordered_map 中 some_key 的匹配值替换 Template_ 中出现的 {{some_key}}。
std::string View::Render(const std::unordered_map<std::string, std::string> &model) const {
for(int i = 0; i < this->Template_.length(); ++i){
if(this->Template_[i] == '{' && this->Template_[i+1] == '{'){
int PositionBegin = i;
std::string Key = FindKey();
if(Key.length() > 0) {
std::unordered_map<std::string, std::string>::const_iterator Found = model.find(Key);
if (Found != model.end())
this->Template_.replace(PositionBegin, Key.length()+4, Found->second);
}
}
}
return this->Template_;
}
View 类看起来就这么简单:
class View {
public:
View(const std::string &Template);
std::string Render(const std::unordered_map<std::string, std::string> &model) const;
std::string Template_;
};
完整的错误是:
error: no matching function for call to ‘std::__cxx11::basic_string<char>::replace(int&, std::__cxx11::basic_string<char>::size_type, const std::__cxx11::basic_string<char>&) const’ this->Template_.replace(PositionBegin, Key.length()+4, Found->second);
【问题讨论】: