桥型C++;代码编译错误 我试图编译一个桥模式C++示例代码,如所提到的

桥型C++;代码编译错误 我试图编译一个桥模式C++示例代码,如所提到的,c++,linux,gcc,bridge,C++,Linux,Gcc,Bridge,但是当我执行g++-o Bridge.C 节目: #include <iostream> #include <iomanip> #include <string> class TimeImp { public: TimeImp(int hr, int min) { hr_ = hr; min_ = min; } virtual void tell() { cout <<

但是当我执行
g++-o Bridge.C

节目:

#include <iostream>
#include <iomanip>
#include <string>

class TimeImp {
  public:
    TimeImp(int hr, int min) {
        hr_ = hr;
        min_ = min;
    }
    virtual void tell() {
        cout << "time is " << setw(2) << setfill(48) << hr_ << min_ << endl; //error
    }
  protected:
    int hr_, min_;
};

class CivilianTimeImp: public TimeImp {
  public:
    CivilianTimeImp(int hr, int min, int pm): TimeImp(hr, min) {
        if (pm)
          strcpy(whichM_, " PM");
        else
          strcpy(whichM_, " AM");
    }

    /* virtual */
    void tell() {
        cout << "time is " << hr_ << ":" << min_ << whichM_ << endl;
    }
  protected:
    char whichM_[4];
};

class ZuluTimeImp: public TimeImp {
  public:
    ZuluTimeImp(int hr, int min, int zone): TimeImp(hr, min) {
        if (zone == 5)
          strcpy(zone_, " Eastern Standard Time");
        else if (zone == 6)
          strcpy(zone_, " Central Standard Time");
    }

    /* virtual */
    void tell() {
        cout << "time is " << setw(2) << setfill(48) << hr_ << min_ << zone_ << endl; //error
    }
  protected:
    char zone_[30];
};

class Time {
  public:
    Time(){}
    Time(int hr, int min) {
        imp_ = new TimeImp(hr, min);
    }
    virtual void tell() {
        imp_->tell();
    }
  protected:
    TimeImp *imp_;
};

class CivilianTime: public Time {
  public:
    CivilianTime(int hr, int min, int pm) {
        imp_ = new CivilianTimeImp(hr, min, pm);
    }
};

class ZuluTime: public Time {
  public:
    ZuluTime(int hr, int min, int zone) {
        imp_ = new ZuluTimeImp(hr, min, zone);
    }
};

int main() {
  Time *times[3];
  times[0] = new Time(14, 30);
  times[1] = new CivilianTime(2, 30, 1);
  times[2] = new ZuluTime(14, 30, 6);
  for (int i = 0; i < 3; i++)
    times[i]->tell();
}
#包括
#包括
#包括
类时间戳{
公众:
TimeImp(整数小时,整数分钟){
hr=hr;
最小值=最小值;
}
虚拟void tell(){
cout
std::setfill
,您正在向它传递一个
int

尝试:

cout
std::setfill
,您正在向它传递一个
int

尝试:


可能不能使用不同的设计模式吗?先试试适配器。@juanchopanza我想这不是设计模式错误,反正我试过适配器,但在不同的代码上成功运行了。这很有趣,你的标题和代码示例似乎表明你对桥接模式有问题:-)我想你把“std:”放错地方了s、 是的,事实上,我只是为了让读者知道我正在实现桥接模式,也不完全确定它的模式是否错误,因为sourcemaking.com显示的o/p是:
Outputtime是1430时间是下午2:30时间是1430中央标准时间
可能使用不同的设计模式?先试试适配器。@juanchopanza我想这不是设计模式错误,不管怎样,我尝试了适配器,但在不同的代码上成功运行。有趣的是,您的标题和代码示例似乎表明您对桥接模式有问题:-)我认为您错放了“std:”s、 是的,事实上,我只是为了让读者知道我正在实现桥接模式,也不完全确定它的模式是否错误,因为sourcemaking.com显示o/p是:
输出时间是1430时间是下午2:30时间是1430中央标准时间
字符不再是数字了吗?我错过了什么吗?@πάνταῥεῖ:
setfill(48)
将给出
setfill
的返回类型,这(可能)不同于
setfill
的返回类型,即
operator@πάνταῥεῖ : 你说得对,我关注的是如何修复错误,而不是解释错误的原因(谢谢迈克)。字符不再是数字了吗?我错过了什么吗?@πάνταῥεῖ: 
setfill(48)
将给出
setfill
的返回类型,这(可能)不同于
setfill
的返回类型,即
operator@πάνταῥεῖ : 你说得对,我关注的是如何修复错误,而不是解释错误出现的原因(谢谢迈克)。
cout << "time is " << setw(2) << setfill(' ') << hr_ << min_ << endl;