473,385 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Question about template error message

I can't understand why I'm getting an error message from code like
this...

#include <vector>

template <class T>
struct Foo
{
typedef int (T::*Method)( int i );
typedef int (*Func)( int i );

std::vector< Method > mMethodVec;

void Bar()
{
std::vector< Func >::iterator i;
std::vector< Method >::iterator j;
}
};

The error is on the line declaring the iterator j. For some reason the
compiler expects a semicolon before that. There's no error on the mere
declaration of a vector of method pointers, and if I can create the
vector, I ought to be able to have an iterator for it.
Jul 19 '05 #1
3 1364
"James W. Walker" <os*******@jwwalker.com.invalid> wrote in message
news:080920030853086449%os*******@jwwalker.com.inv alid...
I can't understand why I'm getting an error message from code like
this...

#include <vector>

template <class T>
struct Foo
{
typedef int (T::*Method)( int i );
typedef int (*Func)( int i );

std::vector< Method > mMethodVec;

void Bar()
{
std::vector< Func >::iterator i;
std::vector< Method >::iterator j;
}
};

The error is on the line declaring the iterator j. For some reason the
compiler expects a semicolon before that. There's no error on the mere
declaration of a vector of method pointers, and if I can create the
vector, I ought to be able to have an iterator for it.


I think it should be:

typename std::vector<Method>::iterator j;

The point being, of course, that it's a dependent name. It's equivalent to
doing:

typename std::vector<int (T::*)(int)>::iterator j;

when the need for typename becomes clearer. The latter fails to compile
under g++ for some reason (even though the corrected version of what you
wrote compiles fine). Both compile without errors under Comeau C++.

HTH,

Stuart.
Jul 19 '05 #2
In article <3f*********************@news.dial.pipex.com>, Stuart
Golodetz <sg*******@dial.pipex.com> wrote:
I think it should be:

typename std::vector<Method>::iterator j;
Thanks! That works.
The point being, of course, that it's a dependent name. It's equivalent to
doing:

typename std::vector<int (T::*)(int)>::iterator j;

when the need for typename becomes clearer.


Umm, I can't say that makes it clearer to me. What would it be other
than a type name, that would cause the compiler to need a hint?
Jul 19 '05 #3
On Mon, 08 Sep 2003 16:25:01 GMT, "James W. Walker"
<os*******@jwwalker.com.invalid> wrote:
In article <3f*********************@news.dial.pipex.com>, Stuart
Golodetz <sg*******@dial.pipex.com> wrote:
I think it should be:

typename std::vector<Method>::iterator j;


Thanks! That works.
The point being, of course, that it's a dependent name. It's equivalent to
doing:

typename std::vector<int (T::*)(int)>::iterator j;

when the need for typename becomes clearer.


Umm, I can't say that makes it clearer to me. What would it be other
than a type name, that would cause the compiler to need a hint?


A static member or member function. If you don't use "typename", the
compiler does assume it is one of those, hence the error. This relates
to the fact that when the code in question is parsed, the compiler
doesn't know about specific specializations of vector, which
theoretically might have a static member variable called iterator.

Tom
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Dave Rahardja | last post by:
OK, so I've gotten into a philosophical disagreement with my colleague at work. He is a proponent of the Template Method pattern, i.e.: class foo { public: void bar() { do_bar(); } protected:...
6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
14
by: LRS Kumar | last post by:
The following code - from "C++ Templates: The Complete Guide" by Vandevoorde/Josuttis - seems to compile with Borland C++. However it fails to compile with other compilers - Comeau Online and g++....
5
by: derek | last post by:
(Message also posted to comp.std.c++) I have just been experimenting with g++ 3.4 and code that I thought was compliant has raised compiler problems. Basically, if we have the following code: ...
5
by: ESOJAY | last post by:
My native language is c, but I have the unpleasant task of porting a C++ code from SGI MIPS to Linux Opteron, I have no help from the author of the code. One of several problems is that my g++...
5
by: Arne Claus | last post by:
Hi. I ran into the problem described here http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12, but the solutions presented there only helped to fix a part of my problems. I use the...
18
by: Amadeus W. M. | last post by:
I'm trying to read a whole file as a single string, using the getline() function, as in the example below. I can't tell what I'm doing wrong. Tried g++ 3.2, 3.4 and 4.0. Thanks! #include...
5
by: Tony Johansson | last post by:
Hello experts! I have two class template below with names Array and CheckedArray. The class template CheckedArray is derived from the class template Array which is the base class This program...
2
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const...
3
by: stdlib99 | last post by:
Hi, I have a simple question regarding templates and meta programming. I am going to try and work my way through the C++ Template Metaprogramming, a book by David Abrahams and Aleksey...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.