C ++ Decorator - 不能正常工作

时间:2014-11-24 05:13:18

标签: c++ decorator

第一个问题已解决

正如下面的答案所说,我错误地写了

#ifndef ICETOWER_H
#define ICETOWER_H

我的c ++代码仍有问题。

我实施了装饰图案,将基本塔升级为冰塔,成本为2 *。但是当我运行它时,即使它已被装饰,它也会显示两个塔的相同规格。任何人都知道我做错了什么?

以下是文件:

tower.h

#ifndef __TOWER_H__
#define __TOWER_H__

#include <iostream>
#include <string>
using namespace std;

class Tower {

private:

    string type;
    string effect;
    int cost;
    int sellTower;
    int damage;
    int range;
    int rate;

public:

    string getType() { return type; }
    string getEffect() { return effect; }

    int getCost() { return cost; }
    int getSale() { return sellTower; }
    int getDamage() { return damage; }
    int getRange() { return range; }
    int getROF() { return rate; }

    Tower();
    virtual ~Tower(){}
};

#endif __TOWER_H__

tower.cpp

#include "Tower.h"

Tower::Tower()
{
    // Tower Type
    this->type = "Basic";

    // Tower Special Effect
    this->effect = "None";

    // Tower Cost
    this->cost = 500;

    // Tower Sell Cost
    this->sellTower = 300;

    // Tower Damage inflicted
    this->damage = 50;

    // Tower Range (paths)
    this->range = 2;

    // Tower rate of fire
    this->rate = 0.5;
};

TowerDecorator.h

#ifndef __TOWERDECORATOR_H__
#define __TOWERDECORATOR_H__
#include <iostream>
#include <string>
#include "Tower.h"
using namespace std;

class TowerDecorator : public Tower {

private:

    Tower *decoratedTower;

public:

    TowerDecorator(Tower *decoratedTower)
    {
        this->decoratedTower = decoratedTower;
    }

    string type() { return decoratedTower->getType(); }
    string effect() { return decoratedTower->getEffect(); }

    int getCost() { return decoratedTower->getCost(); }
    int getSale() { return decoratedTower->getSale(); }
    int getDamage() { return decoratedTower->getDamage(); }
    int getRange() { return decoratedTower->getRange(); }
    int getROF() { return decoratedTower->getROF(); }

};

#endif __TOWERDECORATOR_H__

iceTower.h

#ifndef ICETOWER_H
#define ICETOWER_H

#include "TowerDecorator.h"

class IceTower : public TowerDecorator {

public:
    IceTower(Tower *decoratedTower) : TowerDecorator (decoratedTower){}

    int getCost(){return TowerDecorator::getCost() * 2;}
};

#endif __ICETOWER_H__

Driver.cpp

#include "Tower.h"
#include "TowerDecorator.h"
#include "IceTower.h"

void printTowerDetails(Tower* tower)
{
    cout << endl << "This is a " << tower->getType() << " Tower" << endl;
    cout << "Build Tower : " << tower->getCost() << " Coins" << endl;
    cout << "Sell Tower : " << tower->getSale() << " Coins" << endl;
    cout << "Tower Range : " << tower->getRange() << " paths" << endl;
    cout << "Tower Rate Of Fire : " << tower->getROF() << " p/s" << endl;
    cout << "Tower Special Effect : " << tower->getEffect() << "" << endl;
}

int main() {

    Tower *t1 = new Tower();

    printTowerDetails(t1);

    t1 = new IceTower(t1);

    printTowerDetails(t1);


}

输出

图片:http://i.imgur.com/Ws018iV.png

error

3 个答案:

答案 0 :(得分:0)

嗯,你遇到的第一个问题是你的包含警卫问题。它应该是:

#ifndef __TOWER_H__
#define __TOWER_H__

您可能还有其他问题。

顺便说一句,你不应该使用以两个下划线开头的名字,因为它们是保留的。

答案 1 :(得分:0)

你的标题保护不一致: -

#ifndef __TOWER_H__
#define __TOWER__

这应该是

#ifndef __TOWER_H__
#define __TOWER_H__

其次改变这个

#endif __TOWER_H__

#endif

答案 2 :(得分:0)

至少有两个严重的错误。 第一个是检查带有后缀_H__的清单常量的定义,但是没有后缀来定义它们:

#ifndef __TOWER_H__
#define __TOWER__

#ifndef __TOWERDECORATOR_H__
#define __TOWERDECORATOR__

第二个是你忘了在TowerDecorator的课程定义后放置分号

class TowerDecorator : public Tower {
//...
}