1

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();
6
  • 2
    fncptr1.h includes fncptr2.h and fncptr2.h includes fncptr1.h, this is seriously wrong. Commented Aug 25, 2021 at 11:55
  • 1
    And f.implfncptr(&addition); should be f.implfncptr(&fncptr1::addition); Commented Aug 25, 2021 at 11:59
  • @Jabberwocky: please assist how to remodify the code Commented Aug 25, 2021 at 12:00
  • Also show main.cpp, the one you show is incomplete, and show the full error log. Commented Aug 25, 2021 at 12:00
  • It's also unclear what you're hoping for when calling f.testfncptr(); in main. The result of the addition is just thrown away. Commented Aug 25, 2021 at 12:04

1 Answer 1

2
  • Remove the includes of fncptr2.h and fncptr1.h from all .h files. Instead add them to fncptr1.c and fncptr2.c.
  • you need f.implfncptr(&fncptr1::addition); instead of f.implfncptr(&addition); in fncptr1.cpp

fncptr1.h

#ifndef FNCPTR1
#define FNCPTR1

class fncptr1
{
public:
  int addition(int a, int b);
  void testfncptr();
};

#endif // !FNCPTR1

fncptr2.h

#ifndef FNCPTR2
#define FNCPTR2

class fncptr1;

class fncptr2
{
public:
  int implfncptr(int (fncptr1::* add)(int, int));
};

#endif // !FNCPTR2

fncptr1.cpp

#include "fncptr1.h"
#include "fncptr2.h"
#include <iostream>

using namespace std;

int fncptr1::addition(int a, int b)
{
  return a + b;
}

void fncptr1::testfncptr()
{
  fncptr2 f;
  f.implfncptr(&fncptr1::addition);
}

fncptr2.cpp

#include "fncptr2.h"
#include "fncptr1.h"
#include <iostream>

using namespace std;
fncptr1 ff;

int fncptr2::implfncptr(int (fncptr1::* add)(int, int))
{
  return (ff.*add)(1, 2);
}

main.cpp

#include "fncptr1.h"

int main()
{
  fncptr1 f;
  f.testfncptr();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.