fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template<class T>
  5. class Test;
  6.  
  7.  
  8. class BeMyFriend
  9. {
  10. public:
  11. template<class T>
  12. int checkprivacy(const Test<T> areyoumyfriend)
  13. {
  14. std::cout << "lemme check " << areyoumyfriend.mything << std::endl;
  15.  
  16. };
  17. BeMyFriend& operator=(const BeMyFriend& );
  18. BeMyFriend(){};
  19. };
  20. class Imnotyourfriend
  21. {
  22. public:
  23. template<class T>
  24. int checkprivacy(const Test<T> areyoumyfriend){
  25. areyoumyfriend.mything;
  26. };
  27. Imnotyourfriend(){std::cout << "works " << std::endl;};
  28. };
  29.  
  30. template<class T>
  31. class Test
  32. {
  33. private:
  34. string mything= "here it is";
  35. public:
  36. friend T& T::operator=(const T&);
  37. friend BeMyFriend&::BeMyFriend(const BeMyFriend&);
  38. friend T&::T(const T&);
  39. template<class D=T>
  40. friend int T::checkprivacy(const Test<D>);
  41.  
  42. };
  43.  
  44.  
  45.  
  46.  
  47.  
  48. int main()
  49. {
  50. Test<BeMyFriend> hmm;
  51. Imnotyourfriend wannabefriend;
  52. wannabefriend.checkprivacy(hmm);
  53.  
  54. return 0;
  55. }
  56.  
Compilation error #stdin compilation error #stdout 0s 4192KB
stdin
Standard input is empty
compilation info
prog.cpp:38:23: warning: friend declaration ‘T& T(const T&)’ declares a non-template function [-Wnon-template-friend]
  friend T&::T(const T&);
                       ^
prog.cpp:38:23: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) 
prog.cpp: In instantiation of ‘int Imnotyourfriend::checkprivacy(Test<T>) [with T = BeMyFriend]’:
prog.cpp:52:32:   required from here
prog.cpp:25:17: error: ‘std::__cxx11::string Test<BeMyFriend>::mything’ is private within this context
  areyoumyfriend.mything;
  ~~~~~~~~~~~~~~~^~~~~~~
prog.cpp:34:18: note: declared private here
  string mything= "here it is";
                  ^~~~~~~~~~~~
stdout
Standard output is empty