17

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> > >

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;
}

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!

jtravs
  • 321
  • 1
  • 3
  • 9

1 Answers1

23
#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;
}

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

ForEveR
  • 55,233
  • 2
  • 119
  • 133
  • if only the compiler could just know it as a missing bracket, rather than printing a thousand line error message - which btw completely meaningless – artm May 17 '23 at 04:38