首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >:ostream& operator<<( std::ostream&,const &)未被覆盖

:ostream& operator<<( std::ostream&,const &)未被覆盖
EN

Stack Overflow用户
提问于 2011-06-29 10:21:06
回答 1查看 3.9K关注 0票数 1

偶尔,我会编写一个类(T ),并尝试重写std::ostream& operator<<(std::ostream&,const &),但它不适用于某些类。下面是一个(简化的)类的示例,它对我不起作用。

代码语言:javascript
复制
class ConfigFile {
public:
    explicit ConfigFile(const std::string& filename);
    virtual ~ConfigFile();

    bool saveToDisk() const;
    bool loadFromDisk();

    std::string getSetting(const std::string& setting, const std::string& section="Misc") const;
    void setSetting(std::string value, const std::string& name, const std::string& section ="Misc", bool updateDisk = false);

    inline const SettingSectionMap& getSettingMap() const {
         return mSettingMap;
    }

private:
    std::string         mSettingFileName;
    SettingSectionMap       mSettingMap;

#if  defined(_DEBUG) || defined(DEBUG)
public:
    friend std::ostream& operator<<(std::ostream& output, const ConfigFile& c) {
        output << “Output the settings map here”;
        return output;
    }
#endif
}

我确信显式关键字将阻止转换构造函数场景,但它的作用肯定类似,因为当我执行以下操作时

代码语言:javascript
复制
std::cout << config_ << std::endl;

它输出的内容类似于: 0x100588140。但是我在另一个班级做同样的事情,就像下面的那个一样,一切都很好。

代码语言:javascript
复制
class Stats {
    Stats() {};

#if  defined(_DEBUG) || defined(DEBUG)
    friend std::ostream& operator<<(std::ostream& output, const Stats& p) {
        output << "FPS Stats: " << p.lastFPS_ << ", " << p.avgFPS_ << ", " << p.bestFPS_ << ", " << p.worstFPS_ << " (Last/Average/Best/Worst)";
        return output;
    };
#endif
}; 

谢谢你的帮助。

编辑:为了解决这个问题,我现在将以下内容添加到我的所有类中:

代码语言:javascript
复制
#if  defined(_DEBUG) || defined(DEBUG)
public:
    friend std::ostream& operator<<(std::ostream& output, const ConfigFile& c);
    friend std::ostream& operator<<(std::ostream& output, ConfigFile* c) {
        output << *c;
        return output;
}
#endif
EN

回答 1

Stack Overflow用户

发布于 2011-07-02 09:38:35

尝试将签名更改为const指针:

代码语言:javascript
复制
std::ostream& operator<<(std::ostream& output, const ConfigFile* c);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6519068

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档