friend-function

Is there any way to declare mutual friend functions for two classes

折月煮酒 提交于 2021-02-16 15:15:12
问题 class CDB; class CDM { public: friend CDB& CDB::Add(const CDM&); CDM& Add(const CDB&); }; class CDB { public: CDB& Add(const CDM&); friend CDM& CDM::Add(const CDB&); }; This code gives me the error : error C2027: use of undefined type 'CDB'. How to resolve this. 回答1: No, you can't do that. There is no way to remove the cyclic dependency. You should be able to get by with making the class CDB a friend of CDM instead of wanting to making CDB::Add() a friend. class CDB; class CDM { public:

Overloading of hidden friends by differences only in (mutually exclusive) requires-clauses: legal or an ODR-violation?

有些话、适合烂在心里 提交于 2021-02-07 06:13:31
问题 Consider the following class template, which contains two (hidden) friend declarations of the same friend (same function type ; see below), which also defines the friend (and the friend is thus inline), but with the definition conditional on (mutually exclusive) requires-clauses : #include <iostream> struct Base {}; template<int N> struct S : public Base { friend int foo(Base&) requires (N == 1) { return 1; } friend int foo(Base&) requires (N == 2) { return 3; } }; [dcl.fct]/8 states that

Friend Function of Class produces error: “no '___' member function declared”

拥有回忆 提交于 2021-02-05 08:36:08
问题 I have a class, and I am trying to create a friend function to operate on the data of that class. Here is an example of what I am trying to do: // test.hpp class test { public: friend void friendly_function(); private: int data; }; void test::friendly_function() { data = 0; } However, the compiler spits out an error: test.hpp:23:34: error: no ‘void test::friendly_function()’ member function declared in class ‘test’ I know I can declare operators in this way, like so: class test { public:

Friend Function of Class produces error: “no '___' member function declared”

醉酒当歌 提交于 2021-02-05 08:35:39
问题 I have a class, and I am trying to create a friend function to operate on the data of that class. Here is an example of what I am trying to do: // test.hpp class test { public: friend void friendly_function(); private: int data; }; void test::friendly_function() { data = 0; } However, the compiler spits out an error: test.hpp:23:34: error: no ‘void test::friendly_function()’ member function declared in class ‘test’ I know I can declare operators in this way, like so: class test { public:

template friend functions of template class

情到浓时终转凉″ 提交于 2020-07-15 07:46:47
问题 I have the following template class and template function which intends to access the class' private data member: #include <iostream> template<class T> class MyVar { int x; }; template<class T> void printVar(const MyVar<T>& var) { std::cout << var.x << std::endl; } template<class T> void scanVar(MyVar<T>& var) { std::cin >> var.x; } struct Foo {}; int main(void) { MyVar<Foo> a; scanVar(a); printVar(a); return 0; } To declare the two functions as MyVar<T> 's friend functions, I've tried the

C++ compile time counters, revisited

房东的猫 提交于 2020-02-12 08:05:44
问题 TL;DR Before you attempt to read this whole post, know that: a solution to the presented issue has been found by myself, but I'm still eager to know if the analysis is correct; I've packaged the solution into a fameta::counter class that solves a few remaining quirks. You can find it on github; you can see it at work on godbolt. How it all started Since Filip Roséen discovered/invented, in 2015, the black magic that compile time counters are in C++, I have been mildly obsessed with the device

What is the right way to define a friend function outside a template class?

这一生的挚爱 提交于 2020-02-05 13:11:12
问题 If I have a normal class I can "inject" a non-free friend function inside the class. (That among other things can be only be found by ADL). case 1: class A{ double p_; friend double f(A const& a){return a.p_;} }; If instead this is a template class I can do: case 2: template<class T> class A{ double p_; friend double f(A const& a){return a.p_;} // apparently A const& is a synomyn for A<T> const& }; Now suppose that I need to implement f in terms of a class that needs to be defined later. I

Are friend functions inherited? and why would a base class FRIEND function work on a derived class object?

ぐ巨炮叔叔 提交于 2020-01-16 05:26:27
问题 class baseClass { public: friend int friendFuncReturn(baseClass &obj) { return obj.baseInt; } baseClass(int x) : baseInt(x) {} private: int baseInt; }; class derivedClass : public baseClass { public: derivedClass(int x, int y) : baseClass(x), derivedInt(y) {} private: int derivedInt; }; in the function friend int friendFuncReturn(baseClass &obj) { return obj.baseInt; } I don't understand why would the friend function of the base class work for the derived class? should not passing derived

C++ friend function hidden by class function?

Deadly 提交于 2020-01-13 08:22:13
问题 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) {}

friend function in template definition

送分小仙女□ 提交于 2019-12-24 05:33:15
问题 My question ist related a bit to this one. I want to overload the operator << for some class and I found two different notations that both work: template <class T> class A{ T t; public: A(T init) : t(init){} friend ostream& operator<< <> (ostream &os, const A<T> &a); //need forward declaration //template <class U> friend ostream& operator<< (ostream &os, const A<U> &a); }; Do I define identical things with different notations? Or is the first version more restrictive in which instance (in