I read this page about passing function as a arguement on http://www.cplusplus.com/forum/beginner/6596/ and i tried it myself but some error
is popping up and i am unable to run the program.
The code:
inside "TextOperation2.h"
1 2 3 4 5 6 7 8 9 10 11
#pragma once
class TextOperation2
{
public:
TextOperation2(void);
bool primeOrNot(int);
void primesInList(int*,int,bool (*f)(int));
~TextOperation2(void);
};
#include "stdafx.h"
#include "TextOperation2.h"
usingnamespace std;
TextOperation2 testop;
void main()
{
fflush(stdout);
int n ;
cout <<"enter the number of values u want";
cin>>n;
int* list = newint[n];
cout<<endl<<"enter the list 1"<<endl;
for ( int counter = 0; counter <n;counter ++)
{
cin>>list[counter];
}
bool boolorn = testop.primeOrNot(n);
cout<<"\n"<<boolorn;
testop.primesInList(list,n,&primeOrNot);//i also tried testop.primeOrNot
_getch();
}
how can i correct this error ?
I am working under:
Operating system: Windows 7
IDE : Visual Studio 2012
A method is a message passed to an object, if you are not going to use/modify the state of said object, then it should not be a method but a free function.
To clarify, suppose that you were trying to do it in C.
The method bool TextOperation2::primeOrNot(int x)
would become bool primeOrNot(TextOperation2 *this, int x)
note how that's different that the function signature that you are expecting bool foo(int)
> some error is popping up and i am unable to run the program.
¿don't you think that the error message would be important?
If you could not understand what was trying to tell you, then that's more a reason to post it so we could read it