不匹配'operator =

时间:2015-06-11 00:47:20

标签: c++ templates compiler-errors operator-overloading

我能够对相似类型的点一起执行操作,但不能对不同类型的点执行操作。我想我需要一些方法将point int的矢量坐标转换为vector double的矢量坐标。

#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>

#if 1
    #define log(x) std::cout << x << std::endl;
#else
    #define log(x)
#endif

template<typename type>
std::vector<type> operator+(const std::vector<type> l, const std::vector<type> r){
    std::vector<type> ans;
    std::transform(l.begin(), l.end(), r.begin(), std::back_inserter(ans), std::plus<type>());
    return ans;
};
template<typename type>
std::vector<type> operator*(const std::vector<type> l, const std::vector<type> r){
    std::vector<type> ans;
    std::transform(l.begin(), l.end(), r.begin(), std::back_inserter(ans), std::multiplies<type>());
    return ans;
};
template<typename type>
std::vector<type> operator-(const std::vector<type> l, const std::vector<type> r){
    std::vector<type> ans;
    std::transform(l.begin(), l.end(), r.begin(), std::back_inserter(ans), std::minus<type>());
    return ans;
};
template<typename type>
std::vector<type> operator/(const std::vector<type> l, const std::vector<type> r){
    std::vector<type> ans;
    std::transform(l.begin(), l.end(), r.begin(), std::back_inserter(ans), std::divides<type>());
    return ans;
};

template<class type> struct point{

    template<typename a = type, typename... b> point(a coordinate, b... coordinates){
        this->coordinates = {coordinate, coordinates...};
    };std::vector<type> coordinates;

    template<typename a = type> point(std::vector<a> coordinates){
        this->coordinates = coordinates;
    };
    friend point<type> operator+(const point<type>& l, const point<type>& r){
        point<type> ans(l.coordinates + r.coordinates);
        return ans;
    };
    friend point<type> operator*(const point<type>& l, const point<type>& r){
        point<type> ans(l.coordinates * r.coordinates);
        return ans;
    };
    friend point<type> operator-(const point<type>& l, const point<type>& r){
        point<type> ans(l.coordinates - r.coordinates);
        return ans;
    };
    friend point<type> operator/(const point<type>& l, const point<type>& r){
        point<type> ans(l.coordinates / r.coordinates);
        return ans;
    };
    friend std::ostream& operator<<(std::ostream& stream, const point& p){
        switch(p.coordinates.size()){
        case 1:
            std::cout << "(" << p.coordinates[0] << ")";
            break;
        default:    
            std::cout << "("; 
            for(int i = 0; i < p.coordinates.size(); ++i){
                if(i == (p.coordinates.size() - 1))
                    std::cout << p.coordinates[i];
                else
                    std::cout << p.coordinates[i] << ", ";
            }
            std::cout << ")";
            break;
        }
    }
};
int main(){
    point<int> a(2,5,5,4,3);
    point<int> b(3,5,3,5,7);
    point<double> c(a/b);
    log(c);
    return 0;
}

1 个答案:

答案 0 :(得分:0)

您需要编写接受适当右侧的operator=的实现,或者您可以使用模板化的&lt;&gt;运算符=。这是C ++ 11的实现,如果您没有C ++ 11,那么您将需要第2版

template<typename T>
struct Point {
    using value_type = T;
    using self_type = Point<T>;

    value_type m_value;

    Point(value_type value) : m_value(value) {}

    template<typename RhsT> self_type& operator = (const Point<RhsT>& rhs) {
        m_value = static_cast<value_type>(rhs.m_value);
        return *this;
    }
};

int main() {
    Point<int> a1(4);
    Point<int> a2(5);
    Point<double> a3(6.0);

    a1 = a2;
    a2 = a3;

    return 0;
}

http://ideone.com/csATfH Pre-C ++ 11版本:

#include <string>
#include <type_traits>

template<typename T>
struct Point {
    typedef T value_type;
    typedef Point<T> self_type;

    value_type m_value;

    Point(value_type value) : m_value(value) {}

    template<typename RhsT>
    self_type& operator = (const Point<RhsT>& rhs) {
        m_value = static_cast<value_type>(rhs.m_value);
        return *this;
    }
};

int main() {
    Point<int> a1(4);
    Point<int> a2(5);
    Point<double> a3(6.0);

    a1 = a2;
    a2 = a3;

    return 0;
}

http://ideone.com/w24O9s

带有enable_if的花哨版本:

#include <string>
#include <type_traits>

template<typename T>
struct Point {
    using value_type = T;
    using self_type = Point<T>;

    value_type m_value;

    Point(value_type&& value) : m_value(std::forward<value_type>(value)) {}

    template<typename RhsT,
        typename std::enable_if<
            std::is_arithmetic<value_type>::value
            && std::is_arithmetic<RhsT>::value, int>::type = 0>
    self_type& operator = (Point<RhsT>&& rhs) {
        m_value = static_cast<value_type>(rhs.m_value);
        return *this;
    }
};

int main() {
    Point<int> a1(4);
    Point<int> a2(5);
    Point<double> a3(6.0);
    Point<std::string> a4("hello");

    a1 = a2;
    a2 = a3;

    a3 = a4;

    return 0;
}

http://ideone.com/OtD24Y