473,653 Members | 3,000 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1376
"James W. Walker" <os*******@jwwa lker.com.invali d> wrote in message
news:0809200308 53086449%os**** ***@jwwalker.co m.invalid...
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<Met hod>::iterator j;

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

typename std::vector<int (T::*)(int)>::i terator 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<Met hod>::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)>::i terator 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*******@jwwa lker.com.invali d> wrote:
In article <3f************ *********@news. dial.pipex.com> , Stuart
Golodetz <sg*******@dial .pipex.com> wrote:
I think it should be:

typename std::vector<Met hod>::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)>::i terator 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
3615
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: virtual void do_bar() {} };
6
3325
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 my particular application. One of the advantages is that the _compiler_ can force inner matrix dimensions used in multiplication to agree. A _complie-time_ error will be triggered if you write A * B and the number of coluns in A does not equal the...
14
1644
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++. Any idea why? template<typename T> class Shell { public: template<int N> class In {
5
1501
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: #include <iostream> using std::cout;
5
1482
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++ (3.3.3) compiler balks at expressions like <int>: for example if Scan is a well defined object, the compiler balks at Scan<int> ::~Scan; the error message is 'undefined reference to Scan<int>'. Is <int> a cast operator? What can be done to avoid...
5
1306
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 following construct template <class T> class A { public: typedef smart_ptr<A<T> > aPtr;
18
8244
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 <iostream> #include <fstream> #include <cstdlib> #include <string>
5
2079
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 works fine but there in one thing that I'm unsure about and that is the inheritance statement. What difference is it if I have this construction class CheckedArray : public Array<T>
2
2171
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 Debug_Level ddl;
3
1847
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 Gurtovoy. I’m not doing this because I want to be a Meta Programming guru (because a lot of that stuff looks too crazy for use in the real world). Rather I want to learn heavyweight templates and this is the only
0
8370
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8283
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8811
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7302
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4147
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2707
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1591
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.