C++ primer上的单词转换程序

seyisa 2009-02-17 09:27:18
程序是用map容器实现单词转换,但是我在VC编译器下编译一直无法通过,请各位指点。

#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <string>

using namespace std;

ifstream& open_file(ifstream &in, const string &file);


int main (int argc, char **argv)
{
map <string , string > trans_map;
string key,value;
if (argc != 3)
throw runtime_error("wrong number of arguments");
ifstream map_file;
if (!open_file(map_file, argv[1]))
throw runtime_error("no file for transforming");
while (map_file >> key >> value)
trans_map.insert(make_pair(key, value));
ifstream input;
if (!open_file(input, argv[2]))
throw runtime_error("no input file");
string line;
while (getline(input, line))
{
istringstream stream(line);
string word;
bool firstword = true;
while (stream >> word)
{
map<string,string>::const_iterator map_it = trans_map.find(word);
if (map_it != trans_map.end())
word = map_it->second;
if (firstword)
firstword = false;
else
cout << " ";
}
cout << endl;
}
return 0;
}


ifstream& open_file(ifstream &in, const string &file)
{
in.close();
in.clear();
in.open(file.c_str());
return in;
}



出了一堆错误:
D:\map_word.cpp(20) : error C2079: 'map_file' uses undefined class 'basic_ifstream<char,struct std::char_traits<char> >'
D:\map_word.cpp(21) : error C2664: 'open_file' : cannot convert parameter 1 from 'int' to 'class std::basic_ifstream<char,struct std::char_traits<char> > &'
A reference that is not to 'const' cannot be bound to a non-lvalue
D:\map_word.cpp(23) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::operator >>(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &)' : could not deduce template argument for 'class std::b
asic_istream<_E,_Tr> &' from 'int'
D:\map_word.cpp(23) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::operator >>(class std::basic_istream<_E,_Tr> &,short *)' : could not deduce template argument for 'class std::basic_istream<_E,_Tr> &' from
'int'
D:\map_word.cpp(23) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::operator >>(class std::basic_istream<_E,_Tr> &,unsigned char &)' : could not deduce template argument for 'class std::basic_istream<_E,_Tr>
&' from 'int'
D:\map_word.cpp(23) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::operator >>(class std::basic_istream<_E,_Tr> &,unsigned char *)' : could not deduce template argument for 'class std::basic_istream<_E,_Tr>
&' from 'int'
D:\map_word.cpp(23) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::operator >>(class std::basic_istream<_E,_Tr> &,signed char &)' : could not deduce template argument for 'class std::basic_istream<_E,_Tr> &'
from 'int'
D:\map_word.cpp(23) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::operator >>(class std::basic_istream<_E,_Tr> &,signed char *)' : could not deduce template argument for 'class std::basic_istream<_E,_Tr> &'
from 'int'
D:\map_word.cpp(23) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::operator >>(class std::basic_istream<_E,_Tr> &,_E &)' : could not deduce template argument for 'class std::basic_istream<_E,_Tr> &' from 'in
t'
D:\map_word.cpp(23) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::operator >>(class std::basic_istream<_E,_Tr> &,_E *)' : could not deduce template argument for 'class std::basic_istream<_E,_Tr> &' from 'in
t'
D:\map_word.cpp(23) : error C2677: binary '>>' : no global operator defined which takes type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)
D:\map_word.cpp(23) : fatal error C1903: unable to recover from previous error(s); stopping compilation
执行 cl.exe 时出错.
...全文
248 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
seyisa 2009-02-18
  • 打赏
  • 举报
回复
我知道了问题了,是因为我的test_out.txt文件里没有单词。原来这段代码是从test_out.txt里的单词来作为健来查找test_in.txt里的内容。。
搞定了,还是谢谢各位。
seyisa 2009-02-18
  • 打赏
  • 举报
回复
//学语言不要用VC6,对标准支持不好
请问飞雪,不用VC6用什么比较好呢?
还有就是我的程序也编译通过了,但是为何结果什么都没有呢?
baihacker 2009-02-17
  • 打赏
  • 举报
回复

//VC6编译通过
//学语言不要用VC6,对标准支持不好
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

ifstream& open_file(ifstream &in, const string &file);


int main (int argc, char **argv)
{
map <string , string > trans_map;
string key,value;
if (argc != 3)
throw runtime_error("wrong number of arguments");
ifstream map_file;
if (!open_file(map_file, argv[1]))
throw runtime_error("no file for transforming");
while (map_file >> key >> value)
trans_map.insert(make_pair(key, value));
ifstream input;
if (!open_file(input, argv[2]))
throw runtime_error("no input file");
string line;
while (getline(input, line))
{
istringstream stream(line);
string word;
bool firstword = true;
while (stream >> word)
{
map<string,string>::const_iterator map_it = trans_map.find(word);
if (map_it != trans_map.end())
word = map_it->second;
if (firstword)
firstword = false;
else
cout << " ";
}
cout << endl;
}
return 0;
}


