473,748 Members | 5,242 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating unary function by binding-2nd to class member

Hi!

I have the code like this
(obvious things like ctor/dtor removed)

typedef struct _NODE
{
int val;
int index;
} Node;

struct A:
{
std::vector<Nod e*Nodes;

bool EqIndex(const Node* ptr, int idx) const
{ return ptr->index == idx; };

int foo();
}

I would like to write like this:

int A::foo()
{
...
std::find_if(No des.begin(), Nodes.end(), bind2nd(&A:EqIn dex, 5));
...
}

but got these errors:

1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(303)
: error C2825: '_Fn2': must be a class or namespace when followed by '::'
1 main.cpp(361) : see reference to class template instantiation
'std::binder2nd <_Fn2>' being compiled
1 with
1 [
1 _Fn2=bool (__thiscall A::* )(const Node*,unsigned long)
1 ]
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(303)
: error C2039: 'first_argument _type' : is not a member of '`global
namespace''
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(303)
: error C2146: syntax error : missing ',' before identifier
'first_argument _type'
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(303)
: error C2065: 'first_argument _type' : undeclared identifier
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(304)
: error C2825: '_Fn2': must be a class or namespace when followed by '::'
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(304)
: error C2039: 'result_type' : is not a member of '`global namespace''
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(304)
: error C2146: syntax error : missing ',' before identifier 'result_type'
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(304)
: error C2065: 'result_type' : undeclared identifier
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(305)
: error C2955: 'std::unary_fun ction' : use of class template requires
template argument list
1 c:\program files\microsoft visual studio
8\vc\include\fu nctional(21) : see declaration of 'std::unary_fun ction'
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(307)
: error C2825: '_Fn2': must be a class or namespace when followed by '::'
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(307)
: error C2143: syntax error : missing ',' before '`global
namespace'::fir st_argument_typ e'
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(308)
: error C2825: '_Fn2': must be a class or namespace when followed by '::'
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(308)
: error C2143: syntax error : missing ',' before '`global
namespace'::res ult_type'
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(309)
: error C2955: 'std::unary_fun ction' : use of class template requires
template argument list
1 c:\program files\microsoft visual studio
8\vc\include\fu nctional(21) : see declaration of 'std::unary_fun ction'
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(310)
: error C2955: 'std::unary_fun ction' : use of class template requires
template argument list
1 c:\program files\microsoft visual studio
8\vc\include\fu nctional(21) : see declaration of 'std::unary_fun ction'
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(312)
: error C2825: '_Fn2': must be a class or namespace when followed by '::'
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(312)
: error C2825: '_Fn2': must be a class or namespace when followed by '::'
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(312)
: error C2039: 'second_argumen t_type' : is not a member of '`global
namespace''
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(312)
: error C2143: syntax error : missing ',' before '&'
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(330)
: error C2825: '_Fn2': must be a class or namespace when followed by '::'
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(330)
: error C2039: 'second_argumen t_type' : is not a member of '`global
namespace''
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(330)
: error C2146: syntax error : missing ';' before identifier 'value'
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(330)
: error C4430: missing type specifier - int assumed. Note: C++ does not
support default-int

: error C2664: 'std::find_if' : cannot convert parameter 3 from
'std::binder2nd <_Fn2>' to 'std::binder2nd <_Fn2>'
1 with
1 [
1 _Fn2=bool (__thiscall A::* )(const Node *,unsigned long)
1 ]
1 Cannot copy construct class 'std::binder2nd <_Fn2>' due to
ambiguous copy constructors or no available copy constructor
1 with
1 [
1 _Fn2=bool (__thiscall A::* )(const Node *,unsigned long)
1 ]

What I do wrong?
Using MS Visual Studio 8.

If I write a functor derived from unary_function it works but I'd prefer
using single member function...

Thanks,
-Marcin
Feb 20 '07 #1
5 5482
* Marcin Gil:
Hi!

