我在超负荷的ostream操作员上遇到了一些麻烦。有3个类,抽象基类包,另一个从数据包继承的抽象类序列和从序列继承的类TimeHistory。
class Packet
{
//some stuff
};
template <typename T = double>
class Sequence : public Packet
{
std::vector<T> buffer;
//some other stuff
}
template <typename T = double>
class TimeHistory : public Sequence<T>
{
template <typename X>
friend std::ostream &operator<<(std::ostream &out, Packet *A);
//another stuff
}和一个用于打印对象中数据的朋友函数。
std::ostream &operator<<(std::ostream &out, Packet *A)
{
TimeHistory<T> *th = dynamic_cast<TimeHistory<T> *>(A);
out << th->device
<< th->description
<< th->channelNr;
for (auto in : th->buffer)
out << in << std::endl;
return out;
} 当我创建一个类的实例时,例如:
std::unique_ptr<Packet> channel1 = std::make_unique<TimeHistory<double>>(/*some constructor stuff*/);并调用该函数
std::cout<<(channel1.get());在输出端,我只得到一个内存单元: 0x560c8e4f1eb0有人能指出我做错了什么吗?
发布于 2020-05-20 16:40:34
std::unique_ptr::get将返回指向托管对象的指针。如果要获得托管值的引用,请使用
std::cout<< *channel1;要为抽象类重载operator<<,可以使用引用而不是指针:
friend std::ostream &operator<<(std::ostream &out, Packet &A);https://stackoverflow.com/questions/61918273
复制相似问题