错误:一个无法解析的外部命令

他过江 2016-07-22 11:58:48
#include<iostream>
#include<map>
#include<string>
#include<fstream>
#include<sstream>
#include<algorithm>
using namespace std;

map<string, string> rule(ifstream &rule_file);
const string &output(const string &s, map<string, string> &m);

void Transform(ifstream &rule_file, ifstream &source_file)
{
auto trans_rule = rule(rule_file);
string line;
while (getline(source_file, line))
{
istringstream in(line);
string word;
bool firstword = true;
while (in >> word)
{
if (firstword)
firstword = false;
else
cout << " ";
cout << output(word, trans_rule);
}
cout << endl;
}
}

map<string, string> rule(ifstream &rule_file)
{
map<string, string> m;
string word, expression;
while (rule_file >> word && getline(rule_file, expression))
if (expression.size() > 1)
m[word] = expression.substr(1);
else
throw runtime_error("no rule for " + word);
return m;
}

const string &output(const string &s, const map<string, string> &m)
{
auto map_it = m.find(s);
if (map_it != m.cend())
return map_it->second;
else
return s;
}

int main(int argc, char *argv[])
{
ifstream in(argv[2]);
ifstream ru(argv[1]);
if (!ru)
{
cerr << "file1 open error" << endl;
system("pause");
exit(-1);
}
if (!in)
{
cerr << "file2 open error" << endl;
system("pause");
exit(-1);
}
in.close();
ru.close();
Transform(ru, in);
system("pause");
return 0;
}
为什么我把rule函数和output函数提前声明就有错,而两个函数的函数体放在调用它们的Transform函数之前就可以?
...全文
561 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lm_whales 2016-07-23
  • 打赏
  • 举报
回复
没看懂,你的问题
in.close();
ru.close();
Transform(ru, in);
不过,你的文件(流)都关闭了(close ),还能读写吗? 你是怎么做到的?
paschen 版主 2016-07-23
  • 打赏
  • 举报
回复
对于output函数第二个参数,声明是map<string, string> &m,而实现的时候写的是const map<string, string> &m
小灸舞 2016-07-23
  • 打赏
  • 举报
回复
说明你的声明和实现不一致。 注意大小写,类型,拼写
flying_music 2016-07-23
  • 打赏
  • 举报
回复
output函数的声明和定义不一样,声明时少了一个const

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