gpt4 book ai didi

c++ - 桥接模式C++代码编译错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:37:46 25 4
gpt4 key购买 nike

我正在尝试编译桥接模式 C++ 示例代码,如前所述 here

但是当我执行 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: In member function ‘virtual void TimeImp::tell()’: Bridge.C:14: error: no match for ‘operator<<’ in ‘std::operator<< [with _CharT = char, _Traits = std::char_traits](((std::basic_ostream >&)((std::basic_ostream >)std::operator<< [with _Traits = std::char_traits](((std::basic_ostream >&)(& std::cout)), ((const char)"time is ")))), std::setw(2)) << std::setfill with _CharT = int’ /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/ostream.tcc:67: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& ()(std::basic_ostream<_CharT, _Traits>&)) [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: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& ()(std::basic_ios<_CharT, _Traits>&)) [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<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits]

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

我已经复制粘贴了相同的代码,分析了错误行但无法得到它。

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

最佳答案

std::setfill takes a char ,您正在向它传递 int

尝试:

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

关于c++ - 桥接模式C++代码编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24491092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com