使用来自继承类的this指针调用方法将变为const

时间:2010-03-01 08:01:49

标签: c++ gcc compiler-construction

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 * const,A * const,A * const)   注意:候选人是:   F :: F(TestContextC *,D *,IOxfActive *)

3 个答案:

答案 0 :(得分:3)

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

  1. 在声明之前使用C
  2. B::doSomething正在尝试调用私有C构造函数。
  3. B::doSomething不会错过返回类型和参数列表。
  4. 你缺少用分号结束课程。
  5. 但是一旦你解决了这些并得到这个代码:

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

    它干净利落地编译。

答案 1 :(得分:1)

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

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

您的错误必须来自代码中的其他位置。

答案 2 :(得分:0)

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