【问题标题】:How to overload << operator for cv::Filestorage?如何为 cv::Filestorage 重载 << 运算符?
【发布时间】:2015-11-23 08:47:05
【问题描述】:

我想使用 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;
    }
};

错误信息:

【问题讨论】:

    标签: c++ opencv


    【解决方案1】:

    您的operator&lt;&lt; 应具有以下签名:

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

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

    【讨论】:

    • 谢谢,但这样我会收到“to many arguments”错误消息。 stackoverflow.com/questions/19709048/…
    • 好的,我刚刚删除了 MarkerDistance:: 部分,它起作用了。谢谢
    • @R_Valdez,很高兴知道)
    猜你喜欢
    • 1970-01-01
    • 2015-09-04
    • 1970-01-01
    • 2012-05-29
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多