1

大家好,我是新来的,在嵌套类中声明构造函数时出现错误。

假设我有一个名为 Event 的类,有 2 个继承它的嵌套类 SampleEvent 和 TimeEvent,在 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)

有人能帮我吗?谢谢

4

1 回答 1

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 中不接受代码的错误。)

于 2013-03-26T03:11:53.370 回答