用tc++pl书上讲的东西为什么会出错?
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>等也会提示无法包括进去,是不是版本有问题?