473,396 Members | 1,843 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,396 software developers and data experts.

little problem with find stl's algorithm

Hello,

I'm trying to use the find algo from the stl library, over a list of
pointers:

std::list<Program *> m_programs

in this method:
Program *GLSLManager::GetProgram(GLhandleARB id)
{
std::list<Program *>::iterator result;
result = find(m_programs.begin(), m_programs.end(), id);
if(result!=m_programs.end())
return *result;
else
return NULL;
}

As far I know I must implement the operator== between the types
GLhandleARB (it is a typedef of ints) and Program * pointers. I do this
by this way:

int operator==(Program *program, GLhandleARB id); /*it is not a member
of Program class*/

[implementation]
int operator==(Program *program, GLhandleARB id)
{
return program->GetHandle() == id;
}

but I'm having errors with this:
error C2803: 'operator == must have at least a formal parameter of class
type'

Anyone can help me?

thanks in advance
--
Jacobo Rodríguez Villar

TyphoonLabs Lead Programmer

http://www.typhoonlabs.com
Nov 17 '05 #1
3 963
> I'm trying to use the find algo from the stl library, over a list of
pointers:

std::list<Program *> m_programs

in this method:
Program *GLSLManager::GetProgram(GLhandleARB id)
{
std::list<Program *>::iterator result;
result = find(m_programs.begin(), m_programs.end(), id);
if(result!=m_programs.end())
return *result;
else
return NULL;
}

As far I know I must implement the operator== between the types
GLhandleARB (it is a typedef of ints) and Program * pointers. I do this
by this way:

int operator==(Program *program, GLhandleARB id); /*it is not a member
of Program class*/

[implementation]
int operator==(Program *program, GLhandleARB id)
{
return program->GetHandle() == id;
}

but I'm having errors with this:
error C2803: 'operator == must have at least a formal parameter of class
type'

Anyone can help me?

thanks in advance


Your GLhandleARB is probably typedef of some primitive type or pointer. If
you could declare GLhandleARB as a struct, class, or enum you would
succeeded in operator== overloading.

--
Vladimir Nesterovsky
e-mail: vl******@nesterovsky-bros.com
home: www.nesterovsky-bros.com
Nov 17 '05 #2
Jacobo Rodriguez Villar wrote:
Hello,

I'm trying to use the find algo from the stl library, over a list of
pointers:

std::list<Program *> m_programs

in this method:
Program *GLSLManager::GetProgram(GLhandleARB id)
{
std::list<Program *>::iterator result;
result = find(m_programs.begin(), m_programs.end(), id);
if(result!=m_programs.end())
return *result;
else
return NULL;
}

As far I know I must implement the operator== between the types
GLhandleARB (it is a typedef of ints) and Program * pointers. I do this
by this way:

int operator==(Program *program, GLhandleARB id); /*it is not a member
of Program class*/

[implementation]
int operator==(Program *program, GLhandleARB id)
{
return program->GetHandle() == id;
}

but I'm having errors with this:
error C2803: 'operator == must have at least a formal parameter of class
type'

Anyone can help me?


You can't overload operators for primitive types (I assume GLhandleARG
is just a typedef for a primitive type). Instead, you should use a
functor and find_if. e.g.

struct ProgramHandleChecker
{
GLhandleARB id;
ProgramHandleChecker(GLhandleARB id):id(id){}
bool operator()(Program* p) const
{
return p != 0 && id == p->GetHandle();
}
};

//...
result = find_if(m_programs.begin(), m_programs.end(),
ProgramHandleChecker(id));

If you use boost::bind (soon to be semi-standard as std::tr1::bind), you
can do:

result = find_if(m_programs.begin(), m_programs.end(),
bind<bool>(std::equals<GLhandleARB>(), bind(&Program::GetHandle, _1), id));

which saves the extra functor, but is more complex.

Tom
Nov 17 '05 #3

Yes, GLhandleARB is a typedef of int, I'll try use it into a struct,
many thanks.
Jacobo Rodriguez Villar wrote:
Hello,

I'm trying to use the find algo from the stl library, over a list of
pointers:

std::list<Program *> m_programs

in this method:
Program *GLSLManager::GetProgram(GLhandleARB id)
{
std::list<Program *>::iterator result;
result = find(m_programs.begin(), m_programs.end(), id);
if(result!=m_programs.end())
return *result;
else
return NULL;
}

As far I know I must implement the operator== between the types
GLhandleARB (it is a typedef of ints) and Program * pointers. I do
this by this way:

int operator==(Program *program, GLhandleARB id); /*it is not a
member of Program class*/

[implementation]
int operator==(Program *program, GLhandleARB id)
{
return program->GetHandle() == id;
}

but I'm having errors with this:
error C2803: 'operator == must have at least a formal parameter of
class type'

Anyone can help me?

You can't overload operators for primitive types (I assume GLhandleARG
is just a typedef for a primitive type). Instead, you should use a
functor and find_if. e.g.

struct ProgramHandleChecker
{
GLhandleARB id;
ProgramHandleChecker(GLhandleARB id):id(id){}
bool operator()(Program* p) const
{
return p != 0 && id == p->GetHandle();
}
};

//...
result = find_if(m_programs.begin(), m_programs.end(),
ProgramHandleChecker(id));

If you use boost::bind (soon to be semi-standard as std::tr1::bind), you
can do:

result = find_if(m_programs.begin(), m_programs.end(),
bind<bool>(std::equals<GLhandleARB>(), bind(&Program::GetHandle, _1), id));

which saves the extra functor, but is more complex.

Tom

--
Jacobo Rodríguez Villar

TyphoonLabs Lead Programmer

http://www.typhoonlabs.com
Nov 17 '05 #4

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

Similar topics

7
by: JN | last post by:
Hello. Sorry about the length of this post. I am trying to implement a hash table for my chess program, and decided to try using the VC++ . NET (Dinkumware) version of hash_map. The role of the...
3
by: Prakash Bande | last post by:
Hi, I have bool operator == (xx* obj, const string st). I have declared it as friend of class xx. I am now able to do this: xx ox; string st; if (&ox == st) { } But, when I have a vector<xx*>...
10
by: Jason Heyes | last post by:
How do I rewrite the following code so that I am taking advantage of the STL? The purpose of the code is to determine whether a vector contains any objects of MyClass that are "true" (i.e.,...
0
by: Tony Johansson | last post by:
Hello! I have two classes called Handle which is a template class and a class Integer which is not a template class. The Integer class is just a wrapper class for a primitive int with some...
7
by: Christian Christmann | last post by:
Hi, in the past I always appreciated your help and hope that you also can help me this time. I've spent many many hours but still can't solve the problem by myself and you are my last hope. ...
5
by: Draw | last post by:
Hi All, Just a thought, about the find() algorithm in the C++ STL. I read that the find algorithm can take a range of iterators. If it does not find the element it is looking for in that range...
4
by: fightwater | last post by:
i wrote a program which uses the stl compose1 functor but the compiler seems cannot find it here is the code ////////////////////////////////////////// #include <vector> #include <numeric>...
4
by: naknak4 | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
7
by: Wei | last post by:
Hi all, I found out I can use the max function which is defined in STL <algorithmwithout including this header in my program. The compilers I used are GNU g++ 3.4.4 and Visual Studio C++ 2005. ...
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:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
0
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,...

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.