c++ - Template Class Constructor as Friend of Other Template Class -


i have compiled code below , run microsoft visual studio 2008's compiler. however, compiler need use in production (due project constraints) intel c++ compiler 11.1.065.

so, question is: part of syntax incorrect in following code (and visual studio compiler works it) or version of intel compiler using lack support? if so, kind provide syntax version 11.1.065 of intel compiler can understand? thank in advance!

header file a.h:

#ifndef a_h #define a_h  #include "b.h"  //forward declarations template <typename t> class b; // tried adding following line (even though //    not needed visual studio compiler: //template <typename t> b<t>::b(a<t> const&);  template <typename t> class { 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 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 intel compiler is:

.\a.h(16): error: expected ")"       friend b<t>::b(a<t> const& x);                           ^ 

edit: because there focus surrounding whether or not caused multiple inclusion, above code expands following shows multiple inclusion should not impact 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 defined, b.h's contents not expanded //</#include "b.h" expanded>  //forward declarations template <typename t> class b; // tried adding following line (even though //    not needed visual studio compiler: //template <typename t> b<t>::b(a<t> const&);  template <typename t> class { 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 defined, a.h's contents not expanded //</#include "a.h" expanded>  int main(int argc, char* argv[]) {     a<int> a;     b<int> b(a);      return 0; } 

remove forward declaration of b a.h , reverse order of includes.

#include "b.h" #include "a.h". 

you cannot make individual member functions friends until have been declared. hence b's ctor (and hence b) needs visible before declare friend in a. however, b can fwd declaration of a because uses reference a.

hence need reverse order of includes.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -