error C2259: cannot instantiate abstract class - then how do you do it?!

S

Simon Bond

In C# we can go:

<foo.cs>
// Create a serializable class
[Serializable]
public class Foo : ISerializable
{
public void GetObjectData(SerializationInfo info,
StreamingContext context)
{};
}

// In our application
void MakeFoo()
{
Foo f = new Foo();
}


But in C++, I'm attempting:

<Foo.h>
[Serializable]
public __gc class Foo : public ISerializable
{
public:
Foo(){};
void GetObjectData(SerializationInfo* info,
StreamingContext* context);
};

<Foo.cpp>
// In the application
void MakeFoo()
{
Foo* foo = new Foo; //error C2259: cannot instantiate abstract class
}

I've been trying to get this to work for a number of hours now with no
success - any hints?

Cheers

- Si.
 
S

Steve McLellan

First off, it needs to be:
Foo *foo = new Foo();

If you click on the 'Output' tab (probably next to the task list) while
you've got the error highlighted, it will tell you which functions it thinks
you haven't implemented.

Steve
 
D

Doug Harrison [MVP]

Simon Bond said:
In C# we can go:

<foo.cs>
// Create a serializable class
[Serializable]
public class Foo : ISerializable
{
public void GetObjectData(SerializationInfo info,
StreamingContext context)
{};
}

// In our application
void MakeFoo()
{
Foo f = new Foo();
}


But in C++, I'm attempting:

<Foo.h>
[Serializable]
public __gc class Foo : public ISerializable
{
public:
Foo(){};
void GetObjectData(SerializationInfo* info,
StreamingContext* context);
};

<Foo.cpp>
// In the application
void MakeFoo()
{
Foo* foo = new Foo; //error C2259: cannot instantiate abstract class
}

I've been trying to get this to work for a number of hours now with no
success - any hints?

Hint: Look carefully at the signature of your GetObjectData and compare it
to the docs:

ISerializable.GetObjectData Method
http://msdn.microsoft.com/library/d...lizableClassGetObjectDataTopic.asp?frame=true
 
T

tom_usenet

But in C++, I'm attempting:

<Foo.h>
[Serializable]
public __gc class Foo : public ISerializable
{
public:
Foo(){};
void GetObjectData(SerializationInfo* info,
StreamingContext* context);
};

<Foo.cpp>
// In the application
void MakeFoo()
{
Foo* foo = new Foo; //error C2259: cannot instantiate abstract class
}

I've been trying to get this to work for a number of hours now with no
success - any hints?

Read the documentation for ISerializable.

Hint: (void GetObjectData(
SerializationInfo* info,
StreamingContext context
); - you're hiding that function, not overriding it)

Tom
 
D

Doug Harrison [MVP]

Steve McLellan said:
First off, it needs to be:
Foo *foo = new Foo();

While required in C#, those parens are optional in C++. For a managed type,
I'm unaware they make any difference. For an unmanaged type that has no
ctors, the parens cause members of built-in types to be zero-initialized.
Without the parens, those members would be uninitialized.
 
S

Simon Bond

Ah-ha!

Thanks, Doug. Just a * in there where it shouldn't have been.

Your help is greatly appreciated.

- Si


Doug Harrison said:
Simon Bond said:
In C# we can go:

<foo.cs>
// Create a serializable class
[Serializable]
public class Foo : ISerializable
{
public void GetObjectData(SerializationInfo info,
StreamingContext context)
{};
}

// In our application
void MakeFoo()
{
Foo f = new Foo();
}


But in C++, I'm attempting:

<Foo.h>
[Serializable]
public __gc class Foo : public ISerializable
{
public:
Foo(){};
void GetObjectData(SerializationInfo* info,
StreamingContext* context);
};

<Foo.cpp>
// In the application
void MakeFoo()
{
Foo* foo = new Foo; //error C2259: cannot instantiate abstract class
}

I've been trying to get this to work for a number of hours now with no
success - any hints?

Hint: Look carefully at the signature of your GetObjectData and compare it
to the docs:

ISerializable.GetObjectData Method
http://msdn.microsoft.com/library/d...lizableClassGetObjectDataTopic.asp?frame=true
 
S

Simon Bond

Thanks, Tom.

tom_usenet said:
But in C++, I'm attempting:

<Foo.h>
[Serializable]
public __gc class Foo : public ISerializable
{
public:
Foo(){};
void GetObjectData(SerializationInfo* info,
StreamingContext* context);
};

<Foo.cpp>
// In the application
void MakeFoo()
{
Foo* foo = new Foo; //error C2259: cannot instantiate abstract class
}

I've been trying to get this to work for a number of hours now with no
success - any hints?

Read the documentation for ISerializable.

Hint: (void GetObjectData(
SerializationInfo* info,
StreamingContext context
); - you're hiding that function, not overriding it)

Tom
 
S

Steve McLellan

Doug Harrison said:
While required in C#, those parens are optional in C++. For a managed type,
I'm unaware they make any difference. For an unmanaged type that has no
ctors, the parens cause members of built-in types to be zero-initialized.
Without the parens, those members would be uninitialized.

I stand corrected :) I've always used them out of habit, and possibly
because Java requires them (unless it doesn't, in which case it's only
habit).
 

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