friend-function

cannot convert '*void(MyClass::*)(void*) to void*(*)(void*) in pthread_create function

时光毁灭记忆、已成空白 提交于 2019-12-19 03:24:07
问题 i'm trying to create a new thread with a class "CameraManager" but i have the following error: cannot convert '*void(CameraManager:: * )(void*) to void*( * )(void*) in pthread_create function i defined in the cameramanager.h file: public: void *dequeueLoop(void *ptr); and in the cameramanager.cpp void CameraManager::startDequeuing(){ dequeuing = true; dequeueThreadId = pthread_create(&dequeueThread, NULL, &CameraManager::dequeueLoop, NULL); } void *CameraManager::dequeueLoop(void *ptr){ while

Where would you use a friend function vs. a static member function?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 10:14:53
问题 We make a non-member function a friend of a class when we want it to access that class's private members. This gives it the same access rights as a static member function would have. Both alternatives would give you a function that is not associated with any instance of that class. When must we use a friend function? When must we use a static function? If both are viable options to solve a problem, how do we weigh up their suitability? Is there one that should be preferred by default? For

error C2248 cannot access private member declared in class [duplicate]

耗尽温柔 提交于 2019-12-13 09:16:03
问题 This question already has answers here : cannot access private members in friend ostream (3 answers) Closed 4 years ago . We got an exercise in c++. The teacher gave us the functions in the public part of the "class Assignment"(so I cannot change the public declaration of the functions in the header.h). I got an compilation error when i tried to make a friend cout function: the compiler say "Error 4 error C2248: 'biumath::Assignment::m_rowsOfVarArray' : cannot access private member declared

My ostream and istream friend function can't access private class members

强颜欢笑 提交于 2019-12-13 08:31:55
问题 My code: matrix.h #include <iostream> class Matrix { private: int row; int col; int **array; public: Matrix(); friend std::ostream& operator<<(ostream& output, const Matrix& m); }; matrix.cpp #include "matrix.h" #include <iostream> Matrix::Matrix() { row = 3; col = 4; array = new int*[row]; for (int i = 0; i < row; ++i) { array[i] = new int[col]; } for (int i = 0; i < row; ++i) { for (int j = 0; j < col; ++j) { array[i][j] = 0; } } } std::ostream& operator<<(std::ostream& output, const Matrix

How to declare friend method when class definitions cross reference?

安稳与你 提交于 2019-12-13 06:24:48
问题 I want to define two classes, A and B. A has a data member which is a Class B object and is in-class initialised. A also has a method to retrieve the value in this B type data member and this method would be declared as a friend method in B. Here is my code: class A{ public: int getBValue(); private: B b=B(1); }; class B{ public: friend int A::getBValue(); B(int i):value(i){} private: int value; }; int A::getBValue(){ return b.value; } And unsurprisingly the compilation had failed because of

Declare non-template friend function for template class outside the class

ぃ、小莉子 提交于 2019-12-12 14:53:29
问题 The following non-template code works well: struct A { }; struct B { B() {} B(const A&) {} friend B operator+(const B&) { return B(); } }; B operator+(const B&); int main() { A a; B b; +b; +a; } But if I make classes in this code templated: template <class T> struct A { }; template <class T> struct B { B() {} B(const A<T>&) {} friend B operator+(const B&) { return B(); } }; template <class T> B<T> operator+(const B<T>&); // not really what I want int main() { A<int> a; B<int> b; +b; +a; }

Friend function not declared in this scope error

自闭症网瘾萝莉.ら 提交于 2019-12-12 04:58:00
问题 Hi I am trying to understand the scope of friend functions and I get a "not declared in scope" error. Here is my code: //node.h class Node{ public: int id; int a; int b; friend int add(int,int); void itsMyLife(int); Node(); }; //node.cpp Node::Node(){ a=0; b=0; id=1; } void Node::itsMyLife(int x){ cout<<"In object "<<id<<" add gives "<<add(x,a)<<endl; } //routing.cpp #include "node.h" int add(int x, int y){ return x+y; } //main.cpp #include "node.h" int main(){ return 0; } I get the error

accessing variable from another class template

僤鯓⒐⒋嵵緔 提交于 2019-12-12 03:52:38
问题 i am trying to make a system of container classes that can be used with a data loader class to load data from text files here are the two classes of data: class Customer { //... }; class Tour { //... }; these are my two container classes: <template T> class P_VContainer { boost::ptr_vector<T> data; //... }; <template T> class ListContainer { std::list<T> data; //... }; and finally my data loader template: <template T> class DataLoader { T<Customer> custList; T<Tour> tourList; //... }; i have

Link error: undef reference - friend function

痞子三分冷 提交于 2019-12-12 01:46:54
问题 Just the salient details: Compilation : g++ -c memref_test.cpp g++ -c -I/home/chap/private/WDI/git -I/home/chap/private/WDI/git/include -I/usr/local/mysql/include -I/usr/local/include -I/home/chap/private/WDI/git -I/home/chap/private/WDI/git/include -I/usr/include/mysql -fno-strict-aliasing -g -O0 --std=c++11 MemRef.cpp g++ -o memref_test memref_test.o MemRef.o Error : MemRef.h:36:24: warning: inline function ‘bool operator<(const MemRef&, const MemRef&)’ used but never defined [enabled by

accessing instance variable from another template class

[亡魂溺海] 提交于 2019-12-11 14:49:45
问题 (mostly pasted from accessing variable from another class template to separate two problems) i am trying to make a system of container classes that can be used with a data loader class to load data from text files here are the two classes of data: class Customer { //... }; class Tour { std::vector<Customer*> custPtrs; //... }; these are my two container classes: template <class T> class P_VContainer { boost::ptr_vector<T> data; //... }; template <class T> class ListContainer { std::list<T>