Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / December 2004

Tip: Looking for answers? Try searching our database.

little problem with find stl's algorithm

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jacobo Rodriguez Villar - 20 Dec 2004 13:20 GMT
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

Signature

Jacobo Rodríguez Villar

TyphoonLabs Lead Programmer

http://www.typhoonlabs.com

Vladimir Nesterovsky - 20 Dec 2004 14:49 GMT
> I'm trying to use the find algo from the stl library, over a list of
> pointers:
[quoted text clipped - 32 lines]
>
> 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.

Signature

Vladimir Nesterovsky
e-mail: vladimir@nesterovsky-bros.com
home: www.nesterovsky-bros.com

Tom Widmer - 20 Dec 2004 17:18 GMT
> Hello,
>
[quoted text clipped - 32 lines]
>
> 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 Rodriguez Villar - 20 Dec 2004 21:19 GMT
Yes, GLhandleARB is a typedef of int, I'll try use it into a struct,
many thanks.

>> Hello,
>>
[quoted text clipped - 60 lines]
>
> Tom

Signature

Jacobo Rodríguez Villar

TyphoonLabs Lead Programmer

http://www.typhoonlabs.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.