friend

friend class with forward class declaration does not compile

一世执手 提交于 2019-12-11 03:09:27
问题 This a basic program to understand how to use friend class in C++. Class xxx has a class yyy object using friend . Since class yyy is defined after class xxx I have declared class yyy using forward declaration. #include<iostream> using std::cout; using std::endl; class yyy; //Forward Declaration of class yyy class xxx{ private: int a; public: xxx(){a=20;yyy y2;y2.show();} //Error// void show(){cout<<"a="<<a<<endl;} friend class yyy; //Making class yyy as freind of class xxx }; class yyy{

Friend method “not declared in this scope” in C++

瘦欲@ 提交于 2019-12-10 23:17:39
问题 First to provide some context, this is for an assignment involving semaphores. We are to find code for the dining philosophers problem, get it working, and then perform some analysis and manipulation. However, I am stuck with an error. The original code is taken from http://www.math-cs.gordon.edu/courses/cs322/projects/p2/dp/ using the C++ solution. The error I am receiving in Code::Blocks is philosopher.cpp|206|error: 'Philosopher_run' was not declared in this scope| and this error occurs in

cannot access namespace scope friend explicitly

狂风中的少年 提交于 2019-12-10 22:38:58
问题 I had an issue today where ADL wasn't finding a static member function for a type defined inside a class. That is, in the below example, str(foo::Foo::Enum) isn't located via ADL without explicitly scoping it, foo::Foo::str(foo::Foo::Enum) namespace foo { struct Foo { enum Enum { FOO1, FOO2 }; static const char* str(Enum e); }; } foo::Foo::Enum e = foo::Foo::FOO1; const char* s = str(e); // ADL doesn't work I found this SO question, and as stated in the accepted answer, changing it to a

Implicit conversion of lefthand argument in in-class declared friend operator

爱⌒轻易说出口 提交于 2019-12-10 22:03:55
问题 I am using CRTP to provide template-argument dependent addition of functions to a class, in this case the addition of operator + and operator += , using the template class ImplAdd . For the former, implicit conversions should be performed on both arguments, which means I have to use an in-class friend operator like this: template<class Type, bool active> struct ImplAdd{ virtual int get_val_() const = 0; virtual void set_val_(int) = 0; }; //if activated is true, the operators + and += will be

C++ unique_ptr versus friend class private destructor

谁都会走 提交于 2019-12-10 20:10:44
问题 I have this arrangement: class LexedFile { friend class Lex; //... private: ~LexedFile(); }; class Lex { //... private: std::map<std::string, std::unique_ptr<LexedFile> > Files; }; A Lex is the sole creator of LexedFile objects and retains ownership of all the LexedFile objects it creates in a map. Unfortunately, the compiler complains mightily about this due to visibility rules from the map variable to the LexedFile destructor. I can fix that problem by making ~LexedFile() public, but of

Cannot understand friend functions in a template class

五迷三道 提交于 2019-12-10 18:49:50
问题 This is code i have written to understand the concept. The code is fine and it runs. What i dont understand is that why is the marked line needed ? template <class T> class D { public : template <class P> //<------------------Why is this needed ? -------------- friend void print(D <P> obj); }; template <class T> void print(D<T> obj) {std::cout<<sizeof(T);}; int main() { D <char>obj3; print(obj3); return 0; } or in other words why does the following not run ? template <class T> class D {

Template + Friend (a deadly combination) [duplicate]

帅比萌擦擦* 提交于 2019-12-10 18:12:04
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Making an undefined class as friend, and defining it later. I have the following code template<typename T> class A { class B; B b; }; int main() { return 0; } The code doesn't emit any error because A is not instantiated. Am I right? But have a look at the second code sample template<typename T> class A { protected: class a { int x; int y; private: friend class b; }; template <typename U > class b { int z; U y;

friend class : inherited classes are not friend as well?

天涯浪子 提交于 2019-12-10 17:13:27
问题 In C++, I have a class A which is friend with a class B. I looks like inherited classes of B are not friend of class A. I this a limitation of C++ or my mistake ? Here is an example. When compiling, I get an error on line "return new Memento": Memento::Memento : impossible to access private member declared in Memento. class Originator; class Memento { friend class Originator; Memento() {}; int m_Data; public: ~Memento() {}; }; class Originator { public: virtual Memento* createMemento() = 0; }

Friend function with a definition - template or non-template?

谁都会走 提交于 2019-12-10 16:59:34
问题 Suppose we have the following code: template<class T> struct S; template<class T> void operator++(S<T>); template<class T> struct S { friend void operator++(S); }; template<class T> void operator++(S<T>) {} int main() { S<int> s; ++s; } This will compile but won't link, because the friend declaration introduces a non-template operator++ , that has never been defined. This FAQ answer reads ( bold is mine): The solution is to convince the compiler while it is examining the class body proper

How do I define a friend class from the global namespace in another namespace?

回眸只為那壹抹淺笑 提交于 2019-12-10 16:46:38
问题 In a previous Q&A (How do I define friends in global namespace within another C++ namespace?), the solution was given for making a friend function definition within a namespace that refers to a function in the global namespace. I have the same question for classes . class CBaseSD; namespace cb { class CBase { friend class ::CBaseSD; // <-- this does not work!? private: int m_type; public: CBase(int t) : m_type(t) {}; }; }; // namespace cb class CBaseSD { private: cb::CBase* m_base; public: