c++ - 好奇的小错误

标签 c++ compiler-errors classname

这是我的错误:

In file included from braincalc.cpp:8:0:
AbstractStack.h:43:1: error: expected class-name before â{â token

这是我的.h:

//抽象堆栈.h

#ifndef ABSTRACTSTACK_H
#define ABSTRACTSTACK_H

#include<iostream>
using namespace std;

template < typename T >
class AbstractStack
{
public:

  // Purpose: clears the stack
  // Postconditions: the stack is now empty 
  virtual void clear() = 0;

  // Purpose: push an element into the stack
  // Parameters: x is the value to push into the stack
  // Postconditions: x is now the element at the top of the stack, 
  virtual void push(T x) = 0;


  // Purpose: pop the stack
  // Postconditions: the element formerly at the top of the stack has
  // been removed
  // Panic: if the stack is currently empty, PANIC!
  virtual void pop() = 0;


  // Purpose: looks at the top of the stack
  // Returns: a reference to the element currently on top of the stack
  // Panic: if the stack is currently empty, PANIC!

  virtual T& top() = 0;


  // Purpose: Checks if a stack is empty
  // Returns: 'true' if the stack is empty
  //     'false' otherwise  
  virtual bool isEmpty() = 0;
};

class LinkedStack: public AbstractStack
{
  public:
    int m_data;
    LinkedStack *m_next;

    void clear()
    {
      LinkedStack *p;
      LinkedStack *tmp;

      while(m_next != NULL)
      {
        p = this;
        tmp = p -> m_next;
        delete p;
      }
    } 

    void push(int x)
    {
      LinkedStack *tmp = new LinkedStack;
      tmp -> m_data = m_data;
      tmp -> m_next = m_next;
      m_data = x;
      m_next = tmp;
    }

    void pop()
    {
      LinkedStack *tmp;

      if (m_next != NULL)
      {
        tmp = m_next;
        m_data = tmp -> m_data;
        m_next = tmp -> m_next;
        delete tmp;
      }
    }

    int& top()
    {
        return m_data;
    }

    bool isEmpty()
    {
      bool empty = false;

      if (m_next == NULL)
      {
        empty = true;
      }

      return empty;
    }
};

#endif

这是.cpp:

//脑计算器.cpp

#include"AbstractStack.h"
#include<string>
#include<cstdlib>


int main()
{
  string input;
  bool again=true;
  int ctr=0;
  int temp1, temp2;
  LinkedStack stack;

  do
  {
    getline(cin, input, '$');
    input.c_str();

    if (isdigit(input[ctr]))
    {
      stack.push(atoi(&input[ctr]));
      ctr++;
    }

    else if (isspace(input[ctr]))
    {
      ctr++;
    }

    else if (input[ctr] == '*')
    {
      temp2 = stack.top();
      stack.pop();
      temp1 = stack.top();
      stack.pop();
      stack.push(temp1 * temp2);
      ctr++;
    }

    else if (input[ctr] == '/')
    {
      temp2 = stack.top();
      stack.pop();
      temp1 = stack.top();
      stack.pop();
      stack.push(temp1 / temp2);
      ctr++;
    }

    else if (input[ctr] == '+')
    {
      temp2 = stack.top();
      stack.pop();
      temp1 = stack.top();
      stack.pop();
      stack.push(temp1 + temp2);
      ctr++;
    }

    else if (input[ctr] == '-')
    {
      temp2 = stack.top();
      stack.pop();
      temp1 = stack.top();
      stack.pop();
      stack.push(temp1 - temp2);
      ctr++;
    }

    else if (input[ctr] == '!')
    {
      temp1 = stack.top();
      stack.pop();
      stack.push(-temp1);
      ctr++;
    }

    else if (input[ctr] == '#')
    {
      again = false;
      ctr=0;
    }

  } while(again == true);

  cout << "["<<stack.top()<<"]"<<endl;

  return 0;
}

我意识到代码没有完成,但是这个错误导致我无法测试它是否像我认为的那样工作。提前致谢。

最佳答案

AbstractStack 不是一个类,所以你不能写:

class LinkedStack: public AbstractStack

您要么必须将 LinkedStack 设为模板:

template<class T>
class LinkedStack: public AbstractStack<T>

或派生自 AbstractStack 的特化:

class LinkedStack: public AbstractStack<int>

关于c++ - 好奇的小错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12722560/

相关文章:

c++ - 如何在我的 for 循环宏中包含多个语句?

c++ - 单元测试资源管理类中的私有(private)方法 (C++)

c++ - 打印字节序列

c - 多个VC编译错误

java - 如何在子类对象上调用父类(super class)方法?

C++为什么有类头却有 "Unknown Type"?

java - 创建EAR文件编译并在eclipse中获取编译错误

.net - 为 .Net XmlDocument 实现 GetByClassName

以数字开头的标识符的 Java 命名约定

ruby-on-rails - 以编程方式获取 Rails 4 中的belongs_to 关联的类