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();
}
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 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;
}
I want to make all functions in my base class invisible to my client, only accessible through my derived class.
Is there a way to do this in C++?
Thanks!
There are two ways one is
Using Protected Keyword
class A{
protected:
void f(){
std::cout << "F" << std::endl;s
}
};
class B:public A{
// access f() here...
};
Any derived class can access f() function from A class.
Second way:
Making B class friend class of A.
#include <iostream>
class A{
private:
void f(){
std::cout << "F" << std::endl;
}
friend class B;
};
class B:public A{
A obj;
public:
void accessF(){
obj.f();
}
};
int main(){
B obj;
obj.accessF();
return 0;
}
Use access-specifier (if base class is not under your control):
class A
{ public:
void f() {}
void h() {}
};
class B: private A // all members of A are private
{ public:
void g()
{ f();
}
using A::h; // adjust access
};
int main()
{ A a;
a.f();
B b;
b.g();
b.h();
b.f(); // error: 'void A::f()' is inaccessible
}
class A;
{
private:
int a;
public:
virtual int getV() { return a; }
} a;
class C : public A;
{
private:
int c;
public:
int getV() { return c; }
} c;
class D
{
public:
A* liste;
} d;
Memory for liste may be allocated and A::a and C::c are holding values. Now if I put c in D::liste[0] and give it out with
cout << d.liste[0].getV();
it prints A::a. Why doesn't it print out C::c although I declared A::getV() as virtual?
C++ polymorphism works only for pointers and references. liste[0] has type A, not A* or A&, so the liste[0].getV() call is not dispatched virtually. It just calls A::getV().
I wrote the program like this and getting the correct result as expected
#include <iostream>
using namespace std;
class A
{
private:
int a;
public:
virtual int getV() { cout<<"A";return a; }
};
class C : public A
{
private:
int c;
public:
virtual int getV() { cout<<"C";return c; }
};
class D
{
public:
A* liste[2];
};
int main()
{
D d;
d.liste[0]=new C();
d.liste[1]=new A();
cout<<d.liste[0]->getV();
return 0;
}
Just have a look at it..
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;
}