使用重载运算符模糊

时间:2016-12-20 12:31:56

标签: c++ operator-overloading

以下代码:

typedef void HELPER;

const HELPER* helper = _helper;

inline ostream& operator <<(ostream& out,  const HELPER* arg) 
{ out << (const char*)(arg); return out; }

如果我尝试

,就会爆炸
cout << helper;

具体来说,我得到:

  

main.cpp:35:28:错误:使用重载运算符&#39;&lt;&lt;&lt;&#39;很暧昧   (操作数类型&#39; basic_ostream&gt;&#39; const HELPER *&#39;(又名&#39; const void *&#39;)

它列出了一些候选人:

/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:207:0: note: candidate function
    basic_ostream& operator<<(const void* __p);
                   ^
main.cpp:25:17: note: candidate function
inline ostream& operator <<(ostream& out,  const HELPER* arg) 
                ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:195:20: note: candidate function
    basic_ostream& operator<<(bool __n);
                   ^

我有点惊讶我的typedef在这里没有调用更强的类型匹配。如何让这个运算符重载运行?

编辑:进一步澄清,此代码的目的是我对一组Arduino库进行双重定位。他们经常用以下方式管理他们的字符串:

typedef void __FlashStringHelper;

void showHelp(const __FlashStringHelper* helpText)
{
   Serial.print(helpText);
}

我喜欢iostream并计划在这个双重目标上,所以我重载了&lt;&lt;在Serial对象上并将之前的内容(例如,这是过于简化的版本)

#define cout Serial

void showHelp(const __FlashStringHelper* helpText)
{
   cout << helpText;
}

现在我想实际针对不同的拱门定位真正的iostream,但旧的Arduino代码与其__FlashStringHelpers不同(很多)。我在

的地方

2 个答案:

答案 0 :(得分:2)

typedef没有创建别名的类型,

inline ostream& operator <<(ostream& out,  const HELPER* arg) 

相当于

inline ostream& operator <<(ostream& out,  const void* arg)

也许你想创建一个名为HELPER

的类型
class HELPER{};

答案 1 :(得分:0)

当Zekian回答你的问题时,这里可能对你有用或帮助你实现你想要做的事。

#include <iostream>

template <class T>
class Helper {
private:
    T obj_;

public:
    explicit Helper<T>( T obj ) : obj_(obj) {}

public:
    T getObj() const { return obj_; }
    void setObj( T obj ) { obj_ = obj; }        

    template<class U>
    inline friend std::ostream& operator<< ( std::ostream& out, const Helper<U>& rhs ); 
};

template<class U>
std::ostream& operator<< ( std::ostream& out, const Helper<U>& rhs ) {
    return out << rhs.obj_;
}

int main() {
    Helper<int> helper( 3 );
    std::cout << helper << std::endl;
    return 0;
}

它是一个包装类模板,带有重载的ostream运算符&lt;&lt ;.这适用于整数和原子类型。如果传递另一个struct或class对象,则必须为它们定义其他重载的ostream运算符。

示例 - 相同的类模板,但这次使用的是类或结构。

#include <iostream>

template <class T>
class Helper {
private:
    T obj_;

public:
    explicit Helper<T>( T obj ) : obj_(obj) {}

public:
    T getObj() const { return obj_; }
    void setObj( T obj ) { obj_ = obj; }

    template<class U>
    inline friend std::ostream& operator<< ( std::ostream& out, const Helper<U>& rhs ); 
};

template<class U>
std::ostream& operator<< ( std::ostream& out, const Helper<U>& rhs ) {
    return out << rhs.obj_;
}

struct Staff {
    int employees = 4; // Default to 4
};

int main() {
    Staff staff; 
    Helper<Staff> helper( staff );
    std::cout << helper << std::endl; // will not compile

    return 0;
}

要修复此ostream,需要为Staff对象

重载运算符
template <class T>
class Helper {
private:
    T obj_;

public:
    explicit Helper<T>( T obj ) : obj_(obj) {}

public:
    T getObj() const { return obj_; }
    void setObj( T obj ) { obj_ = obj; }

    template<class U>
    inline friend std::ostream& operator<< ( std::ostream& out, const Helper<U>& rhs );
};

template<class U>
std::ostream& operator<< ( std::ostream& out, const Helper<U>& rhs ) {
    return out << rhs.obj_;
}

struct Staff {
    int employees = 4;

    inline friend std::ostream& operator<< ( std::ostream& out, const Staff& rhs );
};

std::ostream& operator<<( std::ostream& out, const Staff& rhs ) {
    return out << rhs.employees;
}

int main() {

    Staff staff;
    Helper<Staff> helper( staff );   // Default to 4
    std::cout << helper << std::endl; // Will Print 4
    // To Change Staff's Employee count for the helper wrapper do this:
    staff.employees = 12; // Change To 12
    helper.setObj( staff ); // pass the changed struct back into helper
    std::cout << helper3 << std::endl; // Will Now Print 12

    // And For Other Default Types
    Helper<int> helper2( 3 );
    std::cout << helper2 << std::endl;

    Helper<float> helper3( 2.4f );
    std::cout << helper3 << std::endl;

    return 0;
}