桥模式C ++代码编译错误

时间:2014-06-30 13:19:51

标签: c++ linux gcc bridge

我正在尝试按照here

编译桥接模式C ++示例代码

但是当我g++ -o Bridge 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();
}

错误:

  

] #g ++ -o Bridge Bridge.C Bridge.C:在成员函数'virtual void中   TimeImp :: tell()':Bridge.C:14:错误:不匹配'operator&lt;&lt;'in   “的std ::运营商LT;&LT; [_CharT = char,_Traits =   std :: char_traits](((std :: basic_ostream&gt;&amp;)((std :: basic_ostream&gt; )std :: operator&lt;&lt; [with _Traits =   std :: char_traits](((std :: basic_ostream&gt;&amp;)(&amp; std :: cout)),((const char )&#34;时间是   &#34;)))),std :: setw(2))&lt;&lt; std :: setfill with _CharT = int'   /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/ostream.tcc:67:注意:候选人是: std :: basic_ostream&lt; _CharT,_Traits&gt;&amp;   的std :: basic_ostream&LT; _CharT,   _Traits&gt; :: operator&lt;&lt;(std :: basic_ostream&lt; _CharT,_Traits&gt;&amp;()(std :: basic_ostream&lt; _CharT,_Traits&gt;&amp;))[with _CharT = char,   _Traits = std :: char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/ostream.tcc: 78:注意:std :: basic_ostream&lt; _CharT,_Traits&gt;&amp;   std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(std :: basic_ios&lt; _CharT,   _Traits&GT;&安培; ()(std :: basic_ios&lt; _CharT,_Traits&gt;&amp;))[with _CharT = char,_ Traits = std :: char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.1.2/ ../../../../include/c++/4.1.2/bits/ostream.tcc:90:note:std :: basic_ostream&lt; _CharT,_Traits&gt;&amp;   std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(std :: ios_base&amp;   (*)(std :: ios_base&amp;))[与_CharT = char,_Traits =   的std :: char_traits]

这是错误的一部分,它以相同的模式扩展。

我复制粘贴相同的代码,分析错误行但无法获得。

有人可以告诉我哪里出错了吗?

1 个答案:

答案 0 :(得分:4)

std::setfill takes a char,您传递int

尝试:

cout << "time is " << setw(2) << setfill(' ') << hr_ << min_ << endl;