首页 > 解决方案 > 导致编译错误的转换运算符

问题描述

我已经实现了一个类似字符串的类,称为 Str,并且想要添加到 bool 类型的转换,以便使用 Str 对象作为条件。

Str 类非常简单,它依赖于另一个名为 Vec 的模板类,它类似于 STL 向量类。下面我复制了定义的主要部分

class Str
{

    friend std::istream& operator>>(std::istream&, Str&);

private:
    Vec<char> data;

public:
    typedef Vec<char>::size_type size_type;
    typedef Vec<char>::iterator iterator;
    typedef Vec<char>::const_iterator const_iterator;

    iterator begin() { return data.begin(); }
    const_iterator begin() const { return data.begin(); }
    iterator end() { return data.end(); }
    const_iterator end() const { return data.end(); }

    //[...]

    char& operator[](size_type i) { return data[i]; }
    const char& operator[](size_type i) const { return data[i]; }
    Str& operator+=(const Str& s)
    {
        std::copy(s.data.begin(), s.data.end(), std::back_inserter(data));
        return *this;
    }

};

Str::operator bool() const
{
    if (data.size() > 0)
        return true;
    return false;
}

一切正常,除了现在,如果我尝试执行以下操作:

Str greeting = "Hello, " + name + "!";

然后我得到一个编译错误,说“'operator +':2个重载有类似的转换,可能是'Str operator +(const Str &,const Str &)'或'built-in C++ operator+(const char [8],整数)'。

我了解错误的原因,但我不知道如何解决它。我怎样才能解决这个问题?

谢谢。

编辑:下面我添加了一个最小、完整且可验证的示例:

#include <iterator>
#include <vector>
#include<iostream>

using namespace std;

class Str
{

private:
    std::vector<char> data;

public:
    typedef std::vector<char>::size_type size_type;
    typedef std::vector<char>::iterator iterator;
    typedef std::vector<char>::const_iterator const_iterator;

    iterator begin() { return data.begin(); }
    const_iterator begin() const { return data.begin(); }
    iterator end() { return data.end(); }
    const_iterator end() const { return data.end(); }


    // default constructor must be defined explicitly, since non-default constructors are also defined
    Str() { }
    Str(const size_type n, const char c) : data(n, c) {  }
    Str(const char* cp)
    {
        std::copy(cp, cp + std::strlen(cp), std::back_inserter(data));
    }
    // Since this is a template, the iterators can be anything: pointers in an array of chars, std::vector<string> iterators, std::std::vectortor<string> iterators, std::list<string> iterators, etc.
    template <class In> Str(In b, In e)
    {
        std::copy(b, e, std::back_inserter(data));
    }
    char& operator[](size_type i) { return data[i]; }
    const char& operator[](size_type i) const { return data[i]; }
    Str& operator+=(const Str& s)
    {
        std::copy(s.data.begin(), s.data.end(), std::back_inserter(data));
        return *this;
    }
    size_type size() const
    {
        return data.size();
    }

    // conversion operators
    operator std::vector<char>() const;
    operator bool() const;

    template <class In> void insert(iterator dest, In b, In e)
    {
        data.insert(dest, b, e);
    }

};

Str operator+(const Str& s1, const Str& s2)
{
    Str s = s1;
    s += s2;
    return s;
}

Str::operator std::vector<char>() const
{
    std::vector<char> ret = data;
    return ret;

}

Str::operator bool() const
{
    if (data.size() > 0)
        return true;
    return false;
}


int main()

{

    Str name = "Joe";
    Str greeting = "Hello, " + name + "!";

    return 0;
}

标签: c++castingstdstring

解决方案


推荐阅读