friend-function

Undefined reference to friend function template defined inside class in a namespace

主宰稳场 提交于 2019-12-06 07:30:32
This is a follow-up on my answer to this question . The original answer I posted does not have any namespaces, and it solves the problem. However, the OP subsequently made an edit claiming that the solution does not work if the class is inside a namespace. My initial reaction was that you can make it work by simply having using N::f; either at global scope or inside main() (or any other function). While that is certainly the case, the OP (justifiably) commented that this is not ideal, and I agree. Nevertheless, I still thought that calling N::f without having using N::f; should work just fine,

What is the point of the complicated scoping rules for friend declarations?

随声附和 提交于 2019-12-05 16:30:18
问题 I recently discovered that friend declarations scoping follows extremely peculiar rules - if you have a friend declaration (definition) for a function or a class that is not already declared, it is automatically declared (defined) in the immediately enclosing namespace, but it is invisible to non-qualified and qualified lookup; however, friend function declarations remain visible through argument-dependent lookup. struct M { friend void foo(); friend void bar(M); }; void baz() { foo(); //

Friend functions in C++

China☆狼群 提交于 2019-12-05 13:38:25
问题 I have a doubt related to friend functions in C++. Friend function is not a member function of the claas and can be invoked directly from the main. So, what difference does it make if we keep the friend function within the private or the public part of the class . I have generally noticed that the friend functions are always in the public part. In what scenario we should keep the friend function within private . 回答1: The compiler does not pay any attention to whether a friend function is in

C++ friend function hidden by class function?

喜欢而已 提交于 2019-12-05 01:36:05
Minimal example: class A { friend void swap(A& first, A& second) {} void swap(A& other) {} void call_swap(A& other) { swap(*this, other); } }; int main() { return 0; } g++ 4.7 says: friend.cpp: In member function ‘void A::call_swap(A&)’: friend.cpp:7:20: error: no matching function for call to ‘A::swap(A&, A&)’ friend.cpp:7:20: note: candidate is: friend.cpp:4:7: note: void A::swap(A&) friend.cpp:4:7: note: candidate expects 1 argument, 2 provided Outcomment line 4: // void swap(A& other) {} ...and it works fine. Why, and how to fix this, if I want to keep both variants of my swap function? I

Friend functions in C++

橙三吉。 提交于 2019-12-04 01:45:31
I have a doubt related to friend functions in C++. Friend function is not a member function of the claas and can be invoked directly from the main. So, what difference does it make if we keep the friend function within the private or the public part of the class . I have generally noticed that the friend functions are always in the public part. In what scenario we should keep the friend function within private . The compiler does not pay any attention to whether a friend function is in the private or public (or protected) section of a class. Most people put it in the public section, but it'll

friend member function in C++ - forward declaration not working

烈酒焚心 提交于 2019-12-02 11:57:44
I'm having a situation similar to the one described in Specify a class member function as a friend of another class? . However in my case, class B needs to know class A since it's using it, so the solution given in that thread is not working for me. I tried to give also a forward declaration to the function itself but it didn't work as well. It seems that each class need the full definition of the other... Is there any easy way to solve it? I prefer a solution which doesn't involve new classes which wrap one of the old classes. code example: //A.h class B; //not helping void B::fB(); //not

Forward declaration with friend function: invalid use of incomplete type

主宰稳场 提交于 2019-12-01 17:47:29
#include <iostream> class B; class A{ int a; public: friend void B::frndA(); }; class B{ int b; public: void frndA(); }; void B::frndA(){ A obj; std::cout << "A.a = " << obj.a << std::endl; } int main() { return 0; } When trying to compile this code, some errors occurred. E.g. invalid use of incomplete type What are the problems in this code? Place the whole of the class B ... declaration before class A . You haven't declared B::frndA(); yet. #include <iostream> using namespace std; class B{ int b; public: void frndA(); }; class A{ int a; public: friend void B::frndA(); }; void B::frndA(){ A

Forward declaration with friend function: invalid use of incomplete type

冷暖自知 提交于 2019-12-01 17:32:50
问题 #include <iostream> class B; class A{ int a; public: friend void B::frndA(); }; class B{ int b; public: void frndA(); }; void B::frndA(){ A obj; std::cout << "A.a = " << obj.a << std::endl; } int main() { return 0; } When trying to compile this code, some errors occurred. E.g. invalid use of incomplete type What are the problems in this code? 回答1: Place the whole of the class B ... declaration before class A . You haven't declared B::frndA(); yet. #include <iostream> using namespace std;

Can 2 classes share a friend function?

社会主义新天地 提交于 2019-12-01 16:36:17
Today i have a doubt regarding friend function. Can two classes have same friend function? Say example friend void f1(); declared in class A and class B. Is this possible? If so, can a function f1() can access the members of two classes? An example will explain this best: class B; //defined later void add(A,B); class A{ private: int a; public: A(){ a = 100; } friend void add(A,B); }; class B{ private: int b; public: B(){ b = 100; } friend void add(A,B); }; void add (A Aobj, B Bobj){ cout << (Aobj.a + Bobj.b); } main(){ A A1; B B1; add(A1,B1); return 0; } Hope this helps! There is no

How to declare a variadic template function as a friend?

柔情痞子 提交于 2019-11-30 03:50:16
问题 How to declare a variadic template function as a friend? For example as follows: template<class T> class A { friend ??? MakeA ??? ; // What should be placed here ??? A(T) {} }; template<class T, class... Args> A<T> MakeA(Args&&... args) { T t(std::forward<Args>(args)); return A(t); } 回答1: It's quite straightforward. It's simply a template declaration with the added friend specifier: template<class T> class A { template<class T1, class... Args> friend A<T1> MakeA(Args&&... args); A(T) { } };