 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Bart Decuypere Guest
|
Posted: Sat Nov 18, 2006 6:30 am Post subject: Problem with templates and iterators |
|
|
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;
//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>
friend istream& operator>>(istream& in, const CTest<T>& test);
};
template <class T>
istream& operator>>(istream& in, const CTest<T>& test)
{
vector<vector<T> > rows;
//does not compile
vector<vector<T> >::iterator it=rows.begin();
return in;
}
#endif /*CTEST_H_*/
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
nevergone Guest
|
Posted: Sat Nov 18, 2006 10:10 am Post subject: Re: Problem with templates and iterators |
|
|
template <class T>
class CTest
{
template<T>
friend istream& operator>>(istream& in, const CTest<T>& test);
};
template <class T>
istream& operator>>(istream& in, const CTest<T>& test)
{
vector<vector<T> > rows;
vector<vector<T> >::iterator it=rows.begin();
return in;
}
my answer:
template <class T>
class CTest
{
template <U>
friend istream&operator>>(istream &in,const CTest<U>&test);
};
template <class T>
istream& operator>>(istream& in, const CTest<T>& test)
{
vector<vector<T> > rows;
vector<vector<T> >::iterator it=rows.begin();
return in;
}
or:
template <class T>
class CTest;
template <class T>
istream &operator>>(istream &in,const CTest<T>&test);
template<class T>
class CTest
{
friend istream& operator>> <> (istream& in, const CTest<T>& test);
};
template <class T>
istream& operator>>(istream& in, const CTest<T>& test)
{
vector<vector<T> > rows;
vector<vector<T> >::iterator it=rows.begin();
return in;
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Ivan Novick Guest
|
Posted: Sat Nov 18, 2006 10:10 am Post subject: Re: Problem with templates and iterators |
|
|
| Quote: | 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).
template <class T
istream& operator>>(istream& in, const CTest<T>& test)
{
vector<vector<T> > rows;
//does not compile
vector<vector<T> >::iterator it=rows.begin();
return in;
}
|
The reason is because on CTest.h this code:
vector<vector<T> >::iterator
Is the type of variable "it".
Because it is a type inside a template you must precede it with the
keyword typename.
Try changing that line to:
typename vector<vector<T> >::iterator it=rows.begin();
Ivan
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Bob Guest
|
Posted: Sat Nov 18, 2006 10:10 am Post subject: Re: Problem with templates and iterators |
|
|
Bart Decuypere wrote:
| Quote: | 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"
|
[Code snipped].
The short answer is because the compiler needs a bit of help when
working with templates.
In your header file ....
| Quote: |
template <class T
istream& operator>>(istream& in, const CTest<T>& test)
{
vector<vector<T> > rows;
//does not compile
vector<vector<T> >::iterator it=rows.begin();
|
the last line needs to be replaced with
typename vector<vector<T> >::iterator it=rows.begin();
Unrelated to your problem, but it is usually a VERY good idea
to avoid having "using namespace" directives in any header files.
While that means you have to prefix things in the header with
the "std::" prefix (if you're using namespace std) it prevents
all sorts of ambiguity problems in code that #include's your header
and employs multiple namespaces.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Chris Uzdavinis Guest
|
Posted: Sat Nov 18, 2006 10:10 am Post subject: Re: Problem with templates and iterators |
|
|
Bart Decuypere wrote:
| Quote: | 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).
template <class T
istream& operator>>(istream& in, const CTest<T>& test)
{
vector<vector<T> > rows;
//does not compile
vector<vector<T> >::iterator it=rows.begin();
return in;
}
|
Since T is a template argument to your function, and the actual type of
your vector depends on that T, to access its type members you must
prefix it with "typename". Example:
typename vector<vector<T> >::iterator it=rows.begin();
Hope this helps.
Chris
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Salt_Peter Guest
|
Posted: Sat Nov 18, 2006 10:10 am Post subject: Re: Problem with templates and iterators |
|
|
Bart Decuypere wrote:
| Quote: | 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.
| Quote: |
//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.
| Quote: |
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.
| Quote: |
{
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.
| Quote: |
//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! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|