Click here to Skip to main content
15,911,890 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: center text in title bar of app. Pin
David Crow8-May-03 4:25
David Crow8-May-03 4:25 
GeneralRe: center text in title bar of app. Pin
JensB8-May-03 4:30
JensB8-May-03 4:30 
GeneralRe: center text in title bar of app. Pin
Renjith Ramachandran8-May-03 5:14
Renjith Ramachandran8-May-03 5:14 
GeneralRe: center text in title bar of app. Pin
User 66588-May-03 6:17
User 66588-May-03 6:17 
GeneralRe: center text in title bar of app. Pin
Joan M8-May-03 5:57
professionalJoan M8-May-03 5:57 
GeneralRe: center text in title bar of app. Pin
Hari Krishnan (Noida)8-May-03 6:14
Hari Krishnan (Noida)8-May-03 6:14 
GeneralRe: center text in title bar of app.(Found the link) Pin
Hari Krishnan (Noida)8-May-03 6:22
Hari Krishnan (Noida)8-May-03 6:22 
QuestionC++ Function objects - how to use them? Pin
Ilushka8-May-03 2:37
Ilushka8-May-03 2:37 
Hi All!

Here goes the question:

Mr. C++ has cleary descibed how to use function objects in Parhagraph 18.4 of his Book. The
Sample code works OK, and if i use them with STL, everything`s OK

Here is a simple sample:

void Add7(int& out_Int)
{
out_Int += 7;
}

class AddNumber
{
public:
int m_iToAdd;

AddNumber(int in_iToAdd = 0)
{
m_iToAdd = in_iToAdd;
}

void operator()(int& out_Int)
{
out_Int += m_iToAdd;
}
};

class A
{
public:
std::vector < int > m_vData;
};

void main()
{

A tmp_a;

tmp_a.m_vData.push_back(7);
tmp_a.m_vData.push_back(5);
tmp_a.m_vData.push_back(6);

std::for_each(tmp_a.m_vData.begin(),tmp_a.m_vData.end(),Add7);

std::for_each(tmp_a.m_vData.begin(),tmp_a.m_vData.end(),AddNumber(13));

}

---

But when i try to use them some other way than in combination with STL, I get that:


typedef void (*TraveseFuncPtr) (int&);

void Add7(int& out_Int)
{
out_Int += 7;
}

class AddNumber
{
public:
int m_iToAdd;

AddNumber(int in_iToAdd = 0)
{
m_iToAdd = in_iToAdd;
}

void operator()(int& out_Int)
{
out_Int += m_iToAdd;
}
};

class A
{
public:
std::vector < int > m_vData;
void TraverseVector(TraveseFuncPtr in_func)
{
for (int i = 0; i < m_vData.size(); i++)
{
(*in_func)(m_vData[i]);
}
}
};

void main()
{
A tmp_a;

tmp_a.m_vData.push_back(7);
tmp_a.m_vData.push_back(5);
tmp_a.m_vData.push_back(6);

tmp_a.TraverseVector(Add7); // All ok.

tmp_a.TraverseVector(AddNumber(13)); //C2664
}


---

And thats it.

error C2664: 'TraverseVector' : cannot convert parameter 1 from 'class AddNumber' to 'void (__cdecl *)(int &)'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

I tried to dig up STL source but hell the same for_each takes a CLASS (!) as a template parameter.
No functions there.

Who can say anything to this?


Sincerely yours, Ilya Kalujny.
AnswerRe: C++ Function objects - how to use them? Pin
Aaron Schaefer8-May-03 3:42
Aaron Schaefer8-May-03 3:42 
AnswerRe: C++ Function objects - how to use them? Pin
jhwurmbach8-May-03 3:47
jhwurmbach8-May-03 3:47 
AnswerRe: C++ Function objects - how to use them? Pin
Andrew Walker8-May-03 3:50
Andrew Walker8-May-03 3:50 
AnswerRe: C++ Function objects - how to use them? Pin
John R. Shaw8-May-03 17:15
John R. Shaw8-May-03 17:15 
AnswerRe: C++ Function objects - how to use them? Pin
kuhx198011-May-03 4:16
kuhx198011-May-03 4:16 
GeneralMenu sharing in MDI application with 2 document templates Pin
Jayanta Basu8-May-03 2:27
Jayanta Basu8-May-03 2:27 
Questionwriting data to a text file????????? Pin
xxhimanshu8-May-03 1:43
xxhimanshu8-May-03 1:43 
AnswerRe: writing data to a text file????????? Pin
Nitron8-May-03 1:55
Nitron8-May-03 1:55 
QuestionCan someone help, im new to C++ Pin
mawmany2k8-May-03 1:30
mawmany2k8-May-03 1:30 
AnswerRe: Can someone help, im new to C++ Pin
Stephane Rodriguez.8-May-03 1:33
Stephane Rodriguez.8-May-03 1:33 
AnswerRe: Can someone help, im new to C++ Pin
Hari Krishnan (Noida)8-May-03 2:17
Hari Krishnan (Noida)8-May-03 2:17 
GeneralMemory Leak Pin
saravanaram8-May-03 1:09
saravanaram8-May-03 1:09 
GeneralRe: Memory Leak Pin
Stephane Rodriguez.8-May-03 1:31
Stephane Rodriguez.8-May-03 1:31 
GeneralRe: Memory Leak Pin
Hari Krishnan (Noida)8-May-03 2:47
Hari Krishnan (Noida)8-May-03 2:47 
GeneralRe: Memory Leak Pin
David Crow8-May-03 3:14
David Crow8-May-03 3:14 
GeneralRe: Memory Leak Pin
Hari Krishnan (Noida)8-May-03 4:13
Hari Krishnan (Noida)8-May-03 4:13 
GeneralRe: Memory Leak Pin
David Crow8-May-03 4:26
David Crow8-May-03 4:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.