C ++类模板是一个模板:模板参数无效 [英] C++ class template is a template: template argument is invalid

查看:1686
本文介绍了C ++类模板是一个模板:模板参数无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类模板的问题。我希望类中的私有数据是某种数字类型的向量的向量,即:

I have a problem with a class template. I want the private data in a class to be a vector of vectors of some kind of numeric type, i.e:

std::vector<std::vector<double> >
std::vector<std::vector<std::complex<double> > >

但我想要的矢量类型(我使用的第三方矢量库和stl向量)和要模板化的元素类型。我试过模板模板,但现在我不认为这是我的问题的解决方案。一个高度简化的例子是:

But I want the type of vector (I'm using a library of third party vectors along with stl vectors), and the element type to be templated. I tried template templates but now I don't think that is the solution to my problem. A highly simplified example is:

#include <complex>
#include <vector>
template<typename T>
class Fred {
    std::vector<T> data_;
};
int main(){
    Fred<std::vector<double> > works;
    //Fred<std::vector<std::complex<double> > doesnt_work;
    return 0;
}

如图所示,它编译正常,但如果我取消注释main中的第二行,我收到错误(g ++ 4.6):

As shown it compiles fine, but if I uncomment the second line in main, I get the error (g++ 4.6):

error: template argument 1 is invalid

为什么会收到这个错误?任何人都有建议的修复?
谢谢!

Why do I get this error? And does anyone have a suggested fix? Thanks!

推荐答案

#include <complex>
#include <vector>
template<typename T>
class Fred {
    std::vector<T> data_;
};

int main(){
    //Fred<std::vector<double> > works;
    Fred<std::vector<std::complex<double> > > doesnt_work;
    return 0;
}

您错过了 doesnt_work 声明中的第三个>

Works well. You miss third > in declaration of doesnt_work.

这篇关于C ++类模板是一个模板:模板参数无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