专业化成员S :: display需要'template<&''语法 [英] specializing member S::display requires ‘template<>’ syntax
问题描述
我正在创建一个trait类来帮助我的程序。我有一个包含方法 display
和区域$ c>的模板类
operations
$ c>。当我定义这些功能,我得到错误。这里他们是:
I'm creating a trait class to help with my program. I have a template class called operations
that contains the methods display
and area
. When I define these functions I get errors. Here they are:
错误:specializing member
'traits :: operations< Rectangle& code>需要
'template<>'
语法
错误:specializing member'traits :: operations< Rectangle> ; :: area'
需要'template<>'
语法
如你所见,编译器要求我在这些定义之前插入 template<>
。但是当我做,我得到一个巨大的页面的错误。
As you can see, the compiler wants me to insert template <>
just before those definitions. But when I do, I get a huge page of errors. What's going wrong and how can I fix it?
这是我的程序。
namespace traits
{
template <typename P>
struct operations
{
static void display(Rectangle const &, std::ostream &);
static void area(Rectangle const &);
};
template <typename P, int N>
struct access {};
}
namespace traits
{
template <int N>
struct access<Rectangle, N>
{
static double get(Rectangle const &);
};
}
// The errors occur here
namespace traits
{
static void operations<Rectangle>::display(Rectangle const &rect, std::ostream &os)
{
os << rect.width << '\n';
os << rect.height << '\n';
os << area(rect) << '\n';
}
static void operations<Rectangle>::area(Rectangle const& rect)
{
double width = get<0>(rect);
double height = get<1>(rect);
return width * height;
}
}
namespace traits
{
template <>
struct access<Rectangle, 0>
{
static double get(Rectangle const &rect)
{
return rect.width;
}
};
template <>
struct access<Rectangle, 1>
{
static double get(Rectangle const &rect)
{
return rect.height;
}
};
}
template <int N, typename P>
static inline double get(P const &p)
{
return traits::access<P, N>::get(p);
}
template <typename P>
static inline void display(P const &p)
{
traits::operations<P>::display(p, std::cout);
}
template <typename P>
static inline double area(P const &p)
{
return traits::operations<P>::area(p);
}
int main()
{
}
b
$ b
这是一个显示错误的程序 - http://ideone.com/ WFlnb2#view_edit_box
感谢任何一切帮助。
由于评论的帮助我摆脱了这两个错误,但我没有得到更多后添加模板<>
声明和修复的返回类型:
Thanks to help from the comments I got rid of those two errors, but I'm not getting more after adding the template<>
declaration and fixing the return type of area
:
错误:无法声明成员函数
'static void traits :: operation< P> :: display(const Rectangle& std :: ostream&)[with P = Rectangle; std :: ostream = std :: basic_ostream< char>]'
具有静态链接[-fpermissive]
错误:显式模板专门化不能有存储类
错误:实例化之后'static double traits :: operations< P> :: area(const Rectangle&)[with P = Rectangle]'
错误:显式模板专门化不能有存储类
error: cannot declare member function
‘static void traits::operations<P>::display(const Rectangle&, std::ostream&) [with P = Rectangle; std::ostream = std::basic_ostream<char>]’
to have static linkage [-fpermissive]
error: explicit template specialization cannot have a storage class
error: specialization of‘static double traits::operations<P>::area(const Rectangle&) [with P = Rectangle]’
after instantiation
error: explicit template specialization cannot have a storage class
推荐答案
显示
和区域
应该这样写:
template <>
double operations<Rectangle>::area( Rectangle const& rect )
{
double width = get<0>(rect);
double height = get<1>(rect);
return width * height;
}
- 至于模板专用函数,
template<>
应放置在函数头部的
。 - 对于静态成员函数,
static
不应出现在函数的
定义体。 - As for template specialized functions,
template <>
should be placed at the head of the function. - For static member functions,
static
should not appear at the definition body of the function.
这篇关于专业化成员S :: display需要'template<&''语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!