奇怪的小错误
Curious little error
本文关键字:错误 更新时间:2023-10-16
这是我对错误的处理:
In file included from braincalc.cpp:8:0:
AbstractStack.h:43:1: error: expected class-name before â{â token
这是我的.h:
//AbstractStack.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>
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 错误:未在此范围内声明'reverse'