我不知道理解爲什麼下面的代碼不編譯克+ +:C++的typedef不是一個類,結構或聯合類型
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
型跨越嵌套模板...
#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;
}
注意,OP是通過'解決'作爲參數傳遞給'Distrib'但錯誤是一樣的,如果一個'雙'本身就通過了。 –
jrok
2012-07-10 10:09:09
這仍然不會編譯,即使它確實會是錯誤的。問題是使用'AtomType'作爲'Distrib'的模板參數,它需要某種'Solution'。 – 2012-07-10 10:26:40