C++ 错误:无效使用不完整的类型';类堆栈<;int>';[默认情况下启用]

C++ 错误:无效使用不完整的类型';类堆栈<;int>';[默认情况下启用],c++,templates,mingw,C++,Templates,Mingw,我一直在尝试为堆栈准备一个类模板和一个简单的程序来运行它,并确保它能正常工作。当我编译(使用MinGW)时,我一直会遇到这个错误,我不确定我在这里做错了什么。先谢谢你 错误: In file included from Stacktemp_Driver.cpp:8:0: stacktemp.h: In instantiation of 'class Stack<int>': Stacktemp_Driver.cpp:23:14: required from here stackt

我一直在尝试为堆栈准备一个类模板和一个简单的程序来运行它,并确保它能正常工作。当我编译(使用MinGW)时,我一直会遇到这个错误,我不确定我在这里做错了什么。先谢谢你

错误:

In file included from Stacktemp_Driver.cpp:8:0:
stacktemp.h: In instantiation of 'class Stack<int>':
Stacktemp_Driver.cpp:23:14:   required from here
stacktemp.h:123:8: warning: invalid use of incomplete type 'class Stack<int>' [e
nabled by default]
stacktemp.h:16:7: warning: declaration of 'class Stack<int>' [enabled by default
]
stacktemp.h:153:6: warning: invalid use of incomplete type 'class Stack<int>' [e
nabled by default]
stacktemp.h:16:7: warning: declaration of 'class Stack<int>' [enabled by default
]
Stacktemp_Driver.cpp中包含的文件中的
:8:0:
stacktemp.h:在“类堆栈”的实例化中:
Stacktemp_驱动程序。cpp:23:14:此处需要
stacktemp.h:123:8:警告:不完整类型“类堆栈”的使用无效[e]
默认情况下被逮捕]
stacktemp.h:16:7:警告:声明“类堆栈”[默认启用]
]
stacktemp.h:153:6:警告:不完整类型“类堆栈”的使用无效[e]
默认情况下被逮捕]
stacktemp.h:16:7:警告:声明“类堆栈”[默认启用]
]
这是我的stacktemp.h代码:

#ifndef STACKTEMP_H
#define STACKTEMP_H
#include <stdlib.h>
#include <string>
#include <iostream>
#include <stdexcept>

template <class Type>
class Stack
{
 public:
  Stack();              //default constructor
  Stack(const Stack<Type> & a);//copy constructor
  ~Stack();                          //destructor
  Stack & operator=(Stack<Type> & a);//assignment operator
  bool operator!=(Stack<Type> & a);         //is not equal operator
  bool operator==(Stack<Type> & a);         //is equal operator
  Type & operator[](int subscript);         //subscript operator
  bool isfull() const;                      //determine if stack is full
  bool isempty() const;                     //determine if stack is empty
  bool push(Type item) throw(Stack*);      //add to top of the stack
  void outerpush() throw(std::overflow_error);
  bool pop(Type item) throw(Stack*);       //remove top of the stack
  void outerpop() throw(std::underflow_error);
  int capacity();                          //returns capacity of the stack
  int size();                              //returns size of the stack
  void print_stack();       //print the contents of the stack

 private:
  Type * data;                          
  int stack_size;                          //capacity of stack
  int tos;                                 //top of stack indicator
};


//constructors and destructor
template <class Type>
Stack<Type>::Stack()       //default constructor
{
  stack_size = 10;
  data = new Type[stack_size];
  tos = 0;
}

template <class Type>
Stack<Type>::Stack(const Stack<Type> & a)     //copy constructor
{
  stack_size = a.stack_size;
  data = new Type[stack_size];
  for (int i = 0; i < stack_size; i++)
    data[i] = a.data[i];
  tos = a.tos;
}

template <class Type>
Stack<Type>::~Stack()
{
  delete [] data;
}

//overloaded operators
template <class Type>
Stack<Type> & Stack<Type>::operator=(Stack<Type> & a)
{
  if (this == &a)
    return *this;
  delete [] data;
  stack_size = a.stack_size;
  data = new Type[stack_size];
  for (int i = 0; i < stack_size; i++)
    data[i] = a.data[i];
  tos = a.tos;
  return *this;
}

template <class Type>
Type & Stack<Type>::operator[](int subscript)
{
  if (subscript < 0 || subscript > (stack_size - 1))
    {std::cerr << "*** Out of range subscript = " << subscript << std::endl;
      exit(1);
    }
  return data[subscript];
}

