繁体   English   中英

使用重载运算符不明确

[英]Use of overloaded operator ambiguous

如下代码:

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:错误:重载运算符'<<'的用法含糊(操作数类型为'basic_ostream>'和'const HELPER *'(aka'const void *'))

它列出了一些候选人:

/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并计划在此双重目标上使用,因此我在Serial对象上重载了<<,并将先前的对象放入了其中(例如,这是过分简化的版本)

#define cout Serial

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

现在,我实际上想将真正的iostream定位为其他拱门,但是旧的Arduino代码与__FlashStringHelpers不能有太大的不同。 那是我在的地方

typedef不会创建类型,而是将其作为别名,

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

相当于

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

也许您想创建一个名为HELPER的类型

class HELPER{};

当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运算符<<。 这将适用于整数和原子类型。 如果传递另一个结构或类对象,则必须为其定义其他重载的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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM