friend

Forward declaration of template friend function

时间秒杀一切 提交于 2021-02-18 22:10:22
问题 Consider the following code snippet that works perfectly fine: class A { private: int d; public: A(int n){ d = n;} friend int foo(A a); }; int foo(A a) { return a.d; } However, when I try to use a template for the class, I need to forward declare the friend function for it to run, as follows: template <typename T> class B; template <typename T> T foof(B<T> a); template <typename T> class B { private: T d; public: B(T n){ d = n;} friend T foof<>(B<T> a); }; template <typename T> T foof(B<T> a)

Forward declaration of template friend function

社会主义新天地 提交于 2021-02-18 22:10:18
问题 Consider the following code snippet that works perfectly fine: class A { private: int d; public: A(int n){ d = n;} friend int foo(A a); }; int foo(A a) { return a.d; } However, when I try to use a template for the class, I need to forward declare the friend function for it to run, as follows: template <typename T> class B; template <typename T> T foof(B<T> a); template <typename T> class B { private: T d; public: B(T n){ d = n;} friend T foof<>(B<T> a); }; template <typename T> T foof(B<T> a)

Forward declaration of template friend function

Deadly 提交于 2021-02-18 22:09:44
问题 Consider the following code snippet that works perfectly fine: class A { private: int d; public: A(int n){ d = n;} friend int foo(A a); }; int foo(A a) { return a.d; } However, when I try to use a template for the class, I need to forward declare the friend function for it to run, as follows: template <typename T> class B; template <typename T> T foof(B<T> a); template <typename T> class B { private: T d; public: B(T n){ d = n;} friend T foof<>(B<T> a); }; template <typename T> T foof(B<T> a)

friend declaration of template specialization fails

爷,独闯天下 提交于 2021-02-18 11:16:48
问题 The following code containing friend declaration fails with indicated error (see http://ideone.com/Kq5dy): template<class T> void foo() {} template<typename T> class A { void foo(); friend void foo<T>(); // error: variable or field 'foo' declared void }; int main() { foo<int>(); } If the order of friend declaration and member function declaration reversed, then the code compiles without problems (see http://ideone.com/y3hiK): template<class T> void foo() {} template<typename T> class A {

c++ friend overloading operator <<

大憨熊 提交于 2021-02-10 14:19:44
问题 I am trying overload the operator << but i keep having this error. I try doing research but with no result. I have a Point2D.h and a Point2D.cpp with a friend functions to overload. Below are my codes: Point2D.h #include <string> #include <iomanip> using namespace std; #ifndef Point2D_H #define Point2D_H class Point2D { friend ostream& operator<< (ostream&, Point2D); public: Point2D(); Point2D(int, int); protected: int x; int y; }; Point.cpp #include <string> #include <cmath> #include

How to resolve circular dependency with friend declarations in C++?

随声附和 提交于 2021-02-10 05:39:32
问题 Why doesn't the following code compile and how can I fix it? The error I get is: Use of undeclared identifier 'Foo' although Foo is clearly declared and defined at the point where the error occurs (at the friend declaration in Bar ). foo.h : #ifndef FOO_H #define FOO_H #include "bar.h" // needed for friend declaration in FooChild class Foo { public: void Func(const Bar&) const; }; class FooChild : public Foo { friend void Bar::Func(FooChild*); }; #endif foo.cpp : #include "foo.h" void Foo:

How to resolve circular dependency with friend declarations in C++?

荒凉一梦 提交于 2021-02-10 05:39:08
问题 Why doesn't the following code compile and how can I fix it? The error I get is: Use of undeclared identifier 'Foo' although Foo is clearly declared and defined at the point where the error occurs (at the friend declaration in Bar ). foo.h : #ifndef FOO_H #define FOO_H #include "bar.h" // needed for friend declaration in FooChild class Foo { public: void Func(const Bar&) const; }; class FooChild : public Foo { friend void Bar::Func(FooChild*); }; #endif foo.cpp : #include "foo.h" void Foo:

Nonmember friend function is always inline

孤街醉人 提交于 2021-02-08 12:51:15
问题 I am very new to C++, and when I am trying to learn the friend function, I saw from friend description on Cppreference that: 2) (only allowed in non-local class definitions) Defines a non-member function, and makes it a friend of this class at the same time. Such non-member function is always inline . class X { int a; friend void friend_set(X& p, int i) { p.a = i; // this is a non-member function } public: void member_set(int i) { a = i; // this is a member function } }; Does this mean that

Nonmember friend function is always inline

不想你离开。 提交于 2021-02-08 12:51:11
问题 I am very new to C++, and when I am trying to learn the friend function, I saw from friend description on Cppreference that: 2) (only allowed in non-local class definitions) Defines a non-member function, and makes it a friend of this class at the same time. Such non-member function is always inline . class X { int a; friend void friend_set(X& p, int i) { p.a = i; // this is a non-member function } public: void member_set(int i) { a = i; // this is a member function } }; Does this mean that

Operator << and inheritance

≡放荡痞女 提交于 2021-02-08 05:01:55
问题 I have the following classes in C++: class Event { //... friend ofstream& operator<<(ofstream& ofs, Event& e); }; class SSHDFailureEvent: public Event { //... friend ofstream& operator<<(ofstream& ofs, SSHDFailureEvent& e); }; The code I want to execute is: main() { Event *e = new SSHDFailureEvent(); ofstream ofs("file"); ofs << *e; } This is a simplification, but what I want to do is write into a file several type of Events in a file. However, instead of using the operator << of