Pergunta

I'm trying to write a mixed assembly. Here's a snippet:

public ref class OpusStream
: public Stream
{
protected:
    OpusStream(void);

public:
    ~OpusStream(void);
    !OpusStream(void);
    int TestEncode(void);

    /* System::IO::Stream */
    virtual property bool CanRead { bool get(void) override; }
    virtual property bool CanSeek { bool get(void) override; }
    virtual property bool CanWrite { bool get(void) override; }
    virtual property __int64 Length { __int64 get(void) override; }
    virtual property __int64 Position {
        __int64 get(void) override;
        void set(__int64) override;
    }
    virtual void Flush(void) override;
    int Read(Byte[], int, int) override;
    long Seek(long, System::IO::SeekOrigin) override;
    void SetLength(long) override;
    void Write(Byte[], int, int) override;
};

And this is the compiler error:

Error 11 Cannot create an instance of the abstract class or interface 'OpusNet.OpusStream' C:\Users\benc\Documents\opus-1.1-alpha\OpusTests\MainWindow.xaml.cs 30 21 OpusTests

It's been a while since I've done C++/CLI. I'm really not sure what I've missed here.

Foi útil?

Solução

The Read and Write overrides were using the wrong type for (as per C#) byte[].

For the overrides to actually override the methods on stream you need to use the correct type, which, in a roundabout way I discovered by tinkering with override omitted until the compiler told me I was hiding a method on System.IO.Stream:

So;

typedef array<Byte>^ ByteArray;

Then;

virtual __int32 Read(ByteArray, __int32, __int32) override;

Outras dicas

The problem is with these property definitions:

virtual property bool CanRead { bool get(void) sealed new; }
virtual property bool CanSeek { bool get(void) sealed new; }

By declaring this as new, you're not overriding the abstract member on the base class! Instead, you're declaring a new member in your class, with the same name as the abstract member of the base class, but doesn't override the abstract member.

Switch this to actually override the member, and you'll be fine.

virtual property bool CanRead { bool get(void) sealed override; }
virtual property bool CanSeek { bool get(void) sealed override; }
                                                      ^^^^^^^^
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top