错误信息:
Windows 已在 calc.exe 中触发一个断点。
其原因可能是堆被损坏,这说明 calc.exe 中或它所加载的任何 DLL 中有 Bug。
原因也可能是用户在 calc.exe 具有焦点时按下了 F12。
输出窗口可能提供了更多诊断信息。
源代码:
//Calc.h
#include <iostream>
#include "Stack.cpp"
using namespace std;
extern enum Status;
#ifndef CALC_H_
#define CALC_H_
class Calc
{
private:
char * const line;
Stack<double> * const numstack;
Stack<char> * const oprstack;
double result;
int in(char);
int out(char);
void count(double,char,double);
bool make(void);
public:
Calc(const char * = "1+1",const int = strlen("1+1"));
Calc(const Calc &);
~Calc();
double Result(void)const;
friend ostream & operator << (ostream &,const Calc &);
};
#endif
//Calc.cpp
#include "Calc.h"
Calc::Calc(const char * l,const int n) : numstack (new Stack<double>(50)),oprstack (new Stack<char>(50)),line (new char[n + 1])
{
strncpy(line,l,strlen(line));
line[n] = '\0';
make();
}
Calc::Calc(const Calc & cl) : numstack (new Stack<double>(50)),oprstack (new Stack<char>(50)),line (new char[strlen(cl.line) + 1])
{
strncpy(line,cl.line,strlen(line));
this->numstack->Clear();
oprstack->Clear();
make();
}
Calc::~Calc()
{
delete[] line;
delete[] numstack;
delete[] oprstack;
}
bool Calc::make(void){...}
int Calc::in(char e){...}
int Calc::out(char e){...}
void Calc::count(double a,char opr,double b){...}
double Calc::Result(void)const{...}
//Stack.h
#include <iostream>
using namespace std;
#ifndef STACK_H_
#define STACK_H_
extern enum Status
{
success = 0,
overflow,
underflow
};
template<typename Item>
class Stack
{
private:
Item * stack;
const int size;
int top;
public:
//constructors and other methods
Stack(int s = 10);
Stack(const Stack &);
~Stack();
//functions
inline int ItemCount()const{return top + 1;}
inline bool IsEmpty()const{return (top <= -1);}
inline bool IsFull()const{return (top >= size);}
inline void Clear(){top = -1;}
Status Push(const Item & );
Status Pop(Item &);
Status GetTop(Item &);
};
#endif
//Stack.cpp
#include "Stack.h"
template<typename Item>
Stack<Item>::Stack(int s = 10) : size(s)
{
top = -1;
stack = new Item [size];
}
template<typename Item>
Stack<Item>::Stack(const Stack<Item> & stk):size(stk.size)
{
int i = 0;
if(this->stack != NULL)
delete[] this->stack;
this->stack = new Item [size];
this->top = stk.top;
while (i < this->top + 1)
{
this->stack[i] = stk.stack[i];
}
}
template<typename Item>
Stack<Item>::~Stack()
{
delete[] stack;
}
template<typename Item>
Status Stack<Item>::Push (const Item & im){...}
template<typename Item>
Status Stack<Item>::Pop (Item & im){...}
template<typename Item>
Status Stack<Item>::GetTop (Item & im)
//main.cpp
#include "Calc.h"
int main(void)
{
Calc cl("1+1+1+1+1+1+1",strlen("1+1+1+1+1+1+1"));
cout<<cl.Result();
}
我的问题是解决了,不过我发现
delete[] numstack;
delete[] oprstack;
这里也不应该有[],而且,如果在Calc.h中包含Stack.h,则出现链接错误,我查了一下,网上说模板类的实现要包含.cpp才可以。是这样么?
对的。析构函数应该为
Calc::~Calc()
{
delete []line;
delete numstack;
delete oprstack;
}
链接问题:见上面
3.我不知道你用的是什么编译器,为何你将Stack的实现与定义分开编译器能通过,事实上,c++标准是模板的实现必须在定义是可见。所以即使你的编译器允许,我也希望你遵循标准,把Stack实现放到头文件。
i think it is right.when i debug this program,i found there was an problem in Calc::~Calc().
"delete[] line".
i am a freshman major in software.can you speak chinese?
I can't type chinese right now,only english and it's not easy for communication.
I am sorry
oh,do u have qq?i think i can improve my english by communicate with u.my qq:30798124.
本回答被网友采纳