请高手指点 为什么C++ Primer 中的例子程序在VC6.0中不能通过编译呢??
在学习C++时调试书中的例子程序,在gcc中可以编译通过没有任何问题。但是在VC中却总是抱错不能通过编译,下面是程序和错误警告:
#include <vector>
/***************
( 0 )( bot: :top )
iStack::push( 2 )
iStack::push( 4 )
( 2 )( bot: 2 4 :top )
iStack::push( 6 )
iStack::push( 8 )
iStack::push( 10 )
( 5 )( bot: 2 4 6 8 10 :top )
iStack::pop(): 10
iStack::pop(): 8
( 3 )( bot: 2 4 6 :top )
iStack::push( 12 )
iStack::push( 14 )
( 5 )( bot: 2 4 6 12 14 :top )
iStack::push( 16 )
iStack::push( 18 )
iStack::push( 20 )
( 8 )( bot: 2 4 6 12 14 16 18 20 :top )
iStack::pop(): 20
iStack::pop(): 18
( 6 )( bot: 2 4 6 12 14 16 :top )
iStack::push( 22 )
iStack::push( 24 )
( 8 )( bot: 2 4 6 12 14 16 22 24 :top )
iStack::push( 26 )
iStack::push( 28 )
iStack::push( 30 )
( 11 )( bot: 2 4 6 12 14 16 22 24 26 28 30 :top )
iStack::pop(): 30
iStack::pop(): 28
( 9 )( bot: 2 4 6 12 14 16 22 24 26 :top )
iStack::push( 32 )
iStack::push( 34 )
( 11 )( bot: 2 4 6 12 14 16 22 24 26 32 34 :top )
iStack::push( 36 )
iStack::push( 38 )
iStack::push( 40 )
( 14 )( bot: 2 4 6 12 14 16 22 24 26 32 34 36 38 40 :top )
iStack::pop(): 40
iStack::pop(): 38
( 12 )( bot: 2 4 6 12 14 16 22 24 26 32 34 36 :top )
iStack::push( 42 )
iStack::push( 44 )
( 14 )( bot: 2 4 6 12 14 16 22 24 26 32 34 36 42 44 :top )
iStack::push( 46 )
iStack::push( 48 )
iStack::push( 50 )
( 17 )( bot: 2 4 6 12 14 16 22 24 26 32 34 36 42 44 46 48 50 :top )
iStack::pop(): 50
iStack::pop(): 48
( 15 )( bot: 2 4 6 12 14 16 22 24 26 32 34 36 42 44 46 :top )
****************/
class iStack {
public:
iStack( int capacity )
: _stack( capacity ), _top( 0 ) {};
bool pop( int &value );
bool push( int value );
bool full();
bool empty();
void display();
int size();
private:
int _top;
vector< int, allocator > _stack;
};
inline int iStack::size() { return _top; };
inline bool iStack::empty() { return _top ? false : true; }
inline bool iStack::full() { return _top < _stack.size()-1 ? false : true; };
bool iStack::pop( int &top_value )
{
if ( empty() )
return false;
top_value = _stack[ --_top ];
cout << "iStack::pop(): " << top_value << endl;
return true;
}
bool iStack::push( int value ) {
cout << "iStack::push( " << value << " )\n";
if ( full() )
return false;
_stack[ _top++ ] = value;
return true;
};
void iStack::display()
{
cout << "( " << size() << " )( bot: ";
for ( int ix = 0; ix < _top; ++ix )
cout << _stack[ ix ] << " ";
cout << " :top )\n";
};
#include <iostream>
//#include <iostream.h>
int main()
{
iStack stack( 32 );
stack.display();
for ( int ix = 1; ix < 51; ++ix )
{
if ( ix%2 == 0 )
stack.push( ix );
if ( ix%5 == 0 )
stack.display();
if ( ix%10 == 0) {
int dummy;
stack.pop( dummy ); stack.pop( dummy );
stack.display();
}
}
}
VC总是报错,如下:
--------------------Configuration: stack - Win32 Debug--------------------
Compiling...
stack.cpp
G:\lk\cpp\第四章\stack\stack.cpp(73) : error C2143: syntax error : missing ';' before '<'
G:\lk\cpp\第四章\stack\stack.cpp(73) : error C2501: 'vector' : missing storage-class or type specifiers
G:\lk\cpp\第四章\stack\stack.cpp(73) : error C2059: syntax error : '<'
G:\lk\cpp\第四章\stack\stack.cpp(73) : error C2238: unexpected token(s) preceding ';'
G:\lk\cpp\第四章\stack\stack.cpp(78) : error C2065: '_stack' : undeclared identifier
G:\lk\cpp\第四章\stack\stack.cpp(78) : error C2228: left of '.size' must have class/struct/union type
G:\lk\cpp\第四章\stack\stack.cpp(85) : error C2109: subscript requires array or pointer type
G:\lk\cpp\第四章\stack\stack.cpp(87) : error C2065: 'cout' : undeclared identifier
G:\lk\cpp\第四章\stack\stack.cpp(87) : error C2297: '<<' : illegal, right operand has type 'char [16]'
G:\lk\cpp\第四章\stack\stack.cpp(87) : error C2065: 'endl' : undeclared identifier
G:\lk\cpp\第四章\stack\stack.cpp(94) : error C2297: '<<' : illegal, right operand has type 'char [15]'
G:\lk\cpp\第四章\stack\stack.cpp(99) : error C2109: subscript requires array or pointer type
G:\lk\cpp\第四章\stack\stack.cpp(99) : error C2106: '=' : left operand must be l-value
G:\lk\cpp\第四章\stack\stack.cpp(105) : error C2297: '<<' : illegal, right operand has type 'char [3]'
G:\lk\cpp\第四章\stack\stack.cpp(108) : error C2109: subscript requires array or pointer type
G:\lk\cpp\第四章\stack\stack.cpp(108) : error C2297: '<<' : illegal, right operand has type 'char [2]'
G:\lk\cpp\第四章\stack\stack.cpp(110) : error C2297: '<<' : illegal, right operand has type 'char [9]'
G:\lk\cpp\第四章\stack\stack.cpp(135) : warning C4508: 'main' : function should return a value; 'void' return type assumed
Error executing cl.exe.
stack.obj - 17 error(s), 1 warning(s)
我试了好长时间都搞不定,请高手指点。