ifstream& open_file(ifstream &in, const string &file)
{
in.close();
in.clear();
in.open(file.c_str());
return in;
}
  • 打赏
  • 举报
回复
改成无参数的main还不可以?等下帮你调试下看看。
seyisa 2009-02-17
  • 打赏
  • 举报
回复
照大家说的,改了头文件了,但是运行程序什么都没有,我已经在工程的同一目录下添加了2个文件,一个是test_in.txt,一个是test_out.txt。test_in.txt里有些单词。
改动的地方如下:

if (!open_file(map_file, "test_in.txt"))
{
cout << "no file for transforming" << endl;
throw runtime_error("no file for transforming");
}
while (map_file >> key >> value)
trans_map.insert(make_pair(key, value));
ifstream input;
if (!open_file(input, "test_out.txt"))
{
cout << "no input file" << endl;
throw runtime_error("no input file");
}

但是运行没有任何结果,为何啊?
waizqfor 2009-02-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 seyisa 的回复:]
弱弱地问个问题,关于io地操作,#include <iostream>还不够吗。
我本以为有这个就不需要#include <fstream>
#include <sstream> 这些了。
[/Quote]
不是一个#include <iostream> 就包含所有的 LZ 你用到的所有的
你看看那个两个头文件的内容就知道了 声明什么了
sagegz 2009-02-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 seyisa 的回复:]
弱弱地问个问题,关于io地操作,#include <iostream>还不够吗。
我本以为有这个就不需要#include <fstream>
#include <sstream> 这些了。
[/Quote]
http://hi.baidu.com/ctrlaltz/blog/item/613822510521c88c8d5430a2.html
xuruichen 2009-02-17
  • 打赏
  • 举报
回复
#pragma warning(disable:4786)//如果不加 很多警告
这个是干什么的?

楼主少了头文件。IO流文件。
xuruichen 2009-02-17
  • 打赏
  • 举报
回复
#pragma warning(disable:4786)//如果不加 很多警告
这个是干什么的?

楼主少了头文件。IO流文件。
seyisa 2009-02-17
  • 打赏
  • 举报
回复
弱弱地问个问题,关于io地操作,#include <iostream>还不够吗。
我本以为有这个就不需要#include <fstream>
#include <sstream> 这些了。
waizqfor 2009-02-17
  • 打赏
  • 举报
回复
[Quote=引用楼主 seyisa 的帖子:]
程序是用map容器实现单词转换,但是我在VC编译器下编译一直无法通过,请各位指点。

C/C++ code
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <string>

using namespace std;

ifstream& open_file(ifstream &in, const string &file);


int main (int argc, char **argv)
{
map <string , string > trans_map;
string key,value;
if (argc != 3)

[/Quote]
改完了 LZ看看

#pragma warning(disable:4786)//如果不加 很多警告
#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <string>

using namespace std;

ifstream& open_file(ifstream &in, const string &file);


int main (int argc, char **argv)
{
map <string , string > trans_map;
string key,value;
if (argc != 3)
throw runtime_error("wrong number of arguments");
ifstream map_file;
if (!open_file(map_file, argv[1]))
throw runtime_error("no file for transforming");
while (map_file >> key >> value)
trans_map.insert(make_pair(key, value));
ifstream input;
if (!open_file(input, argv[2]))
throw runtime_error("no input file");
string line;
while (getline(input, line))
{
istringstream stream(line);
string word;
bool firstword = true;
while (stream >> word)
{
map<string,string>::const_iterator map_it = trans_map.find(word);
if (map_it != trans_map.end())
word = map_it->second;
if (firstword)
firstword = false;
else
cout << " ";
}
cout << endl;
}
return 0;
}


ifstream& open_file(ifstream &in, const string &file)
{
in.close();
in.clear();
in.open(file.c_str());
return in;
}
sagegz 2009-02-17
  • 打赏
  • 举报
回复
缺少了2个头文件.
#include <fstream>
#include <sstream>
xkyx_cn 2009-02-17
  • 打赏
  • 举报
回复

#include <fstream>
#include <sstream>

少了这2个头文件,加上就行了

如果用vc 6编译的话,最好在最前面加上下面这句,不然一大堆警告:

#pragma warning(disable:4786)
  • 打赏
  • 举报
回复
你的main()函数要输入参数,但是你编译后,运行exe应该没有设置输入参数吧?

64,645

社区成员

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

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