How do you return a vector iterator from a variable in a template class?

I'm trying to return an iterator for a vector in a templated class (I'm not sure if this matters, but I read I could, so I thought I mentioned it). The problem is I get an error that C ++ doesn't support default-int when I try to do this. I looked online and from what I see in the forums and explanations I don't think I'm that far, it just won't compile.

template<class T>
class Table
{
public:
  ...

  vector<shared_ptr<vector<T>>>::iterator GetRowIterator();
  //vector<shared_ptr<vector<CellValueType> > >::const_iterator GetRowIterator();

  ...
protected:

  vector<shared_ptr<vector<CellValueType> > > data;  //outside vector is rows, inside vector is columns

  ...
};

vector<shared_ptr<vector<T> > >::const_iterator Table<T>::GetRowIterator()
{
  return data.begin();
}

      

I am getting the following errors:

error C2146: syntax error : missing ';' before identifier 'GetRowIterator'

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   

      

Edit:
Changed the end angle brackets so they are not that close together - same error.

Any thoughts on why this is happening?
As always, thanks for the advice / help in advance!

+1


source to share


2 answers


Also, remember to use typename when declaring the type of a template's return type:

typename vector< shared_ptr< vector< T > > >::iterator GetRowIterator();

      

and method definition

typename vector< shared_ptr< vector< T > > >::const_iterator Table<T>::GetRowIterator()
{
  return data.begin();
}

      



Note also that when defining a template class method outside of the class definition, you need to use the template keyword:

template <class T> typename vector< shared_ptr< vector< T > > >::const_iterator Table<T>::GetRowIterator()
    {
      return data.begin();
    }

      

So the compiler can know what T.

+4


source


This part is here:

vector<shared_ptr<vector<T>>>

      

This is a C ++ syntax problem that you cannot install -> together.



vector<shared_ptr<vector<T> > >

      

This is the problem that the new standard addresses.

Since the lexer is the first stage of the compiler, it sees β†’> as a left shift operator followed by>. This way you get syntax errors in your code. To fix this problem, you just need to add a space between> when closing templates.

+3


source







All Articles