c++ - 与“operator =”不匹配

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

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

#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;
}

最佳答案

您要么需要编写operator=的实现以接受适当的右侧,要么可以使用templated <>运算符=。这是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
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

关于c++ - 与“operator =”不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30769752/

相关文章:

c++ - 从文件中逐行加载 2D vector 时出现段错误

c++ - 调整窗口大小时,Gtkmm-app 在 Windows 8.1 上崩溃

templates - Symfony 加载电子邮件的 Twig 模板

c++ - g++-6 阴影模板参数错误,而 g++-5 没有

java 从命令行导入包

C++ 无法从类型 bool(class) 转换为 bool

c++ - 执行示例项目时出现 libvlc-qt 错误

c++ - Boost basic_deadline_timer 在几次迭代后停止触发

html5 Liquid CSS 的固定左侧主要内容区域与右侧内容液体

c++ - 为什么在实现 header 中定义的模板化函数和类时使用 "tpp"文件?