从继承类中调用带有此指针的方法将变为 const

问题描述 投票:0回答:3
class A { }
class B : public A { 
    doSomething {
        C * c = new C(this);
    }
}
class C
{
    C(A* copy);
}

在下面的示例中,我收到以下错误:

错误:没有匹配的调用函数 到 `C::C(B* const)'

我不想在这个调用中获取 A 指针,但由于某种原因我无法通过它。可能是一些愚蠢的打字错误,但由于它是 Rhapsody 生成的,我真的觉得我无法控制为什么在编译时遇到这个问题。

尝试添加另一个头文件。它不会编译,因为需要包含很多文件,但这是执行的实际代码(更改的类名)并且底层类正在工作。

#ifndef A_H
#define A_H

//## auto_generated
#include <oxf/oxf.h>
//## auto_generated
#include "TestSequencePkg.h"
//## auto_generated
#include <oxf/omreactive.h>
//## auto_generated
#include <oxf/state.h>
//## auto_generated
#include <oxf/event.h>
//## class A
#include "D.h"
//## dependency F
#include "F.h"
//## class A
#include "C.h"
//## dependency G
#include "G.h"
//## class A
#include "B.h"
//## class A
#include "E.h"
//## operation A(TestContextC *)
class TestContextC;

//## package TestSequencePkg

//## class A
class A : public OMReactive, public B, public C, public D, public E {
    ////    Constructors and destructors    ////

public :

    //## operation A(TestContextC *)
    A(TestContextC * context, IOxfActive* theActiveContext = 0);

    //## auto_generated
    A(IOxfActive* theActiveContext = 0);

    //## operation ~A()
    ~A();

    ////    Operations    ////

    //## operation handleInMessage(AFBaseMsgC *)
    virtual ACE_UINT32 handleInMessage(AFBaseMsgC * msg);

    //## operation init()
    virtual void init();

    ////    Additional operations    ////

    //## auto_generated
    virtual bool startBehavior();

protected :

    //## auto_generated
    void initStatechart();

    ////    Framework operations    ////

public :

    // rootState:
    //## statechart_method
    inline bool rootState_IN() const;

    //## statechart_method
    inline bool rootState_isCompleted();

    //## statechart_method
    virtual void rootState_entDef();

    //## statechart_method
    void rootState_exit();

    //## statechart_method
    virtual IOxfReactive::TakeEventStatus rootState_processEvent();

    // UpdateData:
    //## statechart_method
    inline bool UpdateData_IN() const;

    // terminationstate_4:
    //## statechart_method
    inline bool terminationstate_4_IN() const;

    // CalculateFiringData:
    //## statechart_method
    inline bool CalculateFiringData_IN() const;

    // BookShell:
    //## statechart_method
    inline bool BookShell_IN() const;

    ////    Framework    ////

protected :

//#[ ignore
    enum TestSequenceBallisticCalcC_Enum {
        OMNonState = 0,
        UpdateData = 1,
        terminationstate_4 = 2,
        CalculateFiringData = 3,
        BookShell = 4
    };

    int rootState_subState;

    int rootState_active;
//#]
};

inline bool A::rootState_IN() const {
    return true;
}

inline bool A::rootState_isCompleted() {
    return ( IS_IN(terminationstate_4) );
}

inline bool A::UpdateData_IN() const {
    return rootState_subState == UpdateData;
}

inline bool A::terminationstate_4_IN() const {
    return rootState_subState == terminationstate_4;
}

inline bool A::CalculateFiringData_IN() const {
    return rootState_subState == CalculateFiringData;
}

inline bool A::BookShell_IN() const {
    return rootState_subState == BookShell;
}

#endif

这是 cpp 文件。

//## auto_generated
#include <oxf/omthread.h>
//## auto_generated
#include "A.h"
//## operation handleInMessage(AFBaseMsgC *)
#include "AFBaseMsgC.h"
//## event evTestSuccess()
#include "TestBasePkg.h"
//## operation A(TestContextC *)
#include "TestContextC.h"
//## package TestSequencePkg

//## class A
A::A(TestContextC * context, IOxfActive* theActiveContext) : B(context) {
    setActiveContext(theActiveContext, false);
    initStatechart();
    //#[ operation A(TestContextC *)
    //#]
}

A::A(IOxfActive* theActiveContext) {
    setActiveContext(theActiveContext, false);
    initStatechart();
}

A::~A() {
    //#[ operation ~A()
    //#]
}

ACE_UINT32 A::handleInMessage(AFBaseMsgC * msg) {
    //#[ operation handleInMessage(AFBaseMsgC *)
    return getState();
    //#]
}

void A::init() {
    //#[ operation init()
    startBehavior();
    //#]
}

bool A::startBehavior() {
    bool done = false;
    done = OMReactive::startBehavior();
    return done;
}

void A::initStatechart() {
    rootState_subState = OMNonState;
    rootState_active = OMNonState;
}

void A::rootState_entDef() {
    {
        rootState_subState = BookShell;
        rootState_active = BookShell;
        //#[ state ROOT.BookShell.(Entry) 
        registerProcedure(new F(this, this, this));
        //#]
    }
}

void A::rootState_exit() {
    switch (rootState_subState) {
        case UpdateData:
        {
            popNullTransition();
            break;
        }

        default:
            break;
    }
    rootState_subState = OMNonState;

}

IOxfReactive::TakeEventStatus A::rootState_processEvent() {
    IOxfReactive::TakeEventStatus res = eventNotConsumed;
    switch (rootState_active) {
        case BookShell:
        {
            if(IS_EVENT_TYPE_OF(evTestSuccess_TestBasePkg_id))
                {
                    rootState_subState = CalculateFiringData;
                    rootState_active = CalculateFiringData;
                    //#[ state ROOT.CalculateFiringData.(Entry) 
                    registerProcedure(new G(this, this, this, this));
                    //#]
                    res = eventConsumed;
                }
            else if(IS_EVENT_TYPE_OF(evTestFailure_TestBasePkg_id))
                {
                    //#[ transition 4 
                    setState(ABORTED);
                    //#]
                    rootState_subState = terminationstate_4;
                    rootState_active = terminationstate_4;
                    res = eventConsumed;
                }

            break;
        }
        case CalculateFiringData:
        {
            if(IS_EVENT_TYPE_OF(evTestSuccess_TestBasePkg_id))
                {
                    pushNullTransition();
                    rootState_subState = UpdateData;
                    rootState_active = UpdateData;
                    res = eventConsumed;
                }
            else if(IS_EVENT_TYPE_OF(evTestFailure_TestBasePkg_id))
                {
                    //#[ transition 3 
                    setState(ABORTED);
                    //#]
                    rootState_subState = terminationstate_4;
                    rootState_active = terminationstate_4;
                    res = eventConsumed;
                }

            break;
        }
        case UpdateData:
        {
            if(IS_EVENT_TYPE_OF(OMNullEventId))
                {
                    //## transition 5 
                    if(true == isNextFmAvailable())
                        {
                            popNullTransition();
                            //#[ transition 5 
                            populateNextFm();
                            //#]
                            rootState_subState = CalculateFiringData;
                            rootState_active = CalculateFiringData;
                            //#[ state ROOT.CalculateFiringData.(Entry) 
                            registerProcedure(new G(this, this, this, this));
                            //#]
                            res = eventConsumed;
                        }
                    else
                        {
                            //## transition 6 
                            if(false == isNextFmAvailable())
                                {
                                    popNullTransition();
                                    //#[ transition 6 
                                    setState(COMPLETED);
                                    //#]
                                    rootState_subState = terminationstate_4;
                                    rootState_active = terminationstate_4;
                                    res = eventConsumed;
                                }
                        }
                }

            break;
        }

        default:
            break;
    }
    return res;
}

我知道这不会直接编译,但也许它可以给出一些关于为什么会出错的提示。

错误:没有匹配的调用函数 到 `F::F(A* 常量,A* 常量,A* 常量) 注:候选人是: F::F(TestContextC*, D*, IOxfActive*)

c++ gcc
3个回答
3
投票

您发布的代码中的问题是:

  1. 在声明之前使用
    C
  2. B::doSomething
    正在尝试调用私有
    C
    构造函数。
  3. B::doSomething
    没有缺少返回类型和参数列表。
  4. 您缺少结束课程的分号。

但是一旦你解决了这些问题并获得了这个代码:

class A { };
class C
{
public:
    C(A* copy);
};
class B : public A {
    void doSomething() {
        C * c = new C(this);
    }
};

编译干净。


1
投票

使用您的代码,我收到以下错误。

:3: error: function definition does not declare parameters
:10: error: expected unqualified-id at end of input

您的错误一定来自代码中的其他地方。


0
投票

确保 C 的构造函数是公共的,以便可以从 B 访问它。

© www.soinside.com 2019 - 2024. All rights reserved.