用tc++pl书上讲的东西为什么会出错?

adrianfeng 2006-09-16 10:28:36
the c++ programing language 书的程序,为什么编译不出来?像下面这个程序用VC++编的

#include<string>
#include<vector>
#include<algorithm>
#include<fstream.h>
#include<istream.h>
#include<ostream.h>
using namespace std;
int main()
{
string from,to;
cin>>from>>to; //取得源文件名和目标文件名

ifstream is(from.c_str()); //输入流
istream_iterator<string> ii(is); //流的输入迭代器
istream_iterator<string> eos; //输入的哨兵

vector<string> b(ii,eos); //b是一个向量,用输入进行初始化
sort(b.begin(),b.end()); //对缓冲区排序

ofstream os(to.c_str()); //输出流
ostream_iterator<string> oo(os,"\n"); //流的输出迭代器

unique_copy(b.begin(),b.end(),oo); //从缓冲区复制到输出,并去掉重复的值

return !is.eof()||!os; //返回错误信息
}

Compiling...
ab.cpp
e:\my exercise\input\ab.cpp(12) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion
)
e:\my exercise\input\ab.cpp(14) : error C2872: 'ifstream' : ambiguous symbol
e:\my exercise\input\ab.cpp(15) : error C2664: '__thiscall std::istream_iterator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,char,struct std::char_traits<char> >::std::istream_iterator<class std::basic_str
ing<char,struct std::char_traits<char>,class std::allocator<char> >,char,struct std::char_traits<char> >(class std::basic_istream<char,struct std::char_traits<char> > &)' : cannot convert parameter 1 from 'class ifstream' to 'class std::basic_istrea
m<char,struct std::char_traits<char> > &'
A reference that is not to 'const' cannot be bound to a non-lvalue
e:\my exercise\input\ab.cpp(18) : error C2664: '__thiscall std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std:
:allocator<char> > > >::std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >(unsigned int
,const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,const class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > &)' : cannot convert parameter 1 fr
om 'class std::istream_iterator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,char,struct std::char_traits<char> >' to 'unsigned int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
e:\my exercise\input\ab.cpp(21) : error C2872: 'ofstream' : ambiguous symbol
e:\my exercise\input\ab.cpp(22) : error C2664: '__thiscall std::ostream_iterator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,char,struct std::char_traits<char> >::std::ostream_iterator<class std::basic_str
ing<char,struct std::char_traits<char>,class std::allocator<char> >,char,struct std::char_traits<char> >(class std::basic_ostream<char,struct std::char_traits<char> > &,const char *)' : cannot convert parameter 1 from 'class ofstream' to 'class std:
:basic_ostream<char,struct std::char_traits<char> > &'
A reference that is not to 'const' cannot be bound to a non-lvalue
Error executing cl.exe.

ab.exe - 6 error(s), 0 warning(s)


为什么会这样呢?
我又做了些改动,紧接着有出现如下问题

#include<string>
#include<vector>
#include<algorithm>
#include<fstream.h>
#include<istream.h>
#include<ostream.h>
using namespace std;
int main()
{
char a[3],b[3];
cin>>a>>b;


ifstream is(a);
istream_iterator<string> ii(is);
istream_iterator<string> eos;

vector<string> b(ii,eos);
oftream os(b);
ofstream_iterator<string> oo(os,"\n");

copy(b.begin(),b.end(),oo);
return !is.eof()||!os;
}


Compiling...
lala.cpp
e:\My exercise\input\lala.cpp(14) : error C2872: 'ifstream' : ambiguous symbol
e:\My exercise\input\lala.cpp(15) : error C2664: '__thiscall std::istream_iterator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,char,struct std::char_traits<char> >::std::istream_iterator<class std::basic_s
tring<char,struct std::char_traits<char>,class std::allocator<char> >,char,struct std::char_traits<char> >(class std::basic_istream<char,struct std::char_traits<char> > &)' : cannot convert parameter 1 from 'class ifstream' to 'class std::basic_istr
eam<char,struct std::char_traits<char> > &'
A reference that is not to 'const' cannot be bound to a non-lvalue
e:\My exercise\input\lala.cpp(18) : error C2040: 'b' : 'class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class s
td::allocator<char> > > >' differs in levels of indirection from 'char [3]'
e:\My exercise\input\lala.cpp(18) : error C2664: '__thiscall std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class st
d::allocator<char> > > >::std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >(unsigned i
nt,const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,const class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > &)' : cannot convert parameter 1
from 'class std::istream_iterator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,char,struct std::char_traits<char> >' to 'unsigned int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
e:\My exercise\input\lala.cpp(19) : error C2065: 'oftream' : undeclared identifier
e:\My exercise\input\lala.cpp(19) : error C2146: syntax error : missing ';' before identifier 'os'
e:\My exercise\input\lala.cpp(19) : error C2065: 'os' : undeclared identifier
e:\My exercise\input\lala.cpp(20) : error C2065: 'ofstream_iterator' : undeclared identifier
e:\My exercise\input\lala.cpp(20) : error C2275: 'string' : illegal use of this type as an expression
d:\program files\vc98\include\xstring(612) : see declaration of 'string'
e:\My exercise\input\lala.cpp(20) : error C2065: 'oo' : undeclared identifier
e:\My exercise\input\lala.cpp(22) : error C2228: left of '.begin' must have class/struct/union type
e:\My exercise\input\lala.cpp(22) : error C2228: left of '.end' must have class/struct/union type
Error executing cl.exe.

