a problem with for_each algorithm 
Author Message
 a problem with for_each algorithm

Hi friends,

I'm writing an specialized set class for my project and I wanna add a member
function to this class which would execute a unique function for each
element so I got that the best choice would be for_each algorithm so I
defined the following member function for my set class:

#include <set>
#include <algorithm>
#include "sync.h"

namespace SS7
{
template <class T> class CTimerSet
{
public: //Public functions
 typedef std::set<T> SET;
....
.....
 template <class Operation> void For_each(Operation Op)
 {
  m_CS.Lock();
  for_each(m_Collection.begin(), m_Collection.end(), Op);
  m_CS.Unlock();
 }

private: // Private members
 SET m_Collection;
 CCriticalSection m_CS;

Quote:
};
}

then in my main file I defined something like the snipped code below and
used my CTimerSet like this:

class PrintInt
{
public:
 void operator() (int elem) const
 {
  std::cout << elem << ' ';
 }

Quote:
};

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 SS7::CTimerSet<int> MySet;
 MySet.Insert(10);
 MySet.Insert(20);
 MySet.For_each(PrintInt());

 return 0;

Quote:
}

The PrintInt function object is the one I passed to CTimerSet::For_each
function. No I get the following error from the compiler :

c:\program files\microsoft visual studio\myprojects\test\safeset.h(46) :
error C2065: 'for_each' : undeclared identifier
        C:\Program Files\Microsoft Visual
Studio\MyProjects\Test\Test.cpp(114) : see reference to function template
instantiation 'void __thiscall SS7::CTimerSet<int>::For_each(class
PrintInt)' being compiled
Error executing cl.exe.

Anyone can help me and find where the problem is???

TIA,
--
M. Shokuie Nia,
MSVC++, SQL, ADO programmer



Thu, 20 May 2004 15:54:57 GMT  
 a problem with for_each algorithm
i can't see a "using namespace std" in your code. therefor i suppose a call
to "std::for_each" instead of "for_each" should do the job.

Olaf



Thu, 20 May 2004 17:19:07 GMT  
 a problem with for_each algorithm
Hey Olaf,

Thank you very much indeed. You see how very simple problem pulling my leg I
think after hours of programming mind cant concentrate enough on the
problem.

Anyhow, Thanx again.
--
M. Shokuie Nia,
MSVC++, SQL, ADO programmer


Quote:
> i can't see a "using namespace std" in your code. therefor i suppose a
call
> to "std::for_each" instead of "for_each" should do the job.

> Olaf



Thu, 20 May 2004 18:06:19 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Tip on persistent data and the for_each() algorithm

2. Tip on persistent data and the for_each() algorithm

3. for_each standard algorithm and virtual functions

4. Problem with STL for_each

5. how can I use the for_each algorithm on a map ?

6. for_each, member functions, and this pointers

7. for_each and compiler bug

8. Can for_each() be used with multi-argument functions?

9. for_each and ostream operator<<

10. combining binders in a for_each call

11. for_each and inlining?

12. for_each() and Functions

 

 
Powered by phpBB® Forum Software