473,839 Members | 1,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<Progr am *> m_programs

in this method:
Program *GLSLManager::G etProgram(GLhan dleARB id)
{
std::list<Progr am *>::iterator result;
result = find(m_programs .begin(), m_programs.end( ), id);
if(result!=m_pr ograms.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==(Prog ram *program, GLhandleARB id); /*it is not a member
of Program class*/

[implementation]
int operator==(Prog ram *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 977
> I'm trying to use the find algo from the stl library, over a list of
pointers:

std::list<Progr am *> m_programs

in this method:
Program *GLSLManager::G etProgram(GLhan dleARB id)
{
std::list<Progr am *>::iterator result;
result = find(m_programs .begin(), m_programs.end( ), id);
if(result!=m_pr ograms.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==(Prog ram *program, GLhandleARB id); /*it is not a member
of Program class*/

[implementation]
int operator==(Prog ram *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******@nester ovsky-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<Progr am *> m_programs

in this method:
Program *GLSLManager::G etProgram(GLhan dleARB id)
{
std::list<Progr am *>::iterator result;
result = find(m_programs .begin(), m_programs.end( ), id);
if(result!=m_pr ograms.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==(Prog ram *program, GLhandleARB id); /*it is not a member
of Program class*/

[implementation]
int operator==(Prog ram *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 ProgramHandleCh ecker
{
GLhandleARB id;
ProgramHandleCh ecker(GLhandleA RB id):id(id){}
bool operator()(Prog ram* p) const
{
return p != 0 && id == p->GetHandle();
}
};

//...
result = find_if(m_progr ams.begin(), m_programs.end( ),
ProgramHandleCh ecker(id));

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

result = find_if(m_progr ams.begin(), m_programs.end( ),
bind<bool>(std: :equals<GLhandl eARB>(), 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<Progr am *> m_programs

in this method:
Program *GLSLManager::G etProgram(GLhan dleARB id)
{
std::list<Progr am *>::iterator result;
result = find(m_programs .begin(), m_programs.end( ), id);
if(result!=m_pr ograms.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==(Prog ram *program, GLhandleARB id); /*it is not a
member of Program class*/

[implementation]
int operator==(Prog ram *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 ProgramHandleCh ecker
{
GLhandleARB id;
ProgramHandleCh ecker(GLhandleA RB id):id(id){}
bool operator()(Prog ram* p) const
{
return p != 0 && id == p->GetHandle();
}
};

//...
result = find_if(m_progr ams.begin(), m_programs.end( ),
ProgramHandleCh ecker(id));

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

result = find_if(m_progr ams.begin(), m_programs.end( ),
bind<bool>(std: :equals<GLhandl eARB>(), 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
2628
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 hashtable is to store HashEntry objects, which contain information about chess positions. A BitBoard is just an __int64, and this is used to determine the hash key. I have not shown the definition of class ChessPosition, but all you need to know is...
3
4447
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*> and use stl find string xyz;
10
1449
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., MyClass::is_true() returns true). bool found = false; vector<MyClass>::size_type i; for (i=0; i < objects.size(); i++) { MyClass object = objects; if (object.is_true())
0
1476
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 methods. I don't show the Integer class because it will not add any information to my problem. Main is using some STL function. In main you put in nodes in the STL list and you can display the STL list and other things such as erase some value from...
7
1817
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. I've a program which is using self-written double-linked lists as a data structure. The template list consists of list elements and the list itself linking the list elements.
5
2581
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 it returns the iterator to the last element in the range, not to the last element in the container, to the last element in the range. That being said, how can we tell if find() has been successful in finding the element we need? Its easy when we...
4
4502
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> #include <ext/numeric> #include <algorithm> #include <ext/algorithm>
4
2214
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 implement all three techniques as programs. In these programs, as well as solving the problem, you will also measure how long the program takes to run. The programs are worth 80% of the total mark. The final 20% of the marks are awarded for a...
7
2611
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. The program is as follows: #include <iostream> //#include <algorithm <-- I don't have to include <algorithmin
0
9855
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
9697
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
10909
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
10588
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
10650
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
9426
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...
0
7019
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();...
0
5867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4492
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

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.