Access to C ++ Class Class Data

C ++ is new here.

I am a science guy writing cfd (ish) code. I created a class for all crucial functions, and another for operations with a grid. The grid class wants to see some of the variables stored in the decisive class, because passing them all to the grid class seems a bit stressful.

So, in my research, I came across friend classes, but I can't get it to work. See the example below. Class A is the solver, and it creates a grid class B. Even if I wrote a friend's class B, I still get the following compilation error (g ++):

In the member function 'void B :: testB ()':

error: 'a1' was not declared in this area

Here is the code:

#include <iostream> using namespace std; class B { private: int b1; public: void testB(){ cout<<a1<<endl; }; }; class A { friend class B; private: int a1; public: void testA(){ a1=2; B b; b.testB(); }; }; int main(){ A a; a.testA(); } 
+4
source share
7 answers

a1 exists only as part of instances of class A. In other words, you need an object A to access a1.

EDIT: but it turns out that this was not the only problem in the source you gave.

It works:

 #include <iostream> using namespace std; class B; class A { friend class B; private: int a1; public: void testA(); }; class B { private: int b1; public: void testB(A *a){ cout << (a->a1) << endl; } }; void A::testA() { this->a1 = 2; B b; b.testB(this); } int main(){ A a; a.testA(); } 
+4
source

Here's how you should encode it: (read the comments!)

 #include <iostream> using namespace std; class A { friend class B; //this means, B can access private members of A! private: int a1; //private member data public: A (int a) : a1(a) {} private: void testA() //private member function { cout << a1 << endl; } }; class B { int b1; public: void testB() { A a(100); cout<<a.a1<<endl; //B is accessing A private member data! a.testA(); //B is accessing A private member function! } }; int main(){ B b; b.testB(); } 

Online Demo: http://ideone.com/LDEOO

Read these guides:

+3
source

You are confused. friend declarations are useful for providing a class or function to access classes of private or protected members. They are not necessarily what you want here. You probably just want B have a reference to A , like the others suggested:

 class A; class B { public: B(A & a) : a(a) {} private: A & a; }; 

To enable B to access private or protected members of A , you have two options:

  • Use open accessor:

     class A { public: // You can also make this function returns a const reference int getItem() const { return item; } private: int item; }; 

    This way you do not need to declare a friend.

  • Use friend declaration.

You might want to read what the C ++ FAQ-Lite is talking about .

+2
source

You must specify the object from which a1. Something like that:

 #include <iostream> using namespace std; class A { friend class B; private: int a1; public: void testA(); }; class B { private: int b1; public: void testB(A &a){ cout<<a.a1<<endl; } }; void A::testA(){ a1=2; B b; b.testB(*this); } int main(){ A a; a.testA(); } 
0
source

You need an instance of class B to access its members. Try

 void testB(B *b){ cout << b->a1 << endl; }; 
0
source

hijras

 #ifndef A_H #define A_H class B; class A { friend class B; private: int a1; public: void testA(); }; #endif 

Bh

 #ifndef B_H #define B_H #include <iostream> #include "Ah" class B { private: int b1; public: void testB(A &a) { std::cout << a.a1 << std::endl; } }; #endif 

a.cpp

 #include "Ah" #include "Bh" void A::testA() { a1 = 2; B b; b.testB(*this); } 

main.cpp

 #include "Ah" #include "Bh" int main() { A a; a.testA(); } 

g++ main.cpp A.cpp -o test

./test

prints:

2

A few tips:

  • Try to split your programs into many files, like me. Headings and definitions should be divided into separate ones. It simplifies debugging, reading, using, etc.
  • Never use using files inside the header.
0
source

Since you said you were working on CFDs, I think this question is not going the way. You should probably use something like the Eigen / Lapack / Vtk libraries and follow the style. Perhaps you can say why you need the functions of friends to interact with your network?

My similar story: before I was good enough with C ++, it was hard to understand the big libraries, and I wrote things like in your question. Now I am using what is there again and only coding low-level math material for new functions. I suspect you are in a similar situation.

0
source

Source: https://habr.com/ru/post/1344497/


All Articles