I have the code like this
(obvious things like ctor/dtor removed)

typedef struct
'typedef' of 'struct' is a C'ism: don't.

_NODE
Names starting with underscore followed by uppercase letter are reserved
to the implemention, not to be used by you.

Anyway, use all uppercase names for macros and macros only.

{
int val;
int index;
} Node;

struct A:
Syntax error.

{
std::vector<Nod e*Nodes;

bool EqIndex(const Node* ptr, int idx) const
{ return ptr->index == idx; };
Since this function doesn't access anything of the object it's called
on, it doesn't seem likely that it should be a non-static member function.

>
int foo();
}
Missing semicolon.
I would like to write like this:

int A::foo()
{
...
std::find_if(No des.begin(), Nodes.end(), bind2nd(&A:EqIn dex, 5));
...
}
Well, first fix the code, then try again.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 20 '07 #2
Alf P. Steinbach wrote:
Well, first fix the code, then try again.
I was writing it from my head.
But I assume it was disrespectful to post non-working code..

Here's the more proper one:

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

struct Node {
int val;
int index;
};

struct A
{
std::vector<Nod e*Nodes;

A() {};

bool EqIndex(const Node* ptr, int idx) const
{
return (ptr->index == idx);
};

int foo()
{
std::find_if(No des.begin(), Nodes.end(), std::bind2nd(&A ::EqIndex, 5));
return 0;
};
};

int main(int argc, char* argv[])
{
A a;
a.foo();

return 0;
}
The problem is in the std::find_if(.. .bind2nd...).
My guess is that it assumes that given type is only class/struct.

I've also tried to use std::mem_fun - no effect.
Do I have to write a functor to use such functionality?

Thanks,
-Marcin
Feb 20 '07 #3
* Marcin Gil:
Alf P. Steinbach wrote:
>Well, first fix the code, then try again.
I was writing it from my head.
But I assume it was disrespectful to post non-working code..

Here's the more proper one:

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

struct Node {
int val;
int index;
};

struct A
{
std::vector<Nod e*Nodes;

A() {};

bool EqIndex(const Node* ptr, int idx) const
{
return (ptr->index == idx);
};

int foo()
{
std::find_if(No des.begin(), Nodes.end(),
std::bind2nd(&A ::EqIndex, 5));
return 0;
};
};

int main(int argc, char* argv[])
{
A a;
a.foo();

return 0;
}
The problem is in the std::find_if(.. .bind2nd...).
My guess is that it assumes that given type is only class/struct.

I've also tried to use std::mem_fun - no effect.
Do I have to write a functor to use such functionality?
No, but as I mentioned in the first reply EqIndex isn't naturally a
non-static member function and won't work directly if you insist.

Having fixed that you also need to wrap the function address via
std::ptr_fun.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 20 '07 #4
Alf P. Steinbach wrote:
>The problem is in the std::find_if(.. .bind2nd...).
My guess is that it assumes that given type is only class/struct.

I've also tried to use std::mem_fun - no effect.
Do I have to write a functor to use such functionality?

No, but as I mentioned in the first reply EqIndex isn't naturally a
non-static member function and won't work directly if you insist.

Having fixed that you also need to wrap the function address via
std::ptr_fun.
Thanks!

I've got this working:

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

struct Node {
int val;
int index;
};

struct A
{
std::vector<Nod e*Nodes;

A() {};

static bool EqIndex(const Node* ptr, int idx)
{
return ptr->index == idx;
};

int foo()
{
std::find_if(No des.begin(), Nodes.end(),
std::bind2nd(st d::ptr_fun(A::E qIndex), 5));
return 0;
};
};

int main()
{
A a;
a.foo();

return 0;
}
Feb 20 '07 #5
Marcin Gil wrote:
Alf P. Steinbach wrote:
Well, first fix the code, then try again.
I was writing it from my head.
But I assume it was disrespectful to post non-working code..
It's perfectly fine to post non-working code. What isn't fine is
posting code that isn't what you're problems with.

