No appropriate default constructor available

G

Guest

Hello,

I'm trying to implement "Null-Field" feature (like Stream::Null) in MC++, but I am not able to initialize static member properly due to compiler error C2512 (no appropriate default constructor available):

public __gc class ObjectCollection : public CollectionBase
{
public:
ObjectCollection() { }

// CollectionBase Methods...

private:
__gc class NullObjectCollection; // Forward declaration

public:
static ObjectCollection *Null = new NullObjectCollection(); // <- Error C2512
};

__gc class ObjectCollection::NullObjectCollection : public ObjectCollection
{
public:
NullObjectCollection() { }
};

I've also tried to replace

static ObjectCollection *Null = new NullObjectCollection();

with

static ObjectCollection *Null;

static ObjectCollection() // Static Constructor
{
Null = new NullObjectCollection(); // <- C2512 Too
}

but with the same error... Any ideas? Is is only possible in C#?
 
V

Vladimir Nesterovsky

Stanislav Simicek said:
Hello,

I'm trying to implement "Null-Field" feature (like Stream::Null) in
MC++, but I am not able to initialize static member properly due to compiler
error C2512 (no appropriate default constructor available):
public __gc class ObjectCollection : public CollectionBase
{
public:
ObjectCollection() { }

// CollectionBase Methods...

private:
__gc class NullObjectCollection; // Forward declaration

public:
static ObjectCollection *Null = new NullObjectCollection(); // <- Error C2512
};

__gc class ObjectCollection::NullObjectCollection : public ObjectCollection
{
public:
NullObjectCollection() { }
};

I've also tried to replace

static ObjectCollection *Null = new NullObjectCollection();

with

static ObjectCollection *Null;

static ObjectCollection() // Static Constructor
{
Null = new NullObjectCollection(); // <- C2512 Too
}

but with the same error... Any ideas? Is is only possible in C#?

It's not enough to have type forward declaration to create its instance.
Type definition should be available.
 
V

Vladimir Nesterovsky

Stanislav Simicek said:
The problem is where to define NullObjectCollection, because

public __gc class ObjectCollection ...
{
...
private:
__gc class NullObjectCollection : public ObjectCollection
{
// ...
}
}

does not compile - error C2504 "ObjectCollection: base class undefined".

Try the following:

public __gc class ObjectCollection: public CollectionBase
{
private:
__gc class NullObjectCollection;

static ObjectCollection *Null;

static ObjectCollection()
{
Null = CreateNullCollection();
}

static ObjectCollection * CreateNullCollection();
};

__gc class ObjectCollection::NullObjectCollection : public ObjectCollection
{
};

ObjectCollection * ObjectCollection::CreateNullCollection()
{
return new NullObjectCollection;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top