Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++, template argument error

Tags:

c++

templates

Below is my Template matrix which I want to build by taking value from user. But when I compile it. I am getting below error. Why the error ?

SO_template.cpp: In member function void Matrix<T>::BuildMatrix(std::vector<T, std::allocator<_CharT> >)': SO_template.cpp:44: error: expected;' before "it"

If I specialize my class using int it does not complain why?

 template<class T>
  class Matrix
  {
    private:
          vector<T> col;
          int iNumberOfRow;
          int iNumberOfCol;
    public:
     void BuildMatrix(const std::vector<T> stringArray)
     {

         std::vector<T>::iterator it= stringArray.begin();
         cout<<"Build Matrix irow="<<stringArray.size();
         ...
         ...
     }
};
like image 804
vrbilgi Avatar asked Jun 19 '26 01:06

vrbilgi


1 Answers

The issue is that std::vector<T>::iterator is a "dependent type" - the whole type depends on T. Prefix this with typename to fix the issue, so make the line read

typename std::vector<T>::iterator it= stringArray.begin();
like image 84
Frerich Raabe Avatar answered Jun 20 '26 15:06

Frerich Raabe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!