VS2005下编译 essential第四章中例子遇到的问题
Stack.h文件中如下
#pragma once
class Stack
{
public:
bool push( const string& );
bool pop( string &elem );
bool peek( string &elem );
bool empty();
bool full();
int size() { return _stack.size(); }
Stack(void);
public:
~Stack(void);
private:
vector<string> _stack;
};
void fill_stack( Stack &stack, istream &is = cin )
{
string str;
while ( is >> str && ! stack.full() )
stack.push( str );
cout << "Read in " << stack.size() << " elements\n";
}
inline bool
Stack::empty()
{
return _stack.empty();
}
bool
Stack::pop( string &elem )
{
if ( empty() )
return false;
elem = _stack.back();
_stack.pop_back();
return true;
}
inline bool
Stack::full()
{
return _stack.size() == _stack.max_size();
}
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;
}
然后teststack.cpp文件如下
只是想先简单测试下
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#include "Stack.h"
int main()
{
Stack blue;
fill_stack(blue,cin);
}
}
错误很多,比如
Error1error C4430: missing type specifier - int assumed. Note: C++ does not support default-intVS2005\projects\teststack2\teststack2\stack.h6
还有
Error2error C2143: syntax error : missing ',' before '&'visual studio 2005\projects\teststack2\teststack2\stack.h6
Error3error C2061: syntax error : identifier 'string' C:\my documents\visual studio 2005\projects\teststack2\teststack2\stack.h7
菜鸟请大家帮忙看看。谢谢了。 vs2005
[解决办法]
缺少string头文件,在Stack.h中加入以下内容
#include<string>
using std::string;
class Stack{……};
[解决办法]
1)
Stack.h文件,有很多非inline函数的定义,这不是很好。
例如:
void fill_stack( Stack &stack, istream &is = cin )
{
string str;
while ( is >> str && ! stack.full() )
stack.push( str );
cout << "Read in " << stack.size() << " elements\n";
}
不是inline
2)
int main()
{
Stack blue;
fill_stack(blue,cin);
}//多余的大括号,应该
return 0;
}