Joh*_*nes 2 c++ templates mingw visual-studio-2010
我试图递归地使用模板来定义(在编译时)双元组的d元组.下面的代码可以很好地编译Visual Studio 2010,但是g ++失败并且抱怨它"无法直接调用构造函数'指向<1> :: point'".
有谁能请说明这里发生的事情?
非常感谢,乔
#include <iostream>
#include <utility>
using namespace std;
template <const int N>
class point
{
private:
pair<double, point<N-1> > coordPointPair;
public:
point()
{
coordPointPair.first = 0;
coordPointPair.second.point<N-1>::point();
}
};
template<>
class point<1>
{
private:
double coord;
public:
point()
{
coord= 0;
}
};
int main()
{
point<5> myPoint;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
你想做什么:
coordPointPair.second.point<N-1>::point();
Run Code Online (Sandbox Code Playgroud)
看起来你想要显式调用point
默认构造函数 - 它pair
在构造时已被调用.你不能直接调用构造函数(除非你使用placement new,这在这种情况下是没有意义的)
只需删除该行.
如果由于某种原因想要覆盖已经构造的,.second
通过从临时分配它,point<N-1>
你可以这样做coordPointPair.second = point<N-1>();
.
如果对于更复杂的情况,想要将参数传递给point
构造函数,则可以在初始化列表中执行此操作:
point(your_type your_arg) :
coordPointPair(
pair<double, point<N-1> >(0.0, point<N-1>(your_arg_here))
)
{
}
Run Code Online (Sandbox Code Playgroud)