我已经查过这个,我发现最接近的是这个,除了我没有任何前向声明。我在子类中实现的基类中只有一个纯虚函数,如下所示:
命令.h
#ifndef _COMMAND_H_
#define _COMMAND_H_
#include <string>
#include "Stack.h"
#include "Number.h"
class Command
{
public:
std::string cmdType;
Command(void);
Command (std::string cmdType);
virtual void executeCommand(Stack<Number> & stack) = 0;
~Command (void);
};
#endif // !defined _COMMAND_H_
命令.cpp
Command::Command(void)
:cmdType("")
{}
Command::Command(std::string cmdType)
:cmdType(cmdType)
{}
Command::~Command(void)
{}
编号.h
#ifndef _NUMBER_H_
#define _NUMBER_H_
#include "Command.h"
#include "Stack.h"
class Number : public Command
{
public:
Number (float num);
void executeCommand(Stack<Number> & stack);
float val;
~Number (void);
};
#endif // !defined _NUMBER_H_
数字.cpp
#include "Number.h"
Number::Number(float num)
:val(num)
{
cmdType = "hi";
}
void Number::executeCommand(Stack<Number> & stack)
{
stack.push((*this));
}
出现文件错误:
Error 4 error C2259: 'Number' : cannot instantiate abstract class c:\...\add.cpp 34
添加.cpp
#include "Add.h"
Add::Add(void)
:Binary("+")
{
}
Add::~Add(void)
{
}
void Add::executeCommand(Stack<Number> & numStack)
{
Number num1 = numStack.top(); //THIS LINE HAS THE ERROR
numStack.pop();
Number num2 = numStack.top();
numStack.pop();
float tempVal = num2.val + num1.val;
num1.val = tempVal;
numStack.push(num1);
}
添加.h
#ifndef _ADD_H_
#define _ADD_H_
#include "Stack.h"
#include "Number.h"
#include "Binary.h"
class Add : public Binary
{
public:
Add (void);
void executeCommand (Stack<Number> & numStack);
~Add (void);
};
#endif // !defined _ADD_H_