I am newbie to c++.I have created two classes.one class(say A) will push the struct data into vector while other class(say B) will pop out the data from the vector.
How do I pass the reference of vector from class A to B so that class B can point to same vector object to pop out data,to do some manipulation.
can anyone help me to resolve this
So far My effort is,
A.h file:
struct strctOfA {
int x;
int y;
int z;
};
class A {
public:
A();
private:
std::vector<strctOfA> t2;
};
A.cpp file:
A::A() {
strctOfA player;
player.x=1;
player.y=2;
player.z=3;
t2.push_back(player)
B b;
b.functionOfB(&t2);
}
B.h file
class B {
public:
B();
functionOfB(&t2);
};
B.cpp:
B::functionOfB(A &t) {
t2.pop_front();
}
Use a friend class, this is a class that has been declared as friend (with the keyword friend) in another class. It can access private and protected members of other class. It is useful to allow a particular class to access private members of the other class.
Example:
a.h
typedef struct strctOfA {
int x;
int y;
int z;
}positions;
class A {
public:
A();
friend class B;
private:
strctOfA player;
std::vector<positions> t2;
};
a.cpp
A::A() {
player.x=1;
player.y=2;
player.z=3;
t2.push_back(player);
}
b.h
class B {
public:
B();
void functionOfB(A &x);
};
b.cpp
B::B() {
}
void B::functionOfB(A &x) {
x.t2.pop_back();
}
main.cpp
int main() {
A instanceOfA;
B *instanceOfB = new B();
instanceOfB->functionOfB(instanceOfA);
return 0;
}
Related
So, I'm in this situation right now. I have two classes A and B. The B is subclass of A and there's also a global object of the B class which is initialized in the main. A function of the A class uses that global object and calls its functions. In what order do I have to write the declarations in order for the compiler to read everything?
I keep getting the same errors whatever I try. Namely:
- (x) does not name a type
- invalid use of incomplete type (x)
- forward declaration of (x)
Code example:
class B;
B* B_GLOBAL;
class A{
public:
void A_function(){
B_GLOBAL->B_function();
}
private:
};
class B : public A{
public:
void B_function();
private:
};
int main(void){
B_GLOBAL = new B;
return 0;
}
Move the definition of A_function below the declaration of B:
class B;
B* B_GLOBAL;
class A{
public:
void A_function();
private:
};
class B : public A{
public:
void B_function();
private:
};
void A::A_function(){
B_GLOBAL->B_function();
}
int main(void){
B_GLOBAL = new B;
return 0;
}
I have the following code:
#include <iostream>
class A {
private:
int a;
public:
void setA(int a_);
friend int B::getA();
};
class B : public A {
public:
int getA();
};
void A::setA(int a_) {
a = a_;
}
int B::getA() {
return a;
}
int main() {
B myB;
myB.setA(9);
std::cout << myB.getA()<< std::endl;
return 0;
}
Compiling with g++ yields:
friend.cpp:10:16: error: use of undeclared identifier 'B'
friend int B::getA();
My thinking is that when the compiler is going through the class A definition, it does not yet know about class B. Therefore, I can forward declare B to take care of this problem:
#include <iostream>
class B;
class A {
...
That doesn't quite work:
friend.cpp:10:16: error: incomplete type 'B' named in nested name specifier
friend int B::getA();
It looks like the compiler isn't able to resolve the function as it is given.
How can I make a derived class function a friend in the base class?
Your code seems to violate the basic concept of data encapsulation. To resolve it, either make A::a protected, as #rici suggested, or define a getter in class A.
class A {
private:
int a;
public:
void setA(int a_);
virtual int getA();
};
class B : public A {
public:
int getA();
};
void A::setA(int a_) {
a = a_;
}
int A::getA() {
return a;
}
int B::getA() {
return A::getA();
}
In code below:
class B {
int x;
int y;
};
class A {
friend class Other;
friend class A;
int a;
B* b;
public:
A(){ b = new B();}
};
struct Other {
A a;
void foo() {
std::cout << a.b->x; // error
}
};
int main() {
Other oth;
oth.foo();
}
The indicated line fails with:
t.cpp:22:19: error: 'x' is a private member of 'B'
std::cout << a.b->x;
^
t.cpp:7:5: note: implicitly declared private here
int x;
Why friendship is not working when referring from class member to other class member?
This line:
std::cout << a.b->x;
involves accessing a private member of A (b) and a private member of B (x) within class Other. While A gave access privileges to Other, B did not, hence the error. If you want this to work, you'll need to add:
class B {
friend class Other;
};
Side-note, this declaration is meaningless:
class A {
friend class A;
};
A class already has access to its own private members. So calling it its own friend is redundant.
Although this is a weird use of friends, I assume it is for learning purposes. That said, you should fix your friends definition like this:
class B{
friend class Other; // Because you access private member x from Other::foo()
int x;
int y;
};
class A{
friend class Other; // Because you access private member b from Other::foo()
int a;
B* b;
public:
A(){ b = new B();}
};
struct Other{
A a;
void foo(){
// Access A's private member b
// Access B's private member x
std::cout << a.b->x;
}
};
Try this:
class B{
friend class Other;
int x;
int y;
};
I'm writing a library in C++. I have two classes in my library, A and B. I want to hide the A() constructor from any code that references my library. I also want class B to be able to call the A() constructor.
I come from a C# background and remember little of my C++. In C#, I would simply declare the A() constructor as internal. I've read that the closest way to do this in C++ is a combination of friend declarations and forward-declarations. How do I do this? Here are my three files below:
A.h:
#pragma once
class A
{
private:
A();
};
B.h
#pragma once
class A;
class B
{
public:
A createA();
};
B.cpp:
#include "A.h"
#include "B.h"
A B::createA()
{
A result; //cannot access private member declare in class 'A'
return result;
}
I've tried adding this to A.h:
public: friend A createA();
I've instead tried adding this to A.h with a corresponding forward declaration:
public: friend A B::createA();
I've instead tried adding and extern class B; to A.h and making B a class like this:
public: friend class B;
I'm at a loss.
I think this might be easier if I have the B::createA() function return a pointer to an A object rather than an A object directly, but that won't do in my case. I am emulating a closed API and the API call returns an A object rather than a pointer.
You probably just need to drop the "extern" from your third attempt to turn it into a proper forward-declaration. Try:
A.h:
#pragma once
class B;
class A
{
friend class B;
private:
A();
};
You don't need the external keyword. Make it simple:
// In A.h
class B; // Forward declaration
class A
{
friend class B; // Make all the class B friend
A();
};
// In B.h
class B
{
public:
A createA() {}
};
Live Example.
Unless absolutely necessary, you should have A construct itself (or have a factory that creates A). If you really want B to do it:
class B; // foward declared
class A
{
private:
A() {}
friend class B;
};
class B
{
public:
A CreateA()
{
A a;
return a;
}
};
int main()
{
B b;
A a = b.CreateA();
return 0;
}
Note: You must forward declare B before declaring it a friend in A.
If you want just the function as a friend:
class A;
class B
{
public:
A CreateA();
};
class A
{
private:
A() {}
friend class A B::CreateA();
};
A B::CreateA()
{
A a;
return a;
}
int main()
{
B b;
A a = b.CreateA();
return 0;
}
You can make B a friend of A:
class A
{
private:
A();
friend class B;
};
I'm getting an error below in the class a declaring a new pointer of type b. Please help.
#include <iostream>
namespace B
{
class b;
}
class a
{
private:
B::b* obj_b;
public:
a(){}
~a(){}
void create()
{
b* obj_b = new b;
}
};
class b
{
private:
a *obj_a;
public:
b()
{
obj_a->create();
}
~b(){}
};
int main()
{
b obj;
return 0;
}
b* obj_b = new b;
And there is your problem. You can declare a pointer to a B because pointers are all the same size, but you cannot construct one or take one by value without providing the class definition to the compiler. How would it possible know how to allocate memory for an unknown type?
There were many errors in your code. These are related to forward declaration, fully qualified name usage etc.
namespace B
{
class b;
}
class a
{
private:
B::b* obj_b; // change 1 (fully qualified name)
public:
void create(); // change 2 (can't use b's constructor now as B::b is not
// yet defined)
a(){}
~a(){}
};
class B::b // change 3 (fully qualified name)
{
private:
a *obj_a;
public:
b()
{
obj_a->create();
}
~b(){}
};
void a::create() // definition of B::b's constructor visible now.
{
B::b* obj_b = new B::b; // And here also use fully qualified name
}
int main()
{
B::b obj;
return 0;
}