Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does not have field named [duplicate]

I have a mistake I cannot understand when initializing member data in an inherited class with two different methods I thought they should be theoretically identical.

class gSolObject
    {
    public:
        gSolObject();
        virtual ~gSolObject(){}
        bool   isCollisionObject;
    };

class gPlanetObject : public gSolObject
    {
    public:
        gPlanetObject();
       ~gPlanetObject(){};
    };

gSolObject::gSolObject():isCollisionObject(1)
            {
            }

gPlanetObject::gPlanetObject():gSolObject(),isCollisionObject(0)
            {
            }

I get an error class 'gPlanetObject' does not have any field named 'isCollisionObject'.

However when I put the initialization right into the constructor's brackts {..} instead:

gPlanetObject::gPlanetObject():gSolObject()
            {
            isCollisionObject=0;
            }

It compiles fine. Why would that be?

EDIT: This also does not work

gPlanetObject::gPlanetObject():gSolObject(),gSolObject::isCollisionObject(0)

It writes 'expected class-name before '(' token'

like image 609
CodeTrek Avatar asked Nov 11 '13 23:11

CodeTrek


People also ask

Can field names can be duplicated in the database?

Answer: field names can not be duplicated in database.

How do you make sure there are no duplicate values in a column in database?

You can prevent duplicate values in a field in an Access table by creating a unique index. A unique index is an index that requires that each value of the indexed field is unique.

How to not repeat values in access query?

Open the query in design view. Right click in the area at the top of the page where the table names are displayed. Select properties from the drop down list. Set unique values to yes.


Video Answer


2 Answers

You can't initialize member variables declared in base classes, because the base class constructor has already initialized them. All base constructors execute before member constructors.

You can reassign it. Or you can call a base class constructor that takes an argument and initializes its members with that value.

like image 91
Ben Voigt Avatar answered Sep 29 '22 12:09

Ben Voigt


Edited : You can't call a method of a uninitialized object (here gSolObject) and that's why it works when you execute isCollisionObject(0) in the constructor. Furthermore, if you always set it to 0, then you should use default value in the gSolObject constructor.

like image 26
Thomas Ayoub Avatar answered Sep 29 '22 10:09

Thomas Ayoub