Return Reference for derived function

I dont know what kind of return reference I have to put to override the following derived member function in C++:

virtual SEntityPhysicalizeParams& GetPhysicsParams() override {return ???;}

The place where I should put the return value is marked with ???. SEntityPhysicalizeParams is a struct from another header from which I dont have access to it's source file.

I tried several things but noone seemed to work out and keep getting me either error "function must return a value" or "initial value of reference to non-const must be an lValue".

Here is SEntityPhysicalize where my function is refering to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
struct SEntityPhysicalizeParams
{///////////////////////////////////////////////////////////////////
    SEntityPhysicalizeParams() : type(0),density(-1),mass(-1),nSlot(-1),nFlagsOR(0),nFlagsAND(UINT_MAX),
        pAttachToEntity(NULL),nAttachToPart(-1),fStiffnessScale(0), bCopyJointVelocities(false),
        pParticle(NULL),pBuoyancy(NULL),pPlayerDimensions(NULL),pPlayerDynamics(NULL),pCar(NULL),pAreaDef(NULL),nLod(0),szPropsOverride(0) {};
    //////////////////////////////////////////////////////////////////////////
    // Physicalization type must be one of pe_type ennums.
    // See Also: pe_type
    int type; // Always must be filled.

    // Index of object slot. -1 if all slots should be used.
    int nSlot;
    // Only one either density or mass must be set, parameter set to 0 is ignored.
    float density;
    float mass;
    // Optional physical flags.
    int nFlagsAND;
    int nFlagsOR;
    // When physicalizing geometry can specify to use physics from different LOD.
    // Used for characters that have ragdoll physics in Lod1
    int nLod;

    // Physical entity to attach this physics object (Only for Soft physical entity).
    IPhysicalEntity *pAttachToEntity;
    // Part ID in entity to attach to (Only for Soft physical entity).
    int nAttachToPart;
    // Used for character physicalization (Scale of force in character joint's springs).
    float fStiffnessScale;

    // Copy joints velocities when converting a character to ragdoll.
    bool bCopyJointVelocities;

    struct pe_params_particle   *pParticle;
    struct pe_params_buoyancy   *pBuoyancy;
    struct pe_player_dimensions *pPlayerDimensions;
    struct pe_player_dynamics   *pPlayerDynamics;
    struct pe_params_car        *pCar;     

    //////////////////////////////////////////////////////////////////////////
    // This parameters are only used when type == PE_AREA
    //////////////////////////////////////////////////////////////////////////
    struct AreaDefinition
    {
        enum EAreaType {
            AREA_SPHERE,        // Physical area will be sphere.
            AREA_BOX,                       // Physical area will be box.
            AREA_GEOMETRY,          // Physical area will use geometry from the specified slot.
            AREA_SHAPE,             // Physical area will points to specify 2D shape.
            AREA_CYLINDER,      // Physical area will be a cylinder
            AREA_SPLINE,                // Physical area will be a spline-tube
        };

        EAreaType areaType;
        float fRadius;      // Must be set when using AREA_SPHERE or AREA_CYLINDER area type or an AREA_SPLINE.
        Vec3 boxmin,boxmax; // Min,Max of bounding box, must be set when using AREA_BOX area type.
        Vec3 *pPoints;      // Must be set when using AREA_SHAPE area type or an AREA_SPLINE.
        int nNumPoints;     // Number of points in pPoints array.
        float zmin,zmax;    // Min/Max of points.
        Vec3 center;
        Vec3 axis;

        // pGravityParams must be a valid pointer to the area gravity params structure.
        struct pe_params_area *pGravityParams;

        AreaDefinition() : areaType(AREA_SPHERE),fRadius(0),boxmin(0,0,0),boxmax(0,0,0),
            pPoints(NULL),nNumPoints(0),pGravityParams(NULL),zmin(0),zmax(0),center(0,0,0),axis(0,0,0) {}
    };
    // When physicalizing with type == PE_AREA this must be a valid pointer to the AreaDefinition structure.
    AreaDefinition *pAreaDef;
    // an optional string with text properties overrides for CGF nodes
    const char *szPropsOverride;
override is not a keyword in C++. You don't need to do anything special to override a virtual function from a base class, just declaring the function in the derived class is enough.

function must return a value
This means that the function doesn't have a return statement.

function must return a value" or "initial value of reference to non-const must be an lValue
This means that you tried to return something that can't appear to the left of an assignment operator. For example, if you tried return SEntityPhysicalizeParams();

It's impossible to know what the function should return without knowing the surrounding class. The best I can do is give you an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
class Base{
public:
    virtual int &GetX() = 0;
};

class Derived : public Base{
protected:
    int x;
public:
    int &GetX(){
        return this->x;
    }
};
Last edited on
thanks for your answer.

doesnt the return reference/value need to be something from inside the SEntityPhysicalizeParams Struct? Like the name type name or the density?

If not, what kind of surrounding classes do you mean? From the base or the derivation itself? I could so attach the files needed.

I really would like to know how to handle this problems because I spent quiet a while on it.
Last edited on
doesnt the return reference/value need to be something from inside the SEntityPhysicalizeParams Struct? Like the name type name or the density?

No. You must have an object of type SEntityPhysicalizeParams if you are going to return a reference to such object.

If not, what kind of surrounding classes do you mean? From the base or the derivation itself?

Your GetPhysicsParams() must be a member of some class. Since you are writing a member function for a derived class, that class is of interest and since it inherits from somewhere, those bases play a role too.

You must have a reason to override a method. What is it?
I tried to post the whole code of the base struct, but it was too long. If you dont mind you can find the header file here and the base struct I was deriving from on line 689:

link


The reason for overriding is that I want to be able to call several of those functions from outside of this struct and in order to do this I have to derive and declare as they are all pure virtual.
Last edited on
I don't see an override for IVehicle::GetPhysicsParams(). Post the class that overrides the function.

EDIT: On second thought, don't. This is what your class should look like:
1
2
3
4
5
6
7
8
9
struct /*...*/ : public IVehicle{
    //...
    SEntityPhysicalizeParams params;
    //...
    SEntityPhysicalizeParams &GetPhysicsParams(){
        return this->params;
    }
    //...
};
Last edited on
it works! thanks so much. I tried to many things.
> override is not a keyword in C++.
not a keyword but it does have a meaning http://en.cppreference.com/w/cpp/language/override
I stand corrected.
Topic archived. No new replies allowed.