1
我跟随this question为了选择合适的容器,但我遇到了问题。选择正确的矢量与模板
我有一个selector
类必须推回到指针的向量,但正确的取决于它的尺寸(1载体,2为矩阵):
class selector
{
struct formValues : std::vector<coolvector<double>*>, std::vector<coolmatrix<double>*> { };
formValues maps;
public:
selector() { };
template<unsigned int formdim, typename F>
void operator+=(const form<formdim, F> &f)
{
typedef typename form<formdim, F>::storage_type storage_type;
typedef typename std::vector<storage_type*> pointer_type;
// Push to the right vector
formValues<pointer_type> &m = maps;
m.push_back(f.storage.get());
}
};
的形式类具有的尺寸和存储,也取决于尺寸,使用共享指针:
template <bool, class if_true, class if_false>
struct type_switch
{
typedef if_false type;
};
template <class if_true, class if_false>
struct type_switch<true, if_true, if_false>
{
typedef if_true type;
};
template <class T> class coolvector {};
template <class T> class coolmatrix {};
template<unsigned int formdim, typename F>
class form
{
public:
form() = delete;
form(const std::string &s) : type(s)
{
storage = std::make_shared<storage_type>();
}
std::string type;
typedef typename type_switch<formdim == 1, coolvector<double>, coolmatrix<double>>::type storage_type;
std::shared_ptr<storage_type> storage;
};
class oneform : public form<1, oneform>
{
public:
oneform() = delete;
oneform(const std::string &s) : form(s) { };
double operator()(unsigned int i) { return i * 2; };
};
class twoform : public form<2, twoform>
{
public:
twoform() = delete;
twoform(const std::string &s) : form(s) { };
double operator()(unsigned int i, unsigned int j) { return i * 2 + j * 20; };
};
的问题是,在selector::operator+=
我得到这个错误:
main.cpp:77:19: error: expected unqualified-id
formValues<pointer_type> &m = maps;
^
任何提示的赞赏!
'+ ='应该返回'selector *'('this')而不是'void'? ....'formValues'类型!='formValues'类型? –
javaLover
我可以返回'selector&',但它对问题没有影响。 – senseiwa
'type_switch'看起来很像'std :: conditional'。 – aschepler