I have compiled the code below and run it successfully with Microsoft Visual Studio 2008's compiler. However, the compiler I need to use in production (due to project constraints) is the Intel C++ compiler 11.1.065.
So, my question is: Is some part of the syntax incorrect in the following code (and the visual studio compiler just works with it) or does the version of the Intel Compiler that I am using just lack support? If so, would anyone be so kind as to provide syntax that the version 11.1.065 of the Intel Compiler can understand? Thank you in advance!
Header file A.h:
#ifndef A_H
#define A_H
#include "B.h"
//forward declarations
template <typename T> class B;
// I tried adding the following line (even though it is
// not needed by the visual studio compiler:
//template <typename T> B<T>::B(A<T> const&);
template <typename T>
class A
{
private:
int m_var;
public:
A() : m_var(1) {}
friend B<T>::B(A<T> const& x);
};
#endif //A_H
Header file B.h:
#ifndef B_H
#define B_H
#include "A.h"
template <typename T> class A;
template <typename T>
class B
{
private:
int m_var;
public:
B(A<T> const& x)
: m_var(x.m_var)
{}
};
#endif //B_H
Body file with Main Function:
#include "B.h"
#include "A.h"
int main(int argc, char* argv[])
{
A<int> a;
B<int> b(a);
return 0;
}
The error returned by the Intel compiler is:
.\A.h(16): error: expected a ")"
friend B<T>::B(A<T> const& x);
^
EDIT: Because there is so much focus surrounding whether or not this is caused by the multiple inclusion, the above code expands to the following which shows that the multiple inclusion should not impact the code:
//<#include "B.h" expanded>
#ifndef B_H
#define B_H
//<#include "A.h" expanded>
#ifndef A_H
#define A_H
//<#include "B.h" expanded>
// Because B_H is defined, B.h's contents are not expanded
//</#include "B.h" expanded>
//forward declarations
template <typename T> class B;
// I tried adding the following line (even though it is
// not needed by the visual studio compiler:
//template <typename T> B<T>::B(A<T> const&);
template <typename T>
class A
{
private:
int m_var;
public:
A() : m_var(1) {}
friend B<T>::B(A<T> const& x);
};
#endif //A_H
//</#include "A.h" expanded>
template <typename T> class A;
template <typename T>
class B
{
private:
int m_var;
public:
B(A<T> const& x)
: m_var(x.m_var)
{}
};
#endif //B_H
//</#include "B.h" expanded>
//<#include "A.h" expanded>
// Because A_H is defined, A.h's contents are not expanded
//</#include "A.h" expanded>
int main(int argc, char* argv[])
{
A<int> a;
B<int> b(a);
return 0;
}