473,748 Members | 4,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can't figure out syntax error with templates/member function pointers

Greetings,

I'm attempting to write my first *real* template function that also deals with
a map of strings to member function pointers that is making the syntax a little
tricky to get right.

The function in question:

36: template <typename Container,
37: typename OutputIterator,
38: typename UnaryOp>
39: void
40: transform_field s_into_matches(
41: typename Container::cons t_iterator first,
42: typename Container::cons t_iterator last,
43: OutputIterator result,
44: const fields_type& fields,
45: const std::map<std::s tring,
46: const std::string& (Container::val ue_type::*)(voi d) const > & fm,
47: UnaryOp op)
48: {
49: typedef const std::string& (Container::val ue_type::*mfp)( void) const;
50:
51: util::Regex criteria;
52: const int cflags(options: :eregex() ?
53: util::Regex::ex tended|util::Re gex::icase : util::Regex::ic ase);
54:
55: for (; first != last ; ++first)
56: {
57: fields_type::co nst_iterator f;
58: for (f = fields.begin() ; f != fields.end() ; ++f)
59: {
60: /* check if field is valid */
61: std::map<std::s tring, mfp>::const_ite rator i = fm.find(f->first);
62: if (i == fm.end())
63: throw InvalidField(f->first);
64:
65: /* it's valid, so compile regex */
66: criteria.assign (f->second, cflags);
67:
68: /* compare criteria against the return value of the
69: * Container::valu e_type member function mapped to
70: * this field. */
71: const typename Container::valu e_type& v(*first);
72: if (criteria != (v.*(i->second))())
73: break;
74:
75: /* we're on the last field, meaning all fields that came before
76: * it also matched, so save it finally. */
77: if ((f+1) == fields.end())
78: *result++ = op(*first);
79: }
80: }
81: }

For some reason I can't figure out, the compile keeps bailing on line 61 with:
"error: expected ';' before i".

