gpt4 book ai didi

c++ - 如何为 cv::Filestorage 重载 << 运算符?

转载 作者:太空宇宙 更新时间:2023-11-03 22:11:43 26 4
gpt4 key购买 nike

我想使用 cv::FileStorage 类将我的 MarkerDistance 结构写入文件。

我尝试以各种方式重载 << 运算符,但它无法编译。而且我不确定到底出了什么问题。如果有人能指出正确的方向,那就太好了。

struct MarkerDistance
{
std::string linkName;
double distance;

MarkerDistance(std::string a, double b)
{
linkName = a;
distance = b;
}
//Version 1
cv::FileStorage & MarkerDistance::operator<< (cv::FileStorage & fs)
{
std::string link = "linkName : " + linkName + "\n";
std::string dist = "distance : " + std::to_string(distance) + "\n";
std::string erg = link + dist;
fs << erg;
}

//Version 2
std::ostream& MarkerDistance::operator << (std::ostream& os)
{
// write obj to stream
std::string link = "linkName : " + linkName + "\n";
std::string dist = "distance : " + std::to_string(distance) + "\n";
std::string erg = link + dist;
return os << erg;
}
};

错误信息:

enter image description here

最佳答案

你的 operator<<应具有以下签名:

std::ostream& MarkerDistance::operator<<(std::ostream& os, const MarkerDistance& obj);

cv::FileStorage& MarkerDistance::operator<<(cv::FileStorage& os, const MarkerDistance& obj);

关于c++ - 如何为 cv::Filestorage 重载 << 运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33866780/

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