0

My question concerns the templates again... From my static template function "functionA" in non template class A

A.h

#ifndef A_H
#define A_H
#include "B.h"
class A
{
  template <class T>
  static T functionA();

};

template <class T>
T A::functionA()
{
     T var;
     T result = B::functionB(var); //Class B has not been declared
}

#endif

I am calling static template function "function B" declared and defined inside non template class B. Before the class A declaration class B has already been included...

B.h

#ifndef B_H
#define B_H
class B
{
  template <class T>
  static T functionB(T var) ;
};

template<class T>
T B::functionB(T var)
{
   ...some code
}
#endif

During the compilation of the program the following error message has been apperared:

//Class B has not been declared

This is not a real code, only example for the ilustration. This situation has appeared calling some static methods i my program. Can you help me, please?

Sancho
  • 21
  • 2
  • 2

1 Answers1

1

The specific problem with the code posted is that the functions are not declared as being public. Also, functionA is not returning a value.

The following code will execute properly.

file A.h

#ifndef A_H
#define A_H
#include "B.h"

class A
{
public:
  template <class T>
  static T functionA();
};

template <class T>
T A::functionA()
{
     T var = 4;
     T result = B::functionB(var); //Class B has not been declared
     return result;
}

#endif

file B.h

#ifndef B_H
#define B_H
class B
{
public:
  template <class T>
  static T functionB(T var) ;
};

template<class T>
T B::functionB(T var)
{
  var++;
  return var;
}
#endif

file main.cpp

#include "A.h"
#include <stdio.h>

int main(void)
{
  int result = A::functionA<int>();
  printf("result: %i\n", result);
  return 0;
}

output

result: 5
Gravis
  • 992
  • 11
  • 17