little problem with find stl's algorithm

  • Thread starter Thread starter Jacobo Rodriguez Villar
  • Start date Start date
J

Jacobo Rodriguez Villar

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
 
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.
 
Jacobo said:
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
 
Yes, GLhandleARB is a typedef of int, I'll try use it into a struct,
many thanks.
Jacobo said:
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
 
Back
Top