I am new to function pointer and tried to pass the function pointer as parameter from one class to other and getting compiler error.
'fncptr1': is not a class or namespace name"
what am I doing wrong?
fncptr1.h
#ifndef FNCPTR1
#define FNCPTR1
#include "fncptr2.h"
class fncptr1
{
public:
int addition(int a,int b);
void testfncptr();
};
#endif // !FNCPTR1
fncptr1.cpp
#include "fncptr1.h"
#include <iostream>
using namespace std;
int fncptr1::addition(int a,int b)
{
return a + b;
}
void fncptr1::testfncptr()
{
fncptr2 f;
f.implfncptr(&addition);
}
fncptr2.h
#ifndef FNCPTR2
#define FNCPTR2
#include "fncptr1.h"
class fncptr2
{
public:
int implfncptr(int (fncptr1::*add)(int,int));
};
#endif // !FNCPTR2
fncptr2.cpp
#include "stdafx.h"
#include "fncptr2.h"
#include <iostream>
using namespace std;
fncptr1 ff;
int fncptr2::implfncptr(int (fncptr1::*add)(int, int))
{
return (ff.*add)(1,2);
}
main.cpp
fncptr1 f;
f.testfncptr();
fncptr1.hincludesfncptr2.handfncptr2.hincludesfncptr1.h, this is seriously wrong.f.implfncptr(&addition);should bef.implfncptr(&fncptr1::addition);main.cpp, the one you show is incomplete, and show the full error log.f.testfncptr();inmain. The result of the addition is just thrown away.