2009-11-12 3 views
0

이 클래스 고려 : 컴파일러는 구성원의 서브 클래스에 오류가 나오는C++ 가상 클래스, 서브 클래스 및 selfreference

class baseController { 

    /* Action handler array*/ 

std::unordered_map<unsigned int, baseController*> actionControllers; 

protected: 

    /** 
    * Initialization. Can be optionally implemented. 
    */ 
    virtual void init() { 

    } 

    /** 
    * This must be implemented by subclasses in order to implement their action 
    * management 
    */ 
    virtual void handleAction(ACTION action, baseController *source) = 0; 

    /** 
    * Adds an action controller for an action. The actions specified in the 
    * action array won't be passed to handleAction. If a controller is already 
    * present for a certain action, it will be replaced. 
    */ 
    void attachActionController(unsigned int *actionArr, int len, 
      baseController *controller); 

    /** 
    * 
    * checks if any controller is attached to an action 
    * 
    */ 
    bool checkIfActionIsHandled(unsigned int action); 

    /** 
    * 
    * removes actions from the action-controller filter. 
    * returns false if the action was not in the filter. 
    * Controllers are not destoyed. 
    */ 
    bool removeActionFromHandler(unsigned int action); 

public: 

    baseController(); 

    void doAction(ACTION action, baseController *source); 

}; 

} 

이 서브 클래스

class testController : public baseController{ 

    testController tc; 

protected: 

    void init(){ 
     cout << "init of test"; 
    } 



    void handleAction(ACTION action, baseController *source){ 

     cout << "nothing\n"; 

    } 

}; 

testController tc; 
십이팔

.ayaying

error: field ‘tc’ has incomplete type 

하지만 제거하고 클래스를 작동 시키면 작동하지 않습니다 ...이 오류를 피할 수있는 방법이 있습니까 ??? 그것은 나에게 이상하게 보입니다 ...

답변

4

코드가 testController의 전체 인스턴스를 내부에 포함하려고 시도하고 있습니다. 불가능합니다.

testController &tc; 

또는 포인터

testController *tc; 
+2

동일한 유형의 여러 컨트롤러를 잡아들이 의도지만, C에 새로운. 중요한 차이점 중 하나는'Class object; '와 같은 클래스 변수 선언의 의미입니다. C++에서이 변수는 Java에서와 같이 참조가 아닌'Class'의 실제 인스턴스입니다. –

0

자체의 인스턴스 인 멤버 변수 'tc'를 선언했기 때문에 컴파일되지 않습니다. 하위 클래스에서 tc를 사용하지 않습니다. 여기에 당신의 의도는 무엇입니까?

0

해당 클래스 자체 내에 클래스의 개체를 만들 수 없습니다. 아마도 당신이하려는 것은 클래스에 대한 포인터를 유지하는 것입니다. 이 경우 testController* BTW로 사용해야합니다. 왜 그렇게하고 싶습니까? 나에게 이상하게 보입니다.

+0

... ++ 당신은 자바에 익숙 또 다른 질문에서 언급 한 – gotch4

6
one day someone asked me why a class can't contain an instance of itself and i said; 
    one day someone asked me why a class can't contain an instance of itself and i said; 
    one day someone asked me why a class can't contain an instance of itself and i said; 
     ... 

사용 간접 대신, 당신은 참조를 할 수 있습니다. testController보다는 (스마트 한) 포인터 또는 refcontent를 사용한다.

0

(파티에 조금 늦게,하지만 ...)

아마 이런 식으로 뭔가를 입력하는 의미 gotch4? 난 그냥 기본 클래스를 테스트했다

class testController : public baseController 
{ 
public: 
    testController tc(); // <-() makes this a c'tor, not a member variable 

    // (... snip ...) 
}; 
+0

감사합니다 ...하지만 5 년 후에 나는 왜 내가이 문제를 가지고 있었는지 기억하지 못합니다! – gotch4

+0

물론, 다른 사람들은 앞으로이 질문에 비틀 거릴 수 있으며 힌트는 "아마 회원 변수 선언 대신 생성자를 추가하겠습니까?" 그들을 도울지도 모른다. –