gpt4 book ai didi

c++ - 为什么无法编译 traits::access (c++)?

转载 作者:行者123 更新时间:2023-11-28 02:04:20 25 4
gpt4 key购买 nike

我现在正在学习如何使用 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  

有什么想法吗?谢谢。

最佳答案

你有专门的 traits::access 而没有给出通用定义,所以添加:

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

关于c++ - 为什么无法编译 traits::access (c++)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38188460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com