有谁能帮我一把,不胜感谢!
stack.h文件:
#include <string>
#include <vector>
using namespace std;
class stack{
public:
bool push(const string&);
bool pop(string &elem);
bool peek(string &elem);
bool empty() const {return _stack.empty();}
bool full() const { return _stack.size()==_stack.max_size();}
int size() const {return _stack.size();}
private:
vector<string>_stack;
}
stack.cpp文件:
#include "stack.h"
#include <iostream>
using namespace std;
bool stack::pop(string &elem){
if (empty()) return false;
elem=_stack.back();
_stack.pop_back();
return true;
}
bool stack::peek(string &elem){
if (empty()) return false;
elem=_stack.back();
return true;
}
bool stack::push(const string &elem){
if (full()) return false;
_stack.push_back(elem);
return true;
}
int main() {
stack st;
string str;
while(cin>>str && ! st.full())
st.push(str);
if(st.empty()){
cout<<"\n"<<"Oops: no string were read--bailing out\n";
return 0;
}
st.peek(str);
if(st.size()==1 && str.empty()){
cout<<"\n"<<"Oops: no string were read--bailing out\n";
return 0;
}
cout<<"\n"<<"Read in "<<st.size()<<" string!\n"
<<"The strings,in reverse order:\n";
while(st.size())
if(st.pop(str))
cout<<str<<" ";
cout<<"\n"<<"There are now "<<st.size()
<<"elements in the stack!\n";
}
编译:
eleting intermediate files and output files for project 'stack - Win32 Debug'.
--------------------Configuration: stack - Win32 Debug--------------------
Compiling...
stack.cpp
c:\program files\microsoft visual studio\vc98\include\iostream(14) : error C2143: syntax error : missing ';' before 'namespace'
Error executing cl.exe.
stack.exe - 1 error(s), 0 warning(s)
这是essential c++练习4.1,却报iostream.h错
问题点数:0、回复次数:1Top
1 楼jumpbob(波波)回复于 2003-09-02 22:13:17 得分 0
我的问题呢?Top




