Re: Problem with templates and iterators
Bart Decuypere wrote:
Hi,
can you help me out with the question why the construct in main.cpp does
compile and the one in CTest.h does not (sources below).
error from g++ on windows/MinGW:
In file included from ../main.cpp:1:
.../CTest.h: In function `std::istream& operator>>(std::istream&, const
CTest<T>&)':
.../CTest.h:26: error: expected `;' before "it"
main.cpp
----------
#include "CTest.h"
#include <vector>
using std::vector;
int main(int argc, char **argv) {
vector<vector<int> > metType;
here, std::vector is *not* a dependant type.
//compiles OK
vector<vector<int> >::iterator it=metType.begin();
}
CTest.h
---------
#ifndef CTEST_H_
#define CTEST_H_
#include <iostream>
#include <vector>
#include <iterator>
using std::istream;
using std::vector;
template <class T>
class CTest
{
template<T>
You are declaring a friend with the same template parameter as the
CTest class.
Either
a) you remove the above template< T > and implemet the friend function
here.
b) or change typename T to something that won't clash with the Class's
template parameter.
friend istream& operator>>(istream& in, const CTest<T>& test);
};
template <class T>
istream& operator>>(istream& in, const CTest<T>& test)
That does not match the signature given above.
{
vector<vector<T> > rows;
The std::vector is a dependant type.
It depends on the template parameter provided.
typedef typename std::vector< std::vector< T > > V2Type;
V2Type rows;
the same goes for iterator. It too is a dependant type.
//does not compile
vector<vector<T> >::iterator it=rows.begin();
return in;
}
#endif /*CTEST_H_*/
#include <iostream>
#include <vector>
template < typename T >
class Test
{
template< typename P >
friend std::istream&
operator>>(std::istream& in, const Test< P >& r_test);
};
template < typename P >
std::istream&
operator>>(std::istream& in, const Test< P >& r_test)
{
typedef typename std::vector< P > VColsType;
typedef typename std::vector< std::vector< P > > VRowsType;
typedef typename VRowsType::iterator VRIter;
VRowsType rows;
VRIter it = rows.begin(); // r_test.rows.begin()
return in;
}
int main()
{
Test< double > test;
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]