关于vc编译问题?

hewittlee 2004-08-01 04:19:47
#include <algorithm>
#include <string>
#include <vector>
#include <utility>

#include <iostream.h>
#include <fstream.h>

#include <stddef.h>

using namespace std;

extern vector<string,allocator> *retrieve_text();

int main()
{
vector<string,allocator> *text_file = retrieve_text();


cout << "----------- about to generate text read --------------\n";
ostream_iterator< string > output( cout, "\n" );
copy( text_file->begin(), text_file->end(), output );

return 0;
}

vector<string,allocator>*
retrieve_text()
{
string file_name;

cout << "please enter file name: ";
cin >> file_name;

ifstream infile( file_name.c_str(), ios::in );
if ( !infile ) {
cerr << "oops! unable to open file "
<< file_name << " -- bailing out!\n";
exit( -1 );
}
else cout << "\n";

vector<string,allocator> *lines_of_text = new vector<string,allocator>;
string textline;

typedef pair<string::size_type, int> stats;
stats maxline;
int linenum = 0;

while ( getline( infile, textline, '\n' ))
{
cout << "line read: " << textline << "\n";

if ( maxline.first < textline.length() )
{
maxline.first = textline.length();
maxline.second = linenum;
}

lines_of_text->push_back( textline );
linenum++;
}

cout << "\n";
cout << "number of lines: "
<< lines_of_text->size() << "\n";

cout << "maximum length: "
<< maxline.first << "\n";

cout << "longest line: "
<< (*lines_of_text)[ maxline.second ] << "\n";

return lines_of_text;
}

/*
dumbo[107] ~/d.stdlib => a.out
please enter file name: alice_emma

line read: Alice Emma has long flowing red hair. Her Daddy says
line read: when the wind blows through her hair, it looks almost alive,
line read: like a fiery bird in flight. A beautiful fiery bird, he tells her,
line read: magical but untamed. "Daddy, shush, there is no such thing,"
line read: she tells him, at the same time wanting him to tell her more.
line read: Shyly, she asks, "I mean, Daddy, is there?"

number of lines: 6
maximum length: 66
longest line: like a fiery bird in flight. A beautiful fiery bird, he tells her,
----------- about to generate text read --------------
Alice Emma has long flowing red hair. Her Daddy says
when the wind blows through her hair, it looks almost alive,
like a fiery bird in flight. A beautiful fiery bird, he tells her,
magical but untamed. "Daddy, shush, there is no such thing,"
she tells him, at the same time wanting him to tell her more.
Shyly, she asks, "I mean, Daddy, is there?"
*/

//出现的问题
Compiling...
main0.cpp
C:\exam\main0\main0.cpp(13) : error C2955: 'allocator' : use of class template requires template argument list
c:\program files\microsoft visual studio\vc98\include\xmemory(72) : see declaration of 'allocator'
C:\exam\main0\main0.cpp(17) : error C2955: 'allocator' : use of class template requires template argument list
c:\program files\microsoft visual studio\vc98\include\xmemory(72) : see declaration of 'allocator'
C:\exam\main0\main0.cpp(21) : error C2664: '__thiscall std::ostream_iterator,class std::allocator >,char,struct std::char_traits >::std::ostream_iterator,class std::allocator >,char,struct std::char_traits >(class std::basic_ostream > &,const char *)' : cannot convert parameter 1 from 'class ostream_withassign' to 'class std::basic_ostream > &'
A reference that is not to 'const' cannot be bound to a non-lvalue
C:\exam\main0\main0.cpp(22) : error C2663: 'begin' : 2 overloads have no legal conversion for 'this' pointer
C:\exam\main0\main0.cpp(22) : error C2663: 'end' : 2 overloads have no legal conversion for 'this' pointer
C:\exam\main0\main0.cpp(27) : error C2955: 'allocator' : use of class template requires template argument list
c:\program files\microsoft visual studio\vc98\include\xmemory(72) : see declaration of 'allocator'
C:\exam\main0\main0.cpp(33) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string,class std::allocator >' (or there is no acceptable conversion)
C:\exam\main0\main0.cpp(35) : error C2872: 'ifstream' : ambiguous symbol
C:\exam\main0\main0.cpp(38) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string,class std::allocator >' (or there is no acceptable conversion)
C:\exam\main0\main0.cpp(43) : error C2955: 'allocator' : use of class template requires template argument list
c:\program files\microsoft visual studio\vc98\include\xmemory(72) : see declaration of 'allocator'
C:\exam\main0\main0.cpp(43) : error C2955: 'allocator' : use of class template requires template argument list
c:\program files\microsoft visual studio\vc98\include\xmemory(72) : see declaration of 'allocator'
C:\exam\main0\main0.cpp(43) : error C2512: 'vector,class std::allocator >,class std::allocator>' : no appropriate default constructor available
C:\exam\main0\main0.cpp(50) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &,const _E)' : could not deduce template argument for 'class std::basic_istream<_E,_Tr> &' from 'class ifstream'
C:\exam\main0\main0.cpp(50) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &,const _E)' : could not deduce template argument for 'class std::basic_istream<_E,_Tr> &' from 'class ifstream'
C:\exam\main0\main0.cpp(50) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &,const _E)' : could not deduce template argument for 'class std::basic_istream<_E,_Tr> &' from 'class ifstream'
C:\exam\main0\main0.cpp(50) : error C2780: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &)' : expects 2 arguments - 3 provided
c:\program files\microsoft visual studio\vc98\include\string(145) : see declaration of 'getline'
C:\exam\main0\main0.cpp(50) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.

这是c++ primer 的源码,由于本人还未学vc,希望各位高手能写个步骤给小弟编译成功!还有alice_emma这个文件是要自己建立的吗?谢谢!


...全文
360 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hewittlee 2004-08-02
  • 打赏
  • 举报
回复
我用的是vc6.0,难道不能解决吗?
antijpn 2004-08-01
  • 打赏
  • 举报
回复
把所有的",allocator"去掉就可以了,如果还是不行的话,你用的就应该是VC++6.0,VC++6.0对模板的支持很有问题,升级到7.1就可以解决了
lingjingqiu 2004-08-01
  • 打赏
  • 举报
回复
觉得哪里错误啊?
micty 2004-08-01
  • 打赏
  • 举报
回复
我不懂,但我还是帮楼主UP一下
Dong 2004-08-01
  • 打赏
  • 举报
回复
可以这样定义的吗?
vector<string,allocator> *retrieve_text();
我第一次看到。觉得是错误的

64,641

社区成员

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

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