分配c+时出现类型错误+;映射迭代器 我正在努力从《加速C++》中学习C++。当我试图编译下面的代码时,我认为这是书中代码的精确副本,我得到了以下错误,我不明白为什么。另外,我不知道如何调试这个错误。任何指向该方向的指示(快速调试此类错误的问题解决策略)都将非常感谢。提前谢谢!以下是编译器错误: g++ main.cpp split.cpp -o main main.cpp: In function ‘void gen_aux(const Grammar&, const string&, std::vector<std::basic_string<char> >&)’: main.cpp:64:41: error: no match for call to ‘(const std::vector<std::vector<std::basic_string<char> > >) ()’ make: *** [all] Error 1

分配c+时出现类型错误+;映射迭代器 我正在努力从《加速C++》中学习C++。当我试图编译下面的代码时,我认为这是书中代码的精确副本,我得到了以下错误,我不明白为什么。另外,我不知道如何调试这个错误。任何指向该方向的指示(快速调试此类错误的问题解决策略)都将非常感谢。提前谢谢!以下是编译器错误: g++ main.cpp split.cpp -o main main.cpp: In function ‘void gen_aux(const Grammar&, const string&, std::vector<std::basic_string<char> >&)’: main.cpp:64:41: error: no match for call to ‘(const std::vector<std::vector<std::basic_string<char> > >) ()’ make: *** [all] Error 1,c++,C++,完整的代码列表: #include <cstdlib> #include <iostream> #include <map> #include <stdexcept> #include <string> #include <vector> #include "split.h" using std::cin; using std::cout; using std::domain_error; using std::endl;

完整的代码列表:

#include <cstdlib>
#include <iostream>
#include <map>
#include <stdexcept>
#include <string>
#include <vector>
#include "split.h"

using std::cin;
using std::cout;
using std::domain_error;
using std::endl;
using std::istream;
using std::logic_error;
using std::map;
using std::pair;
using std::string;
using std::vector;

typedef vector<string> Rule;
typedef vector<Rule> Rule_collection;
typedef map<string, Rule_collection> Grammar;   

int nrand(int n) {
    if (n <= 0 || n > RAND_MAX) {
        throw domain_error("Argument to nrand is out of range");
    }
    const int bucket_size = RAND_MAX / n;
    int r;
    do {
        r = rand() / bucket_size;
    } while (r >= n);

    return r;
}

bool bracketed(const string& s) {
    return s.length() > 0 && s[0] == '<' && s[s.length() - 1] == '>';
}

Grammar read_grammar(istream& in) {
    Grammar g;
    string line;
    while (getline(in, line)) {
        vector<string> words = split(line);
        if (!words.empty()){
            g[words[0]].push_back(Rule(words.begin() + 1, words.end()));
        } 
    }
    return g;
}

void gen_aux(const Grammar& g, const string& word, vector<string>& ret) {
    if (!bracketed(word)) {
        ret.push_back(word);
    }
    else {
        Grammar::const_iterator it = g.find(word);
        if (it == g.end()) {
            throw logic_error("empty rule");
        }
        const Rule_collection& c = it->second();
        const Rule& r = c[nrand(c.size())];
        for (Rule::const_iterator i = r.begin(); i != r.end(); i++) {
            gen_aux(g, *i, ret);
        }
    }
}

vector<string> gen_sentence(const Grammar& g) {
    vector<string> s;
    gen_aux(g, "<sentence>", s);
    return s;
}

int main() {
    vector<string> sentence = gen_sentence(read_grammar(cin));
    if (!sentence.empty()) {
        vector<string>::const_iterator it = sentence.begin();
        cout << *it;
        it++;
        while (it != sentence.end()) {
            cout << " " << *it;
            it++;
        }
        cout << endl;
    }
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括“split.h”
使用std::cin;
使用std::cout;
使用std::domain\u错误;
使用std::endl;
使用std::istream;
使用std::logic\u错误;
使用std::map;
使用std::pair;
使用std::string;
使用std::vector;
类型定义向量规则;
typedef向量规则集合;
typedef映射语法;
内特诺兰(内特诺兰){
如果(n RAND_MAX){
抛出域_错误(“nrand的参数超出范围”);
}
const int bucket_size=RAND_MAX/n;
INTR;
做{
r=兰德()/桶大小;
}而(r>=n);
返回r;
}
带括号的bool(常数串和s){
返回s.length()>0&&s[0]='';
}
语法阅读语法(istream&in){
语法g;
弦线;
while(getline(in,line)){
矢量字=拆分(行);
如果(!words.empty()){
g[words[0]].向后推(规则(words.begin()+1,words.end());
} 
}
返回g;
}
void gen_aux(常量语法和g、常量字符串和单词、向量和ret){
如果(!括号内(单词)){
向后推(字);
}
否则{
语法::const_迭代器it=g.find(word);
如果(it==g.end()){
抛出逻辑_错误(“空规则”);
}
const Rule_collection&c=it->second();
常量规则&r=c[nrand(c.size())];
for(Rule::const_迭代器i=r.begin();i!=r.end();i++){
辅助发电机(g、*i、ret);
}
}
}
向量生成句(const-Grammar&g){
向量s;
辅助发电机(g,“,s);
返回s;
}
int main(){
向量句=生成句(读语法(cin));
如果(!句子.empty()){
vector::const_迭代器it=句子。开始();

cout您需要
it->second
,不带括号。
second
是函数的数据成员,而不是成员函数

const Rule_collection& c = it->second;

您需要
it->second
,不带括号。
second
是函数的数据成员,而不是函数的成员

const Rule_collection& c = it->second;

我猜这只是“const Rule\u collection&c=it->second
否则,您将尝试像函数一样调用向量。

我想这只是“const Rule\u collection&c=it->second;”
const Rule_collection& c = it->second;
否则,您将尝试像调用函数一样调用向量

const Rule_collection& c = it->second;