I'm thinking maybe it has something to do with the lack of 'typename' when
using Container::valu e_type in the function pointer, but adding that seems to
cause another problem (maybe I'm not putting it in the right place?)

using:
typedef const std::string& (typename Container::valu e_type::*mfp)(v oid) const;

causes:
error: expected unqualified-id before ‘typename’
error: expected `)' before ‘typename’
error: expected initializer before ‘typename’

Any pointers in the right direction?

Much appreciated,
Aaron
Oct 1 '05 #1
4 2064
Aaron Walker wrote:
Greetings,

I'm attempting to write my first *real* template function that also deals with
a map of strings to member function pointers that is making the syntax a little
tricky to get right.

The function in question:

36: template <typename Container,
37: typename OutputIterator,
38: typename UnaryOp>
39: void
40: transform_field s_into_matches(
41: typename Container::cons t_iterator first,
42: typename Container::cons t_iterator last,
43: OutputIterator result,
44: const fields_type& fields,
45: const std::map<std::s tring,
46: const std::string& (Container::val ue_type::*)(voi d) const > & fm,
47: UnaryOp op)
48: {
[snip]

Any pointers in the right direction?


well I'm not sure of your actual question because that is wildly complex
syntax you have. But I can see that you are heading in the wrong direction.

Look at this simple code

#include <vector>

template <typename Container>
void f(typename Container::iter ator i)
{
typename Container::valu e_type v;
}

int main()
{
std::vector<int > i;
f(i.begin());
}

It fails to compile. The reason is that the compiler cannot work out
what Container is. The rules of C++ prevent the compiler from deducing
the template argument when the function argument type is of the form
typename T::m.

If you ever got your code to compile you would face this issue and there
isn't a solution (other than specifying the template arguments explcitily).

To pass iterators to a template function you should do the following and
use iterator_traits if you want the value type.

#include <vector>

template <typename I>
void f(I i)
{
std::iterator_t raits<I>::value _type v;
}

int main()
{
std::vector<int > i;
f(i.begin());
}

john
Oct 1 '05 #2
Aaron Walker wrote:
Greetings,

I'm attempting to write my first *real* template function that also
deals with
a map of strings to member function pointers that is making the
syntax a little tricky to get right.

The function in question:
[...]
61: std::map<std::s tring, mfp>::const_ite rator i =
fm.find(f->first);
62: if (i == fm.end())
Please don't post line numbers. Just add a comment to the line you
want to mark.
For some reason I can't figure out, the compile keeps bailing on line
61 with: "error: expected ';' before i".
Add 'typename' at the beginning:

typename std::map<...>:: const_iterator i = ...
I'm thinking maybe it has something to do with the lack of 'typename'
when
using Container::valu e_type in the function pointer, but adding that
seems to cause another problem (maybe I'm not putting it in the right
place?)


Probably.

V
Oct 1 '05 #3
John Harrison wrote:
Aaron Walker wrote:
Greetings,

I'm attempting to write my first *real* template function that also
deals with
a map of strings to member function pointers that is making the syntax
a little
tricky to get right.

The function in question:

36: template <typename Container,
37: typename OutputIterator,
38: typename UnaryOp>
39: void
40: transform_field s_into_matches(
41: typename Container::cons t_iterator first,
42: typename Container::cons t_iterator last,
43: OutputIterator result,
44: const fields_type& fields,
45: const std::map<std::s tring,
46: const std::string& (Container::val ue_type::*)(voi d)
const > & fm,
47: UnaryOp op)
48: {

[snip]

Any pointers in the right direction?


well I'm not sure of your actual question because that is wildly complex
syntax you have. But I can see that you are heading in the wrong direction.


<snip>

My question was why would this:

49: typedef const std::string& (Container::val ue_type::*mfp)( void) const;
....
61: std::map<std::s tring, mfp>::const_ite rator i = fm.find(f->first);

produce this compile failure:

61: "error: expected ';' before i".


It fails to compile. The reason is that the compiler cannot work out
what Container is. The rules of C++ prevent the compiler from deducing
the template argument when the function argument type is of the form
typename T::m.

If you ever got your code to compile you would face this issue and there
isn't a solution (other than specifying the template arguments explcitily).

To pass iterators to a template function you should do the following and
use iterator_traits if you want the value type.


<snip>

I was wondering if it'd be able to deduce the container type, but hadn't gotten
that far due to the syntax error. I didn't realize I could get the value_type
from iterator_traits .

Thanks for helping with what would probably have been my next problem :)

Aaron
Oct 1 '05 #4
Victor Bazarov wrote:

Please don't post line numbers. Just add a comment to the line you
want to mark.

Ah, ok apologies. I figured it'd make it easier on whoever was trying to help.
Will keep in mind next time.
For some reason I can't figure out, the compile keeps bailing on line
61 with: "error: expected ';' before i".

Add 'typename' at the beginning:

typename std::map<...>:: const_iterator i = ...


Yep, that does it.

Thanks,
Aaron
Oct 1 '05 #5

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

Similar topics

5
1851
by: William Payne | last post by:
Hello, consider the following two classes (parent and child): #ifndef SINGLETON_HPP #define SINGLETON_HPP #include <cstddef> /* NULL */ template <typename T> class Singleton {
3
8254
by: Patrick Guio | last post by:
Hi, I have trouble to compile the following piece of code with g++3.4 but not with earlier version // Foo.h template<typename T> class Foo { public:
22
6024
by: Ian | last post by:
The title says it all. I can see the case where a function is to be called directly from C, the name mangling will stuff this up. But I can't see a reason why a template function can't be given extern "C" linkage where it is to be assigned to a C function pointer. Ian
3
4319
by: infinity | last post by:
Hi all, Is constructor a special member function? But I don't think it is either a member function or even a special member function although it has the syntax of a function. I think it confused with a function due to the similar syntax. If i am wrong give some proof and prove you points. I think its a fundamental doubt. ~Thanks Infinity
3
2169
by: Ernesto Bascón | last post by:
Hi everybody: I have two questions: 1. I'm using opaque pointers in my classes to hide their data structures; there is a way to use opaque pointers in template classes; since the implementation and the declaration of the template class is in the same file, the use of opaque pointers does not make sense, but... what about implementation hiding for libraries? how can I make sure that the new versions of my libraries have binary...
6
3450
by: JDT | last post by:
Hi, Can we pass a member function in a class as a callback function? Someone instucted me that I can only use a static functon or a global function as a callback. Your help is appreciated. JD
6
3554
by: Kinbote | last post by:
Hi, I'm trying to make a function that opens a file, reads it in line by line, puts each line into an malloc'd array, and returns the array. I suspect I'm going about it in an atypical fashion, as I'm avoiding the use of fscanf and fgets to read in lines. I don't want to have to specify a temporary char* buffer to read in each line, and then have to concern myself with the (remote) possibility of overflows or with increasing the buffer...
2
1884
by: Eric Lilja | last post by:
As the topic says, I wanted to make a re-usable singleton class that could create pointers to objects with non-trivial constructors. I came up with this: #ifndef SINGLETON_HPP #define SINGLETON_HPP template<typename T> struct DefaultCreatorFunctor {
17
8378
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline the function if possible" (because if the compiler has the function definition available at an instantiation point, it will estimate whether to inline it or not, and do so if it estimates it would be beneficial, completely regardless of whether...
0
8991
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
9541
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
9370
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8242
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
6796
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
4602
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...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2782
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
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.