首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

VS2008头文件老是报错

2012-09-03 
VS2008头文件总是报错?stack.h头文件如下:template class Elem class Stack {public:// Reinitialize th

VS2008头文件总是报错?
stack.h头文件如下:
template <class Elem> class Stack {
public:
  // Reinitialize the stack. The user is responsible for
  // reclaiming the storage used by the stack elements.
  virtual void clear() = 0;
  // Push an element onto the top of the stack. Return
  // true if successful, false if not (if stack is full).
  virtual bool push(const Elem&) = 0;
  // Remove the element at the top of the stack. Return
  // true if succesful, false if stack is empty.
  // The element removed is returned in the first parameter.
  virtual bool pop(Elem&) = 0; // Pop Elem from top of stack
  // Return in first parameter a copy of the top element.
  // Return true if succesful, false if stack is empty.
  virtual bool topValue(Elem&) const = 0;
  // Return the number of elements in the stack.
  virtual int length() const = 0;
};

_


之后还有一个link.h 和Lstack.h , CPP写了一个后缀表达式求值的程序。为何总式头文件报错?
1>f:\calculator\calculator\stack.h(20) : error C2953: 'Stack' : class template has already been defined
1> f:\calculator\calculator\link.h(31) : see declaration of 'Stack'
1>f:\calculator\calculator\lstack.h(14) : error C2143: syntax error : missing ';' before '<'
1>f:\calculator\calculator\lstack.h(14) : error C2059: syntax error : '<'
1>f:\calculator\calculator\lstack.h(14) : error C2143: syntax error : missing ';' before '{'
1>f:\calculator\calculator\lstack.h(14) : error C2447: '{' : missing function header (old-style formal list?)
1>f:\calculator\calculator\stack.h(20) : error C2953: 'Stack' : class template has already been defined
1> f:\calculator\calculator\link.h(31) : see declaration of 'Stack'
1>f:\calculator\calculator\link.h(12) : error C2953: 'Node' : class template has already been defined
1> f:\calculator\calculator\link.h(12) : see declaration of 'Node'
1>f:\calculator\calculator\link.h(13) : error C2011: 'Error_code' : 'enum' type redefinition
1> f:\calculator\calculator\link.h(13) : see declaration of 'Error_code'
1>f:\calculator\calculator\link.h(31) : error C2953: 'Stack' : class template has already been defined
1> f:\calculator\calculator\link.h(31) : see declaration of 'Stack'
1>f:\calculator\calculator\link.h(39) : error C2244: 'Node<Node_entry>::{ctor}' : unable to match function definition to an existing declaration
1> definition
1> 'Node::Node(void)'
1> existing declarations
1> 'Node<Node_entry>::Node(Node_entry,Node<Node_entry> *)'
1> 'Node<Node_entry>::Node(void)'
1>f:\calculator\calculator\link.h(47) : error C2244: 'Node<Node_entry>::{ctor}' : unable to match function definition to an existing declaration
1> definition
1> 'Node::Node(Node_entry,Node *)'
1> existing declarations
1> 'Node<Node_entry>::Node(Node_entry,Node<Node_entry> *)'
1> 'Node<Node_entry>::Node(void)'
1>f:\calculator\calculator\link.h(54) : error C2244: 'Stack<Stack_entry>::{ctor}' : unable to match function definition to an existing declaration
1> definition
1> 'Stack::Stack(void)'
1> existing declarations
1> 'Stack<Stack_entry>::Stack(const Stack<Stack_entry> &)'
1> 'Stack<Stack_entry>::Stack(void)'
1>f:\calculator\calculator\link.h(63) : error C2244: 'Stack<Stack_entry>::empty' : unable to match function definition to an existing declaration
1> f:\calculator\calculator\link.h(20) : see declaration of 'Stack<Stack_entry>::empty'
1> definition
1> 'bool Stack::empty(void) const'
1> existing declarations


1> 'bool Stack<Stack_entry>::empty(void) const'
1>f:\calculator\calculator\link.h(79) : error C2244: 'Stack<Stack_entry>::push' : unable to match function definition to an existing declaration
1> f:\calculator\calculator\link.h(21) : see declaration of 'Stack<Stack_entry>::push'
1> definition
1> 'Error_code Stack::push(const Stack_entry &)'
1> existing declarations
1> 'Error_code Stack<Stack_entry>::push(const Stack_entry &)'
1>f:\calculator\calculator\link.h(94) : error C2244: 'Stack<Stack_entry>::pop' : unable to match function definition to an existing declaration
1> f:\calculator\calculator\link.h(22) : see declaration of 'Stack<Stack_entry>::pop'
1> definition
1> 'Error_code Stack::pop(void)'
1> existing declarations
1> 'Error_code Stack<Stack_entry>::pop(void)'
1>f:\calculator\calculator\link.h(106) : error C2244: 'Stack<Stack_entry>::top' : unable to match function definition to an existing declaration
1> f:\calculator\calculator\link.h(23) : see declaration of 'Stack<Stack_entry>::top'
1> definition
1> 'Error_code Stack::top(Stack_entry &) const'
1> existing declarations
1> 'Error_code Stack<Stack_entry>::top(Stack_entry &) const'
1>f:\calculator\calculator\link.h(117) : error C2244: 'Stack<Stack_entry>::clear' : unable to match function definition to an existing declaration
1> f:\calculator\calculator\link.h(24) : see declaration of 'Stack<Stack_entry>::clear'
1> definition
1> 'void Stack::clear(void)'
1> existing declarations
1> 'void Stack<Stack_entry>::clear(void)'
1>f:\calculator\calculator\link.h(127) : error C2244: 'Stack<Stack_entry>::~Stack' : unable to match function definition to an existing declaration
1> f:\calculator\calculator\link.h(26) : see declaration of 'Stack<Stack_entry>::~Stack'
1> definition
1> 'Stack::~Stack(void)'
1> existing declarations
1> 'Stack<Stack_entry>::~Stack(void)'
1>f:\calculator\calculator\link.h(146) : error C2244: 'Stack<Stack_entry>::{ctor}' : unable to match function definition to an existing declaration
1> definition
1> 'Stack::Stack(const Stack &)'
1> existing declarations
1> 'Stack<Stack_entry>::Stack(const Stack<Stack_entry> &)'
1> 'Stack<Stack_entry>::Stack(void)'
1>f:\calculator\calculator\link.h(168) : error C2244: 'Stack<Stack_entry>::operator =' : unable to match function definition to an existing declaration
1> f:\calculator\calculator\link.h(28) : see declaration of 'Stack<Stack_entry>::operator ='
1> definition
1> 'void Stack::operator =(const Stack &)'
1> existing declarations
1> 'void Stack<Stack_entry>::operator =(const Stack<Stack_entry> &)'
1>.\Calculator.cpp(8) : fatal error C1083: Cannot open include file: 'iostream.h': No such file or directory
1>生成日志保存在“file://f:\Calculator\Calculator\Debug\BuildLog.htm”


头文件是老师给的啊,怎么还报错?


[解决办法]
给头文件加个
#ifndef STACK_H
#define STACK_H
  
你老师给的内容 

#endif
[解决办法]
// Reinitialize the stack. The user is responsible for
// reclaiming the storage used by the stack elements.
注释已经写清楚了,重定义了stack
也就是别再包含定义了stack的库函数了,就用你老师给的,别包含编译器自带的了,不然就有重定义错误了。
------解决方案--------------------


stack类重定义了

热点排行