J
Jon Rea
Hello all,
Sorry if this is long, but I wanted to be specific...
Can anyone shed any light on the following errors in the context of the
following example code:
cullMethod1(); // compiles, but not what we want - we cant access
m_Exclustions
cullMethod2(); // fine but long-winded and causes many array re-allocations
cullMethod3(); // doen't compile!
i.e. How do you pass a member function to std functions.
Many thanks,
Jon Rea
////////////////////////////////////////////////////
// Begin example code ..
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
class Bond
{
public:
Bond( int _i, int _j )
{
i = _i;
j = _j;
}
Bond()
{
i = rand();
j = rand();
}
int i;
int j;
};
class MyBase
{
public:
virtual void Setup()
{
int bigNumber = 10000;
for( int i = 0; i < bigNumber; i++ )
{
m_Bonds.push_back( Bond() ); // add lots of random "bonds"
}
}
protected:
std::vector<Bond> m_Bonds;
};
bool mySpecialSelectionFunction( Bond& _b )
{
return _b.i == 47;
}
class MyDerived : public MyBase
{
public:
virtual void Setup()
{
MyBase::Setup();
m_Exclustions.push_back(Bond(43,21));
m_Exclustions.push_back(Bond(12,14));
m_Exclustions.push_back(Bond(1,41));
// Remove all instances of the definitions in m_Exclustions from the
base class list.
// Call ONE of these in real code:
cullMethod1(); // compiles, but not what we want - we cant access
m_Exclustions
cullMethod2(); // fine but long-whinded and causes many array
re-allocations
cullMethod3(); // doen't compile!
}
bool Matches( Bond& bond )
{
for( size_t k = 0; k < m_Exclustions.size(); k++ )
{
if( ( bond.i == m_Exclustions[k].i && bond.j == m_Exclustions[k].j ) ||
( bond.j == m_Exclustions[k].i && bond.i == m_Exclustions[k].j ) )
{
return true;
}
}
return false;
}
void cullMethod1()
{
m_Bonds.erase( remove_if( m_Bonds.begin(), m_Bonds.end(),
mySpecialSelectionFunction ), m_Bonds.end() );
}
void cullMethod2()
{
for( size_t i = 0; i < m_Bonds.size(); /*blank*/ )
{
if( Matches( m_Bonds ) )
{
m_Bonds.erase(m_Bonds.begin()+i);
}
else
{
i++;
}
}
}
void cullMethod3()
{
// Illegal operand on bound member function expression ...
//m_Bonds.erase( remove_if( m_Bonds.begin(), m_Bonds.end(), &Matches
), m_Bonds.end() );
// Term does not evaluare to a function taking 1 arguments ...
//m_Bonds.erase( remove_if( m_Bonds.begin(), m_Bonds.end(),
&MyDerived::Matches ), m_Bonds.end() );
// Term does not evaluare to a function taking 1 arguments ...
//m_Bonds.erase( remove_if( m_Bonds.begin(), m_Bonds.end(),
std::mem_fun_ref(&MyDerived::Matches) ), m_Bonds.end() );
}
protected:
std::vector<Bond> m_Exclustions;
};
int _tmain(int argc, _TCHAR* argv[])
{
MyDerived bob;
bob.Setup();
return 0;
}
Sorry if this is long, but I wanted to be specific...
Can anyone shed any light on the following errors in the context of the
following example code:
cullMethod1(); // compiles, but not what we want - we cant access
m_Exclustions
cullMethod2(); // fine but long-winded and causes many array re-allocations
cullMethod3(); // doen't compile!
i.e. How do you pass a member function to std functions.
Many thanks,
Jon Rea
////////////////////////////////////////////////////
// Begin example code ..
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
class Bond
{
public:
Bond( int _i, int _j )
{
i = _i;
j = _j;
}
Bond()
{
i = rand();
j = rand();
}
int i;
int j;
};
class MyBase
{
public:
virtual void Setup()
{
int bigNumber = 10000;
for( int i = 0; i < bigNumber; i++ )
{
m_Bonds.push_back( Bond() ); // add lots of random "bonds"
}
}
protected:
std::vector<Bond> m_Bonds;
};
bool mySpecialSelectionFunction( Bond& _b )
{
return _b.i == 47;
}
class MyDerived : public MyBase
{
public:
virtual void Setup()
{
MyBase::Setup();
m_Exclustions.push_back(Bond(43,21));
m_Exclustions.push_back(Bond(12,14));
m_Exclustions.push_back(Bond(1,41));
// Remove all instances of the definitions in m_Exclustions from the
base class list.
// Call ONE of these in real code:
cullMethod1(); // compiles, but not what we want - we cant access
m_Exclustions
cullMethod2(); // fine but long-whinded and causes many array
re-allocations
cullMethod3(); // doen't compile!
}
bool Matches( Bond& bond )
{
for( size_t k = 0; k < m_Exclustions.size(); k++ )
{
if( ( bond.i == m_Exclustions[k].i && bond.j == m_Exclustions[k].j ) ||
( bond.j == m_Exclustions[k].i && bond.i == m_Exclustions[k].j ) )
{
return true;
}
}
return false;
}
void cullMethod1()
{
m_Bonds.erase( remove_if( m_Bonds.begin(), m_Bonds.end(),
mySpecialSelectionFunction ), m_Bonds.end() );
}
void cullMethod2()
{
for( size_t i = 0; i < m_Bonds.size(); /*blank*/ )
{
if( Matches( m_Bonds ) )
{
m_Bonds.erase(m_Bonds.begin()+i);
}
else
{
i++;
}
}
}
void cullMethod3()
{
// Illegal operand on bound member function expression ...
//m_Bonds.erase( remove_if( m_Bonds.begin(), m_Bonds.end(), &Matches
), m_Bonds.end() );
// Term does not evaluare to a function taking 1 arguments ...
//m_Bonds.erase( remove_if( m_Bonds.begin(), m_Bonds.end(),
&MyDerived::Matches ), m_Bonds.end() );
// Term does not evaluare to a function taking 1 arguments ...
//m_Bonds.erase( remove_if( m_Bonds.begin(), m_Bonds.end(),
std::mem_fun_ref(&MyDerived::Matches) ), m_Bonds.end() );
}
protected:
std::vector<Bond> m_Exclustions;
};
int _tmain(int argc, _TCHAR* argv[])
{
MyDerived bob;
bob.Setup();
return 0;
}