5

我不确定为什么不使用 g++ 编译以下代码:

t.cpp: In instantiation of ‘Distrib<double>’:
t.cpp:28:56:   instantiated from ‘Sampler<Distrib<Solution<double> > >’
t.cpp:35:48:   instantiated from here
t.cpp:16:45: erreur: ‘double’ is not a class, struct, or union type
t.cpp:18:43: erreur: ‘double’ is not a class, struct, or union type

我期待能够AtomType在嵌套模板中传播类型……</p>

#include <iostream>
#include <vector>

template<typename T>
class Solution
{
    public:
        typedef T AtomType;
};

template<typename SOLT>
class Distrib
{
    public:
        typedef typename SOLT::AtomType AtomType;
        typedef std::vector<AtomType> Matrix;

        Matrix matrix;
};

template<typename DT>
class Sampler
{
    public:
        typedef typename DT::AtomType AtomType;
        typedef typename Distrib<AtomType>::Matrix Matrix;

        Matrix matrix;
};

int main()
{
    Sampler< Distrib< Solution<double> > > sampler;
}
4

4 回答 4

4

In your Distrib template you have the following typedef

typedef typename SOLT::AtomType AtomType;

Which means that any type you pass in as a template parameter, must have a AtomType as a member, and double has no such thing.

If you made a class like so

class Double
{
   typedef myType AtomType;
};

and passed that in as a template parameter to your Distrib template, it would compile, as Double::AtomType does exist.

于 2012-07-10T09:33:10.257 回答
3

在你的Sampler课堂上,你有:

typedef typename Distrib<AtomType>::Matrix Matrix;

这里,AtomTypedouble,所以这是

typedef typename Distrib<double>::Matrix Matrix;

然后在你的Distrib课上,这条线

typedef typename SOLT::AtomType AtomType;

扩展到

typedef typename double::AtomType AtomType;

因此出现错误消息。我想你希望Sampler课堂上的那一行是:

typedef typename DT::Matrix Matrix;
于 2012-07-10T10:10:02.750 回答
1

Distrib被模板化为Solution; 但是在Sampler::MatrixAtomType用作模板参数的定义中。据推测,您只想要Distrib提供的类型Sampler

template<typename DT>
class Sampler
{
    // ...
    typedef typename DT::Matrix Matrix;
};
于 2012-07-10T10:33:57.113 回答
1

类中的MatrixtypedefDistrib正在使用AtomType,但我们期望的是DT

typedef typename Distrib<DT>::Matrix Matrix;

编译器看到double跨嵌套模板传播。

于 2012-07-10T09:50:00.500 回答