所以我已经有了这个RPN计算器项目,我会尝试发布它的所有代码;我会运行" C ++ dc.cpp"?即使我知道其他人已正确编译它,我也会收到错误。我该如何编译呢?
dc.cpp
#include <iostream>
#include "stack.h"
#include <string>
#include "dsexceptions.h"
using namespace std;
bool IsOperator(char op);
int calculate(char op, int operand1, int operand2);
int main() {
string input;
Stack<int> dc;
while (getline(cin, input))
{
for (int i = 0; i<input.length(); i++)
{
if (input[i] == ' ')
continue;
else if (IsOperator(input[i]))
{
try {
int operand2 = dc.top();
dc.pop();
int operand1 = dc.top();
dc.pop();
int result = calculate(input[i], operand1, operand2);
dc.push(result);
}
catch (Underflow e) {
cout << "No elements in stack";
}
catch (Overflow e) {
cout << "Stack full. Can't insert more";
}
catch (DivisionByZero e) {
cout << "Please choose some other value for division except 0";
}
catch (InvalidOperator e) {
cout << "The operator you choose is invalid";
}
}
else if (isdigit(input[i]))
{
int operand = 0;
while (i<input.length() && isdigit(input[i]))
{
operand = (operand * 10) + (input[i] - '0');
i++;
}
i--;
if (i && input[i - 1] == '_')
operand *= -1;
try {
dc.push(operand);
}
catch (Overflow e) {
cout << "Stack full. Can't insert more";
}
}
else if (input[i] == 'p') {
try {
cout << dc.top() << endl;
}
catch (Underflow e) {
cout << "No elements in stack";
}
}
else if (input[i] == 'n') {
try {
cout << dc.top();
}
catch (Underflow e) {
cout << "No elements in stack";
}
}
else if (input[i] == 'f') {
for (Stack <int> dump = dc; !dump.isEmpty(); dump.pop()) {
try {
cout << dump.top() << " ";
}
catch (Underflow e) {
cout << "No elements in stack";
}
}
cout << endl;
}
else if (input[i] == 'c') {
while (!dc.isEmpty())
dc.pop();
}
else if (input[i] == 'd') {
try {
dc.push(dc.top());
}
catch (Overflow e) {
cout << "Stack full. Can't insert more";
}
catch (Underflow e) {
cout << "No elements in stack";
}
}
else if (input[i] == 'r') {
try {
int x = dc.top();
dc.pop();
int y = dc.top();
dc.pop();
dc.push(x);
dc.push(y);
}
catch (Overflow e) {
cout << "Stack full. Can't insert more";
}
catch (Underflow e) {
cout << "No elements in stack";
}
}
}
}
return 0;
}
bool IsOperator(char op)
{
if (op == '+' || op == '-' || op == '*' || op == '/' || op == '%')
{
return true;
}
else
{
return false;
}
}
int calculate(char op, int operand1, int operand2)
{
if (op == '+')
{
return operand1 + operand2;
}
else if (op == '-')
{
return operand1 - operand2;
}
else if (op == '*')
{
return operand1 * operand2;
}
else if (op == '/')
{
return operand1 / operand2;
if (operand2 == 0)
{
throw DivisionByZero();
}
}
else if (op == '%')
{
return operand1 % operand2;
}
else {
throw InvalidOperator();
}
}
dsexceptions.h
#ifndef _DSEXCEPTIONS_H_
#define _DSEXCEPTIONS_H_
#include <iostream>
class Underflow : public std::runtime_error
{
public:
Underflow(std::string const& error)
: std::runtime_error(error) {}
Underflow()
:std::runtime_error("Underflow Exception") {}
};
class Overflow : public std::runtime_error
{
public:
Overflow(std::string const& error)
: std::runtime_error(error) {}
Overflow()
:std::runtime_error("Overflow Exception") {}
};
class InvalidOperator : public std::runtime_error
{
public:
InvalidOperator(std::string const& error)
: std::runtime_error(error) {}
InvalidOperator()
:std::runtime_error("Invalid Exception") {}
};
class OutOfMemory : public std::runtime_error
{
public:
OutOfMemory(std::string const& error)
: std::runtime_error(error) {}
OutOfMemory()
:std::runtime_error("Out Of Memory Exception") {}
};
class BadIterator : public std::runtime_error
{
public:
BadIterator(std::string const& error)
: std::runtime_error(error) {}
BadIterator()
:std::runtime_error("Bad Iterator Exception") {}
};
class DataError : public std::runtime_error
{
public:
DataError(std::string const& error)
: std::runtime_error(error) {}
DataError()
:std::runtime_error("Data Error Exception") {}
};
class DivisionByZero : public std::runtime_error
{
public:
DivisionByZero(std::string const& error)
: std::runtime_error(error) {}
DivisionByZero()
:std::runtime_error("Division By Zero Exception") {}
};
#endif
stack.cpp
#include "dsexceptions.h"
#include "stack.h"
/**
* Construct the stack.
*/
template <class Object>
Stack<Object>::Stack(int aCapacity)
{
topOfStack = -1;
capacity = aCapacity;
theArray.resize(aCapacity);
}
/**
* Test if the stack is logically empty.
* Return true if empty, false otherwise.
*/
template <class Object>
bool Stack<Object>::isEmpty() const
{
return topOfStack == -1;
}
/**
* Test if the stack is logically full.
* Return true if full, false otherwise.
*/
template <class Object>
bool Stack<Object>::isFull() const
{
return topOfStack == capacity - 1;
}
/**
* Make the stack logically empty.
*/
template <class Object>
void Stack<Object>::makeEmpty()
{
topOfStack = -1;
}
/**
* Get the most recently inserted item in the stack.
* Does not alter the stack.
* Return the most recently inserted item in the stack.
* Exception Underflow if stack is already empty.
*/
template <class Object>
const Object & Stack<Object>::top() const
{
if (isEmpty())
throw Underflow();
return theArray[topOfStack];
}
/**
* Remove the most recently inserted item from the stack.
* Exception Underflow if stack is already empty.
*/
template <class Object>
void Stack<Object>::pop()
{
if (isEmpty())
throw Underflow();
topOfStack--;
}
/**
* Insert x into the stack, if not already full.
* Exception Overflow if stack is already full.
*/
template <class Object>
void Stack<Object>::push(const Object & x)
{
if (isFull())
throw Overflow();
theArray[++topOfStack] = x;
}
/**
* Return and remove most recently inserted item from the stack.
* Return most recently inserted item.
* Exception Underflow if stack is already empty.
*/
template <class Object>
Object Stack<Object>::topAndPop()
{
if (isEmpty())
throw Underflow();
return theArray[topOfStack--];
}
/* int main()
{
cout << "testing stack class" << endl;
Stack<int> dc(100);
cout << " stack class init" << endl;
dc.push(10);
dc.pop();
cout << "done testing stack class" << endl;
}*/
当我运行&#34; c ++ dc.cpp&#34;时,我的编译器会返回这些错误。我不得不错误地编译它,对吧?:
In file included from stack.cpp:1:
dsexceptions.h:8: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘Underflow::Underflow(const std::string&)’:
dsexceptions.h:11: error: expected class-name before ‘(’ token
dsexceptions.h:11: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘Underflow::Underflow()’:
dsexceptions.h:14: error: expected class-name before ‘(’ token
dsexceptions.h:14: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:18: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘Overflow::Overflow(const std::string&)’:
dsexceptions.h:22: error: expected class-name before ‘(’ token
dsexceptions.h:22: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘Overflow::Overflow()’:
dsexceptions.h:25: error: expected class-name before ‘(’ token
dsexceptions.h:25: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:29: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘InvalidOperator::InvalidOperator(const std::string&)’:
dsexceptions.h:33: error: expected class-name before ‘(’ token
dsexceptions.h:33: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘InvalidOperator::InvalidOperator()’:
dsexceptions.h:36: error: expected class-name before ‘(’ token
dsexceptions.h:36: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:42: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘OutOfMemory::OutOfMemory(const std::string&)’:
dsexceptions.h:46: error: expected class-name before ‘(’ token
dsexceptions.h:46: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘OutOfMemory::OutOfMemory()’:
dsexceptions.h:49: error: expected class-name before ‘(’ token
dsexceptions.h:49: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:53: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘BadIterator::BadIterator(const std::string&)’:
dsexceptions.h:57: error: expected class-name before ‘(’ token
dsexceptions.h:57: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘BadIterator::BadIterator()’:
dsexceptions.h:60: error: expected class-name before ‘(’ token
dsexceptions.h:60: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:64: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘DataError::DataError(const std::string&)’:
dsexceptions.h:68: error: expected class-name before ‘(’ token
dsexceptions.h:68: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘DataError::DataError()’:
dsexceptions.h:71: error: expected class-name before ‘(’ token
dsexceptions.h:71: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:75: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘DivisionByZero::DivisionByZero(const std::string&)’:
dsexceptions.h:79: error: expected class-name before ‘(’ token
dsexceptions.h:79: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘DivisionByZero::DivisionByZero()’:
dsexceptions.h:82: error: expected class-name before ‘(’ token
dsexceptions.h:82: error: expected ‘{’ before ‘(’ token
In file included from stack.cpp:2:
stack.h: At global scope:
stack.h:1: error: expected constructor, destructor, or type conversion before ‘<’ token
stack.h:34: error: ISO C++ forbids declaration of ‘vector’ with no type
stack.h:34: error: expected ‘;’ before ‘<’ token
stack.h:36: error: ISO C++ forbids declaration of ‘vector’ with no type
stack.h:36: error: expected ‘;’ before ‘<’ token
stack.cpp: In constructor ‘Stack<Object>::Stack(int)’:
stack.cpp:11: error: ‘theArray’ was not declared in this scope
stack.cpp: In member function ‘const Object& Stack<Object>::top() const’:
stack.cpp:54: error: ‘theArray’ was not declared in this scope
stack.cpp: In member function ‘void Stack<Object>::push(const Object&)’:
stack.cpp:78: error: ‘theArray’ was not declared in this scope
stack.cpp: In member function ‘Object Stack<Object>::topAndPop()’:
stack.cpp:91: error: ‘theArray’ was not declared in this scope
[jones_g@cobra Prog2]$ c++ dc.cpp
In file included from dc.cpp:2:
stack.h:1: error: expected constructor, destructor, or type conversion before ‘<’ token
stack.h:34: error: ISO C++ forbids declaration of ‘vector’ with no type
stack.h:34: error: expected ‘;’ before ‘<’ token
stack.h:36: error: ISO C++ forbids declaration of ‘vector’ with no type
stack.h:36: error: expected ‘;’ before ‘<’ token
In file included from dc.cpp:4:
dsexceptions.h:8: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘Underflow::Underflow(const std::string&)’:
dsexceptions.h:11: error: expected class-name before ‘(’ token
dsexceptions.h:11: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘Underflow::Underflow()’:
dsexceptions.h:14: error: expected class-name before ‘(’ token
dsexceptions.h:14: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:18: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘Overflow::Overflow(const std::string&)’:
dsexceptions.h:22: error: expected class-name before ‘(’ token
dsexceptions.h:22: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘Overflow::Overflow()’:
dsexceptions.h:25: error: expected class-name before ‘(’ token
dsexceptions.h:25: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:29: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘InvalidOperator::InvalidOperator(const std::string&)’:
dsexceptions.h:33: error: expected class-name before ‘(’ token
dsexceptions.h:33: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘InvalidOperator::InvalidOperator()’:
dsexceptions.h:36: error: expected class-name before ‘(’ token
dsexceptions.h:36: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:42: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘OutOfMemory::OutOfMemory(const std::string&)’:
dsexceptions.h:46: error: expected class-name before ‘(’ token
dsexceptions.h:46: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘OutOfMemory::OutOfMemory()’:
dsexceptions.h:49: error: expected class-name before ‘(’ token
dsexceptions.h:49: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:53: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘BadIterator::BadIterator(const std::string&)’:
dsexceptions.h:57: error: expected class-name before ‘(’ token
dsexceptions.h:57: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘BadIterator::BadIterator()’:
dsexceptions.h:60: error: expected class-name before ‘(’ token
dsexceptions.h:60: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:64: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘DataError::DataError(const std::string&)’:
dsexceptions.h:68: error: expected class-name before ‘(’ token
dsexceptions.h:68: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘DataError::DataError()’:
dsexceptions.h:71: error: expected class-name before ‘(’ token
dsexceptions.h:71: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:75: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘DivisionByZero::DivisionByZero(const std::string&)’:
dsexceptions.h:79: error: expected class-name before ‘(’ token
dsexceptions.h:79: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘DivisionByZero::DivisionByZero()’:
dsexceptions.h:82: error: expected class-name before ‘(’ token
dsexceptions.h:82: error: expected ‘{’ before ‘(’ token
[jones_g@cobra Prog2]$ c++ dc.cpp
In file included from dc.cpp:2:
stack.h:1: error: expected constructor, destructor, or type conversion before ‘<’ token
stack.h:34: error: ISO C++ forbids declaration of ‘vector’ with no type
stack.h:34: error: expected ‘;’ before ‘<’ token
stack.h:36: error: ISO C++ forbids declaration of ‘vector’ with no type
stack.h:36: error: expected ‘;’ before ‘<’ token
In file included from dc.cpp:4:
dsexceptions.h:8: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘Underflow::Underflow(const std::string&)’:
dsexceptions.h:11: error: expected class-name before ‘(’ token
dsexceptions.h:11: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘Underflow::Underflow()’:
dsexceptions.h:14: error: expected class-name before ‘(’ token
dsexceptions.h:14: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:18: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘Overflow::Overflow(const std::string&)’:
dsexceptions.h:22: error: expected class-name before ‘(’ token
dsexceptions.h:22: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘Overflow::Overflow()’:
dsexceptions.h:25: error: expected class-name before ‘(’ token
dsexceptions.h:25: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:29: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘InvalidOperator::InvalidOperator(const std::string&)’:
dsexceptions.h:33: error: expected class-name before ‘(’ token
dsexceptions.h:33: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘InvalidOperator::InvalidOperator()’:
dsexceptions.h:36: error: expected class-name before ‘(’ token
dsexceptions.h:36: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:42: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘OutOfMemory::OutOfMemory(const std::string&)’:
dsexceptions.h:46: error: expected class-name before ‘(’ token
dsexceptions.h:46: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘OutOfMemory::OutOfMemory()’:
dsexceptions.h:49: error: expected class-name before ‘(’ token
dsexceptions.h:49: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:53: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘BadIterator::BadIterator(const std::string&)’:
dsexceptions.h:57: error: expected class-name before ‘(’ token
dsexceptions.h:57: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘BadIterator::BadIterator()’:
dsexceptions.h:60: error: expected class-name before ‘(’ token
dsexceptions.h:60: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:64: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘DataError::DataError(const std::string&)’:
dsexceptions.h:68: error: expected class-name before ‘(’ token
dsexceptions.h:68: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘DataError::DataError()’:
dsexceptions.h:71: error: expected class-name before ‘(’ token
dsexceptions.h:71: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:75: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘DivisionByZero::DivisionByZero(const std::string&)’:
dsexceptions.h:79: error: expected class-name before ‘(’ token
dsexceptions.h:79: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘DivisionByZero::DivisionByZero()’:
dsexceptions.h:82: error: expected class-name before ‘(’ token
dsexceptions.h:82: error: expected ‘{’ before ‘(’ token
答案 0 :(得分:1)
看起来你正在编译它,但你的代码有错误,编译器会告诉你。
至少,添加......
#include <stdexcept>
...到你的dsexceptions.h文件。
您可能还需要添加...
#include <vector>
...到你的stack.h文件。
答案 1 :(得分:0)
您需要更仔细地阅读错误消息。只收到第一条消息:
dsexceptions.h:8: error: expected class-name before ‘{’ token
您可以看到编译器无法识别{
之前的令牌。那个象征是什么?第7行是
class Underflow : public std::runtime_error
是的,您使用的是std::runtime_error
,但尚未对其进行定义。当然,修复方法是#include <stdexcept>
,而不是不必要的#include <iostream>
。
在标题中,您通常应该包含标题所需的任何定义(并且只包含标题所需的内容)。如果不这样做,那么如果使用它们的代码不知道先决条件,那么您可能最终会遇到非常脆弱的代码,如上所述标题失败。可以多次包含标准库头文件,任何编写良好的库头文件都是如此。
有一些工具,例如include-what-you-use
(又名iwyu
)来帮助分析您的源文件是否包含丢失或不必要的内容 - 您当然应该查看这是否对您有帮助。