lala.exe - 12 error(s), 0 warning(s)


不知是什么地方出问题了?请指点一下。还有,为什么在TC++上写using namespace std会出现编译错误?用#include<vector>等也会提示无法包括进去,是不是版本有问题?
...全文
512 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lw1a2 2006-09-17
  • 打赏
  • 举报
回复
改下头文件:

#include<string>
#include<vector>
#include<algorithm>
#include<fstream>
#include<iterator>
#include <iostream>
using namespace std;

int main()
{
//istream cin;//在std中cin已经预定义了,不用再次定义
string from,to;
cin>>from>>to; //取得源文件名和目标文件名

ifstream is(from.c_str()); //输入流
istream_iterator<string> ii(is); //流的输入迭代器
istream_iterator<string> eos; //输入的哨兵

vector<string> b(ii,eos); //b是一个向量,用输入进行初始化
sort(b.begin(),b.end()); //对缓冲区排序

ofstream os(to.c_str()); //输出流
ostream_iterator<string> oo(os,"\n"); //流的输出迭代器

unique_copy(b.begin(),b.end(),oo); //从缓冲区复制到输出,并去掉重复的值

return !is.eof()||!os; //返回错误信息
}
universee 2006-09-17
  • 打赏
  • 举报
回复
hh
adrianfeng 2006-09-17
  • 打赏
  • 举报
回复
原来头文件讲究那么多,可是还是有一个错误,在vector<string> b(ii,eos);上。还有,像这种问题怎么知道是应该包含那个头文件呢?比如,怎么知道iostream+istream而不是ostream+istream呢?多谢各位了,实在是菜呀
:\my exercise\ss.cpp(19) : error C2664: '__thiscall std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::alloc
ator<char> > > >::std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >(unsigned int,const
class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,const class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > &)' : cannot convert parameter 1 from 'cl
ass std::istream_iterator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,char,struct std::char_traits<char> >' to 'unsigned int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.

SS.exe - 1 error(s), 0 warning(s)
adrianfeng 2006-09-16
  • 打赏
  • 举报
回复
改了,现在是这样。但是它会说cin无定义,怎么回事?

#include<string>
#include<vector>
#include<algorithm>
#include<fstream>
#include<istream>
#include<ostream>
using namespace std;

int main()
{
istream cin;
string from,to;
cin>>from>>to; //取得源文件名和目标文件名

ifstream is(from.c_str()); //输入流
istream_iterator<string> ii(is); //流的输入迭代器
istream_iterator<string> eos; //输入的哨兵

vector<string> b(ii,eos); //b是一个向量,用输入进行初始化
sort(b.begin(),b.end()); //对缓冲区排序

ofstream os(to.c_str()); //输出流
ostream_iterator<string> oo(os,"\n"); //流的输出迭代器

unique_copy(b.begin(),b.end(),oo); //从缓冲区复制到输出,并去掉重复的值

return !is.eof()||!os; //返回错误信息
}


Compiling...
ab.cpp
E:\My exercise\input\ab.cpp(11) : error C2512: 'basic_istream<char,struct std::char_traits<char> >' : no appropriate default constructor available
E:\My exercise\input\ab.cpp(19) : error C2664: '__thiscall std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std:
:allocator<char> > > >::std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >(unsigned int
,const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,const class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > &)' : cannot convert parameter 1 fr
om 'class std::istream_iterator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,char,struct std::char_traits<char> >' to 'unsigned int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.

ab.exe - 2 error(s), 0 warning(s)
adrianfeng 2006-09-16
  • 打赏
  • 举报
回复
那string 的问题呢?
vvvvy 2006-09-16
  • 打赏
  • 举报
回复
#include<fstream.h>
#include<istream.h>
#include<ostream.h>

改:

#include<fstream>
#include<istream>
#include<ostream>

64,642

社区成员

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

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