template <class Type>
bool Stack<Type>::operator==(Stack<Type> & a)
{
  if (stack_size != a.stack_size)
    return false;
  for (int i = 0; i < stack_size; i++)
    {
      if (data[i] != a.data[i])
    return false;
    }
  return true;
}

//template class methods
template <class Type>
bool Stack<Type>::isfull() const
{
  return tos == stack_size;
}

template <class Type>
bool Stack<Type>::isempty() const
{
  if (tos == 0)
    return true;
  else
    return false;
}

template <class Type>
  bool Stack<Type>::push(Type item) throw(Stack*)
{
  if (isfull())
    throw this;
  data[tos] = item;
  tos = tos + 1;
  return true;
}

template <class Type>
void Stack<Type>::outerpush() throw(std::overflow_error)
{
  using std::cout;
  using std::cin;
  using std::endl;

  cout << "Please enter a value to place on top of stack: " << endl;
  Type item;
  cin >> item;
  try {
    this->push(item);
  }
  catch(Stack * dsx)
     {
      cout << "Error in ";
       throw std::overflow_error("Overflow error");
     }
}

template <class Type>
bool Stack<Type>::pop(Type item) throw(Stack*)
{
  if (isempty())
   throw this;
  tos = tos - 1;
   item = data[tos];
  return true;
}

template <class Type>
void Stack<Type>::outerpop() throw(std::underflow_error)
{
  using std::cout;
  using std::cin;
  using std::endl;

  Type item;
  try {
    this->pop(item);
  }
  catch(Stack * dsx)
  {
    cout << "Error in ";
    throw std::underflow_error("Underflow error");
  }
}

template <class Type>
int Stack<Type>::capacity()
{
  return stack_size;
}

template <class Type>
int Stack<Type>::size()
{
  return tos;
}

template <class Type>
void Stack<Type>::print_stack()
{
  for (int i = 0; i < tos; i++)
    std::cout << "a[" << i << "] = " << data[i] << std::endl;
}

#endif
\ifndef STACKTEMP\u H
#定义堆栈温度
#包括
#包括
#包括
#包括
模板
类堆栈
{
公众:
Stack();//默认构造函数
堆栈(const Stack&a);//复制构造函数
~Stack();//析构函数
堆栈和运算符=(堆栈和a);//赋值运算符
布尔运算符!=(堆栈&a);//不是相等运算符
布尔运算符==(堆栈&a);//是相等运算符
类型和运算符[](int下标);//下标运算符
bool isfull()const;//确定堆栈是否已满
bool isempty()const;//确定堆栈是否为空
bool push(Type item)throw(Stack*);//添加到堆栈顶部
void outerpush()抛出(std::overflow\u错误);
bool pop(Type item)throw(Stack*);//移除堆栈顶部
void outerpop()抛出(标准::下溢错误);
int capacity();//返回堆栈的容量
int size();//返回堆栈的大小
void print_stack();//打印堆栈的内容
私人:
类型*数据;
int stack_size;//堆栈的容量
int tos;//堆栈顶部指示器
};
//构造函数和析构函数
模板
Stack::Stack()//默认构造函数
{
堆栈大小=10;
数据=新类型[堆栈大小];
tos=0;
}
模板
Stack::Stack(const Stack&a)//复制构造函数
{
堆栈大小=a.堆栈大小;
数据=新类型[堆栈大小];
对于(int i=0;i(堆栈大小-1))

{std::cerr正如编译器告诉您的,问题在于下面几行:

bool push(Type item) throw(Stack*);
bool pop(Type item) throw(Stack*);
此时堆栈实际上是不完整的类型

问题是为什么要抛出指向类的指针?理想情况下,抛出类型继承自
std::exception
,或者可以提供更多信息的内容(如中所述)

另一方面


顺便说一句,这里有一个显示问题的最小示例:

struct A
{
    void foo() throw( A* )
    {
        throw this;
    }
};

void menu()
{
    A a;
    a.foo();
}

我想您错过了“!=”的实现。您能标记代码中的行吗?您抛出的
堆栈*
看起来像是编译器可能会抱怨的东西
struct A
{
    void foo() throw( A* )
    {
        throw this;
    }
};

void menu()
{
    A a;
    a.foo();
}