【问题标题】:Why traits::access cannot be compiled (c++)?为什么无法编译traits::access (c++)?
【发布时间】:2016-07-04 15:47:21
【问题描述】:

我现在正在学习如何使用boost::geometry 库,并且我正在关注教程,其中引入了类型特征以使代码更通用。例如,下面的distance函数通过使用traits被认为是遗传的:

struct mypoint
{
    double x, y;
};

template <typename P1, typename P2>
double distance(P1 const& a, P2 const& b)
{
    double dx = get<0>(a) - get<0>(b);
    double dy = get<1>(a) - get<1>(b);
    return std::sqrt(dx * dx + dy * dy);
}

template <typename P1, typename P2>
double distance(P1 const& a, P2 const& b)
{
    double dx = get<0>(a) - get<0>(b);
    double dy = get<1>(a) - get<1>(b);
    return std::sqrt(dx * dx + dy * dy);
}

namespace traits
{
    template <>
    struct access<mypoint, 0>
    {
        static double get(mypoint const& p)
        {
            return p.x;
        }
    };

    template <>
    struct access<mypoint, 1>
    {
        static double get(mypoint const& p)
        {
            return p.y;
        }
    };

}

但是,当我编译代码时,会发生以下编译错误:

Error   3   error C2913: explicit specialization; 'traits::access' is not a specialization of a class template  

有什么想法吗?谢谢。

【问题讨论】:

  • 这个词是“通用的”,而不是“遗传的”:)

标签: c++ boost typetraits boost-geometry


【解决方案1】:

你已经专门化了traits::access 没有给出一个通用的定义,所以添加:

namespace traits
{
    template <typename, int>
        struct access;
    /* ... */
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 2014-07-30
    • 1970-01-01
    相关资源
    最近更新 更多