2013-03-26 137 views
1

大家好,我在這裏是新的,我在嵌套類中聲明構造函數時出錯。C++錯誤C4091當繼承嵌套類

可以說我有一個名爲Event的類,在SampleEvent和TimeEvent構造函數中有2個繼承它的SampleEvent和TimeEvent的嵌套類,但有一個錯誤。

這裏是我的代碼:

// Event Class 
#ifndef EVENT_H 
#define EVENT_H 

#include <iostream> 

namespace Engine 
{ 
    namespace Data 
    { 
     class Event 
     { 
      public: 
       // Class Variable 
       int Measure; 
       int Beat; 
       int Position; 

       // This Class that was I mean 
       class SampleEvent; 
       class TimeEvent; 

       // Constructor 
       Event(int measure, int beat, int pos); 
     }; 

     // Sample Event Class 
     class Event::SampleEvent : public Event 
     { 
      public: 
      // variable in SampleEvent Class 
      int ID; 
      float Pan; 
      float Vol; 

      // Constructor 
      SampleEvent(int id, float pan, float vol, int measure, int beat, int pos); 
     }; 

     // Time Event Class 
     class Event::TimeEvent : public Event 
     { 
      public: 
      // variable in TimeEvent Class 
      double Value; 

      // Constructor 
      TimeEvent(double value, int measure, int beat, int pos); 
     }; 

     // Constructor of Event 
     Event::Event(int measure, int beat, int pos) 
     { 
      Measure   = measure; 
      Beat   = beat; 
      Position  = pos; 
     } 

     // Constructor of Sample Event 
     Event::SampleEvent::SampleEvent(int id, float pan, float vol, int measure, int beat, int pos) : Event::Event(measure, beat, pos) 
     { 
      ID      = id; 
      Pan      = pan; 
      Vol      = vol; 
      Measure   = measure; 
      Beat   = beat; 
      Position  = pos; 
     } 

     // Constructor of Time Event 
     Event::TimeEvent::TimeEvent(double value, int measure, int beat, int pos) : Event::Event(measure, beat, pos) 
     { 
      Value     = value; 
      Measure   = measure; 
      Beat   = beat; 
      Position  = pos; 
     } 
    }  
} 
#endif 

這給我2錯誤:

Error C2039: '{ctor}' : is not a member of 'Engine::Data::Event' (line 60) 
Error C2039: '{ctor}' : is not a member of 'Engine::Data::Event' (line 71) 

有人可以幫助我嗎? 謝謝

回答

0

int Position缺少分號。

而且,你在代碼的末尾缺少一個右括號:

 }  
    } // <--- MISSING 
} 

但是,隨着代碼的主要問題是,你需要明確初始化基類的構造函數,因爲默認的構造函數由於您定義了構造函數Event(int measure, int beat, int pos);,因此不會被隱式定義(也不能使用)。

Event::SampleEvent::SampleEvent(int id, float pan, float vol, 
           int measure, int beat, int pos) 
: Event(id, id, id) // This is just an example, pass the proper values here 
{ 
    //... 
} 

Event::TimeEvent::TimeEvent(double value, int measure, int beat, int pos) 
: Event(measure, measure, measure) // Pass the proper values to base ctor 

編輯

現在的錯誤是因爲你使用Event::Event(measure, beat, pos)代替Event。 (不過,我認爲Event::Event應該的工作,我認爲這是不接受的代碼在MSVC的錯誤。)

+0

感謝您的回覆,看後,我已經修改了代碼,但仍然得到同樣的錯誤 – SirusDoma 2013-03-26 03:14:44

+0

@ChronoCross:將'Event :: Event'改爲'Event'。 – 2013-03-26 03:17:42

+0

哇謝謝,解決我的問題(y)謝謝 – SirusDoma 2013-03-26 03:20:39