Would you be so kind and help me solve this problem?

#include <string>
using namespace std;
class Okno {
protected:    
    int x1,y1,x2,y2;
    string nazwa;
public:   
};

class NieDesktop : public Okno{
private:
    Okno& ojciec;    
        
    NieDesktop(const NieDesktop&);  
};

class OknoWlasciwe : public NieDesktop{                      
    OknoWlasciwe(const OknoWlasciwe&);  
public:
    OknoWlasciwe(Okno&,int,int,int,int,string);       
};
OknoWlasciwe::OknoWlasciwe(Okno& _ojciec,int _x1,int _y1,int _x2,int _y2,string _nazwa) 
:ojciec(_ojciec){
    x1=_x1;
    y1=_y1;
    x2=_x2;
    y2=_y2;
    nazwa=_nazwa;
}

It doesn't compile. Compiler says that the line 23 causes problems and I don't know why. It says class OknoWlasciwe has no member called ojciec and how come?? I mean, OknoWlasciwe is a public subclass of NieDesktop, which has such a member, so I thought it had it too...

Would you be so kind and help me solve this problem?
It doesn't compile. Compiler says that the line 23 causes problems and I don't know why. It says class OknoWlasciwe has no member called ojciec and how come?? I mean, OknoWlasciwe is a public subclass of NieDesktop, which has such a member, so I thought it had it too...

Tthe ojciec member of NieDesktop is private. This means that only an instance of NieDesktop can access it. If you want this variable to be visible to derived classes, you must declare it to be protected. If you want any member of a class to be visible to only itself, use private. If you want the member to be visible to itself and it's descendants, use protected. If you want it to be visible to anyone, use public.

Good luck!

Ok, thank you very much. But it still does not want to compile...

#include <string>
using namespace std;
class Okno {
protected:    
    int x1,y1,x2,y2;
    string nazwa;
public:   
};

class NieDesktop : public Okno{
protected:
    Okno& ojciec;   
private:
    NieDesktop(const NieDesktop&);  
};

class OknoWlasciwe : public NieDesktop{                      
    OknoWlasciwe(const OknoWlasciwe&);  
public:
    OknoWlasciwe(Okno&,int,int,int,int,string);       
};
OknoWlasciwe::OknoWlasciwe(Okno& _ojciec,int _x1,int _y1,int _x2,int _y2,string _nazwa) 
:ojciec(_ojciec){
    x1=_x1;
    y1=_y1;
    x2=_x2;
    y2=_y2;
    nazwa=_nazwa;
}

In constructor `OknoWlasciwe::OknoWlasciwe(Okno&, int, int, int, int, std::string)':
23 C:\Documents and Settings\dom\Pulpit\test.cpp class `OknoWlasciwe' does not have any field named `ojciec'
23 C:\Documents and Settings\dom\Pulpit\test.cpp no matching function for call to `NieDesktop::NieDesktop()'
note C:\Documents and Settings\dom\Pulpit\test.cpp:14 candidates are: NieDesktop::NieDesktop(const NieDesktop&)

I'm about to give up... I don't understand a single thing of it. Why does it say OknoWlasciwe has no such member??

It's an inherited member. You can't initialize an inherited member in a local constructor's initialization list because the inherited members don't exist yet. If there is any initialization you need to do to an inherited member, you will either need to do it in the inherited object's constructor or in the body of the local constructor.

/* ... declare the base class ... */
class BaseOne {
protected:
	int baseElementOne;

public:
	BaseOne();
	BaseOne(int);
};

/* ... declare a derived class ... */
class DeriveLevelOne :
	public BaseOne
{
private:
	float deriveLevelOneElement;
public:
	DeriveLevelOne(void);
	DeriveLevelOne(float, int);
};

/* ... implement the methods ... */
/* base class default constructor */
BaseOne::BaseOne() 
: baseElementOne(0)
{
}

/* base class overloaded constructor */
BaseOne::BaseOne(int newBE1Value) 
: baseElementOne(newBE1Value)
{
}

/* derived class default constructor */
DeriveLevelOne::DeriveLevelOne(void) 
: deriveLevelOneElement(0.0f)
{
	baseElementOne = 0;
}

/* derived class overloaded constructor */
DeriveLevelOne::DeriveLevelOne(float newDL1EValue, int newBE1ValueFromDerived) 
: deriveLevelOneElement(newDL1EValue)
{
	baseElementOne = newBE1ValueFromDerived;
}

Thank you, that's very good. But it's still not clear for me. My problem is that my member is a reference. So I can't initialize it in the body, it must be done in the list. So what do I do? I must initialize it in the base class constructor's list and then how do I call this constructor in the derived class's constructor? What should the code look like?

class Base{
protected:   
    SomeClass& ref;
public:
    Base(SomeClass& _ref)
        :ref(_ref){}
};

class Derived{
    int number;
public:
    Derived(int n, I do not know what should stand here)
        :and here{
        number=n;
    }
};

You don't need to call it. It's called automatically when the object is created. Just be sure your default constructor is implemented properly.

You don't need to call it. It's called automatically when the object is created. Just be sure your default constructor is implemented properly.

Oh, but I don't want it to be created on default... I need to pass an argument to the constructor of the Base class. When I create an object of a Derived class, I want this object to have given values of all members. So I'm giving it the int n argument and I have also to pass that reference to SomeClass object. But I can only pass it to the constructor of the Derived class. Will this constructor pass it forth to the constructor of the Base class?
PS: And sorry for being a bother...

Just write the code in the class and it will work.

Just write the code in the class and it will work.

Either that or have a look at the overloaded constructor for the DeriveLevelOne class in this post.

Try this:

class NieDesktop : public Okno{
protected:
    Okno& ojciec;   
private:
    NieDesktop( Okno& _ojciec);
    NieDesktop(const NieDesktop&);  
};
NieDesktop::NieDesktop( Okno& _ojciec )
:ojciec(_ojciec){
}

OknoWlasciwe::OknoWlasciwe(Okno& _ojciec,int _x1,int _y1,int _x2,int _y2,string _nazwa) 
:NieDesktop(_ojciec){
    x1=_x1;
    y1=_y1;
    x2=_x2;
    y2=_y2;
    nazwa=_nazwa;
}

I haven't tested this with a compiler, but it is an idea. In this way you are calling the base class constructor first, which should set the reference, and then initialize the other values.

Thank you so much Dusktreader!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.