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

  • Thread starter Thread starter Simon Bond
  • Start date Start date
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.
 
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
 
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
 
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
 
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.
 
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
 
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
 
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).
 
Back
Top