When your car is having a problem, do you take your neighbor's car into
the shop? I hope not.

Almost every newsreader supports pasting text. Copy the exact code that
giving you trouble and paste that into the message. Then you know
without a doubt that you haven't introduced typing errors.

You may want to review the FAQ:

<http://www.parashift.c om/c++-faq-lite/how-to-post.html>


Brian
Feb 20 '07 #6

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

Similar topics

3
3904
by: Carlos Ribeiro | last post by:
I was checking the Prolog recipe in the Cookbook: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303057 It's a clever implementation that explores some aspects of Python that I wasn't aware of. One of them is the unary plus operator, that calls the __pos__ method. It's something that can be highly useful in my own experiments with the use of classes as a tool for generic declarative descriptions of objects -- UI forms,...
20
3125
by: svend | last post by:
I'm messing with some code here... Lets say I have this array: a1 = ; And I apply slice(0) on it, to create a copy: a2 = a1.slice(0); But this isn't a true copy. If I go a1 = 42, and then alert(a2) I will see 42 there too. I'm doubting, but I was
2
2272
by: Fred Ma | last post by:
Hello, I have a random generator that takes a scaling factor as an argument. Since it takes one argument, it is not a generator in the sense defined by SGI's online STL documentation. I'd like to use this as a generator in the STL algorithm "generate". I know I have to change random generator from its current implementation to make it adaptable. But unlike the bind1st adapter, there isn't any premade function adapter that reduces...
4
1601
by: Polvere | last post by:
Hi, I need to create a txt file every time the loop of a for cicle is executed, and these files have to be named as the current for variable. Something like this (in pseudo code, I know that's fully wrong): for(a=0; a<VAR; a++) { fopen (a.txt); fprintf ( whatever);
10
4656
by: SpOiLeR | last post by:
I have function bool IsGood (const std::string& sr); I want to use that function in std::not1 STL functor. I tried this: not1(IsGood) /* error : 'std::unary_negate<_Fn1> std::not1(const _Fn1 &)' : could not deduce template argument for 'overloaded function type' from 'overloaded
17
3501
by: Anoob | last post by:
Can we consider () unary operator when calling a function, in exps eq. f(), ( 1 + 2). But when we call function f( 10 ) it is a binary operator. Even if we pass f( 10, 20) as we are using , operator there one operand will be there for ()? And Unary operators like !, +, -, ~, ... are said to be having associativity right to left which means that we can write, (but not allowed) 1. 2! 2. !2
2
4199
by: pshvarts | last post by:
(I'm new in SOAP) I get some wsdl file (from apache service ). I tried creating SOAP client with .NET - trying to add Web Reference and get error like: "Custom tool error: Unable to import WebService/Schema. Unable to import binding..." I thought may be wsdl file is not good enough (it was created with qsoap toolkit), so I paste-copy sample from http://www.w3.org/TR/2001/NOTE-wsdl-20010315#_wsdl (will paste below) and receive same error...
3
1927
by: patrickkellogg | last post by:
I have this code when you click the buttom is suppose to add a job history. it works with firefox, opera, but not ie. (please note - new entries don't have all the elements in them yet, but enough to get the idea). Here is the code: ----------------------------------------------------------------- <html>
28
5079
by: dspfun | last post by:
I'm trying to get a good understanding of how unary operators work and have some questions about the following test snippets. int *p; ~!&*++p--; It doesn't compile, why? The problem seems to be the ++, the compiler says: "Error: invalid l-value in increment". int i = 10; ~!*&i++;
16
4715
by: JoseMariaSola | last post by:
How may operators and operands does (typename) expression has? I'd say one operator, the cast operator, and two operands: typename and expression. But everywhere I read, cast is categorized as an unary operator. Why is that? Is it just a syntax cotegory? Thanks.
0
8987
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
8826
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
9366
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...
1
9316
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8239
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
6793
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
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3303
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
3
2211
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.