FAQ Database Discussion Community


c++ friend class as argument for ctor

c++,class,friend
I am wondering how come this code correct and how the compiler knows to first create an obj of class A, I would think it shouldn't compile since B's ctor request an argument of type A not int class A { int a1; public: A(int i) { cout << i...

Provide a proper operator<< with variadic template specialization

c++,templates,operators,friend
When providing the operator<< for a specialization of a template class, clang likes the inline friend form: #include <iostream> template <typename A, typename... Bs> struct Hello; template <typename A> struct Hello<A> { template <typename A2> friend std::ostream & operator<<(std::ostream & s, const Hello<A2> & h) { return s << "specialized\n";...

Is there a way to protect a class variable from being modified outside of a function

c#,class,private,friend
I have a volume variable that I want to be protected from modification unless the person calls a certain function. Is there a way to make it to where it can only be modified by that function other than creating a private class within the class. I suppose that creating...

Template friend function of template class that introduces a new template parameter

c++,templates,friend
Thanks to Daniel Frey's answer to this post, I know how to declare a template friend function to a template class with the same template parameters. Unfortunately, the syntax for declaring a friend function with additional template parameters still escapes me. I would like to achieve something like this: template...

Trying to befriend a function of Derived in Base - Why doesn't it work?

c++,inheritance,friend,forward-declaration,friend-function
My first post here. I have two classes - Base and Derived (derived publicly from Base). In Derived, I have a function printAsFriend. Compiler is unhappy with this. Why? Methods in other classes can be befriended. Why doesn't it work with a derived class? It works OK if I remove...

specialize friend operator in template class previously defined error

c++,templates,operator-keyword,friend
I tried to specialize << operator for char in my template class hpp template<class T> class tablicowy{ public: T * tablica; int rozmiar; public: tablicowy(T arr[], int n){ { tablica = arr; rozmiar = n; } }; friend std::ostream& operator<<(std::ostream& out, tablicowy<char>& that ); friend std::ostream& operator<<(std::ostream& out, tablicowy<T>& that...

send a friend in modal bootstrap

php,twitter-bootstrap,modal-dialog,send,friend
Well, before asking here I have tried to find an answer for the issue I work with. Maybe someone who knows will help to find solution. I'm using bootstrap framework and here I have a "send friend" button which shows a modal fill-in form. here's link: http://codepen.io/anon/pen/bNvOPB?editors=100 If fill out...

How to separate a template class from its friend template class into different header files?

c++,class,templates,friend,source-file-organization
A file contains template class A and template class B. A is friend of B. I want to separate them into different files. How to deal with it? ...

assert as a friend class

c++,assert,friend
I can not declare neither assert class, nor assert function as a friendly to my class. Am I right with such a declaration? class Baka { private: friend assert; //invalid friend declaration friend void assert(); //expected an identifer public: } I've already googled a lot, but I can't find right...

Linker error when operator== is a friend [duplicate]

c++,templates,linker,friend
This question already has an answer here: overloading friend operator<< for template class 5 answers The following code is a minimum code to reproduce my problem. When I try to compile it, the linker can not find operator== for Config: Undefined symbols for architecture x86_64: "operator==(Config<2> const&, Config<2> const&)",...

some friend functions don't follow the rule

c++,friend
For the following snippet: class A{ friend void f(){}; public: A(){f();} //error }; class B{ friend void f(void* ptr){}; public: B(){f(this);} //no error }; According to the rule that although friend functions can be defined inside a class, yet they are not visible until they are declared somewhere outside the...

template friend functions of template class

c++,templates,friend,friend-function,template-function
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;...

C++: Correct syntax for friending a template type member of template parameter?

c++,templates,c++11,using,friend
I have a class that takes a template type parameter (tTRAIT). I want to friend a template type member alias of tTRAIT, but I can't figure out the syntax. (Is this even possible?). template <bool bBOOL> struct SFoo {}; struct STrait { template <bool bBOOL> using TFoo = SFoo<bBOOL>; };...

I don't understand how the template function gets to be a friend of the class A::X::Y in the example in [namespace.memdef]/3 in C++14

c++,templates,language-lawyer,c++14,friend
Consider the following code from the example in [namespace.memdef]/3 in C++14: // Assume f and g have not yet been declared. void h(int); template <class T> void f2(T); namespace A { class X { friend void f(X); // A::f(X) is a friend class Y { friend void g(); // A::g...

Friend function is unable to construct a unique pointer of the class

c++,c++11,friend,unique-ptr
I have a certain design strategy where the constructor of my class is private and can only be constructed by friends of the class. Inside the friend function, I am trying to create a unique_pointer of my class using std::make_uniquebut it doesn't compile. My VC12 compiler complains c:\program files (x86)\microsoft...

Calling Protected Function From Derived Friend Function

c++,oop,friend,derived-class
I have a base class, Animal, and a derived class, Lion. Animal has a protected function called eat(). I would like to call eat() from a friend function defined in Lion, but when it won't compile: error: call to non-static member function without an object argument Why can't I call...

Template class specialization and friend classes [closed]

c++,templates,c++11,friend
I am in the following situation: template<typename T, int N> class Foo { }; // specialization for 0 template<typename T> class Foo<0> { friend class Foo<T, 1>; }; That is, I need that Foo<T, 1> is friend of Foo<T, 0>, but I have a compiler error. Is this possible?...

Must the C++ standard library support classes that are picky about who their friends are?

c++,stl,language-lawyer,friend,access-modifiers
This question is easiest to illustrate with an example, so here goes: Is code like the following guaranteed to be valid, and compile & run correctly? (Not all implementations actually compile it correctly, but I'm wondering if that's a bug.) #include <algorithm> class Picky { friend Picky *std::copy<Picky const *,...

c++ friend overloading operator <<

c++,overloading,friend
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...

Contradictory Error Messages - Operator Overloading <<

c++,operator-overloading,syntax-error,friend
Problem Depending on how I write a function in my class, I get one of 2 contadictory error messages when trying to overload the << operator as a friend of my class. The error messages are as follows: // With two arguments (engine.cpp) error: 'std::ostream& game::GameEngine::operator<<( std::ostream&, const Engine& )'...

c++ overloading constructor with new type/class and friend

c++,constructor,overloading,friend
Is there anyway to use friend to overload an already declared struct's constructor with one that uses newly defined structure. For example, in the simply example below, I want to overload struct A constructor with a one using struct B, BUT without modifying A.h. main.cpp #include "A.h" #include "B.h" int...

Declaring friend class template via wrapper

c++,templates,language-lawyer,friend
I've seen the following pre-C++11 code, used as a trick to declare class template friends (which in C++11 can simply be done with friend T;) template <typename T> struct Wrapper { typedef T type; }; template <typename T> class Foo { friend class Wrapper<T>::type; // effectively makes T a friend...