C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Solution to "cannot instantiate abstract class due to follow

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Jonathan Browns
Guest





PostPosted: Thu Sep 25, 2003 1:34 pm    Post subject: Solution to "cannot instantiate abstract class due to follow Reply with quote



Dear comp.lang.c++.moderated,

Using MS Visual C++, I am trying to create two abstract classes, one
is a member of another, after that I am trying to use these classes as
base of two concrete clases.

Trying to use it in the following brief sample code give me a lot of
cannot "instantiate abstract class due to following members" errors.

---------- o ----------
class A {
public:
virtual void setNumber(int) const = 0;
virtual int getNumber() const = 0;
};

class B {
public:
virtual A myNumber() const = 0;
};

class C: public virtual A {
public:
virtual void setNumber(int a);
virtual int getNumber();
private:
int n;
};

void C::setNumber(int aNumber)
{
n = aNumber;
}

int C::getNumber()
{
return n;
}

class D: public virtual B {
public:
virtual C myNumber();
};

C D::myNumber()
{
C aNumber;

aNumber.setNumber(5);

return aNumber;
}

int main(int argc, char* argv[])
{
C number;
D d;

printf("%dn", d.myNumber().getNumber());


return 0;
}
---------- o ----------

It would very helpful if somebody can point me how to solve this maze.
My few experience is on dynamic typing languages.

The errors I receive were:

---------- o ----------
testcpp.cpp(15) : error C2259: 'A' : cannot instantiate abstract class
due to following members:
testcpp.cpp(7) : see declaration of 'A'
testcpp.cpp(15) : warning C4259: 'void __thiscall A::setNumber(int)
const' : pure virtual function was not defined
testcpp.cpp(9) : see declaration of 'setNumber'
testcpp.cpp(15) : warning C4259: 'int __thiscall A::getNumber(void)
const' : pure virtual function was not defined
testcpp.cpp(10) : see declaration of 'getNumber'
testcpp.cpp(3Cool : error C2259: 'C' : cannot instantiate abstract class
due to following members:
testcpp.cpp(1Cool : see declaration of 'C'
testcpp.cpp(3Cool : warning C4259: 'void __thiscall A::setNumber(int)
const' : pure virtual function was not defined
testcpp.cpp(9) : see declaration of 'setNumber'
testcpp.cpp(3Cool : warning C4259: 'int __thiscall A::getNumber(void)
const' : pure virtual function was not defined
testcpp.cpp(10) : see declaration of 'getNumber'
testcpp.cpp(42) : error C2259: 'C' : cannot instantiate abstract class
due to following members:
testcpp.cpp(1Cool : see declaration of 'C'
testcpp.cpp(42) : warning C4259: 'void __thiscall A::setNumber(int)
const' : pure virtual function was not defined
testcpp.cpp(9) : see declaration of 'setNumber'
testcpp.cpp(42) : warning C4259: 'int __thiscall A::getNumber(void)
const' : pure virtual function was not defined
testcpp.cpp(10) : see declaration of 'getNumber'
testcpp.cpp(43) : error C2259: 'C' : cannot instantiate abstract class
due to following members:
testcpp.cpp(1Cool : see declaration of 'C'
testcpp.cpp(43) : warning C4259: 'void __thiscall A::setNumber(int)
const' : pure virtual function was not defined
testcpp.cpp(9) : see declaration of 'setNumber'
testcpp.cpp(43) : warning C4259: 'int __thiscall A::getNumber(void)
const' : pure virtual function was not defined
testcpp.cpp(10) : see declaration of 'getNumber'
testcpp.cpp(43) : error C2259: 'C' : cannot instantiate abstract class
due to following members:
testcpp.cpp(1Cool : see declaration of 'C'
testcpp.cpp(43) : warning C4259: 'void __thiscall A::setNumber(int)
const' : pure virtual function was not defined
testcpp.cpp(9) : see declaration of 'setNumber'
testcpp.cpp(43) : warning C4259: 'int __thiscall A::getNumber(void)
const' : pure virtual function was not defined
testcpp.cpp(10) : see declaration of 'getNumber'
testcpp.cpp(52) : error C2259: 'C' : cannot instantiate abstract class
due to following members:
testcpp.cpp(1Cool : see declaration of 'C'
testcpp.cpp(52) : warning C4259: 'void __thiscall A::setNumber(int)
const' : pure virtual function was not defined
testcpp.cpp(9) : see declaration of 'setNumber'
testcpp.cpp(52) : warning C4259: 'int __thiscall A::getNumber(void)
const' : pure virtual function was not defined
testcpp.cpp(10) : see declaration of 'getNumber'
testcpp.cpp(53) : error C2259: 'D' : cannot instantiate abstract class
due to following members:
testcpp.cpp(36) : see declaration of 'D'
testcpp.cpp(53) : warning C4259: 'class A __thiscall B::myNumber(void)
const' : pure virtual function was not defined
testcpp.cpp(15) : see declaration of 'myNumber'
testcpp.cpp(53) : error C2259: 'D' : cannot instantiate abstract class
due to following members:
testcpp.cpp(36) : see declaration of 'D'
testcpp.cpp(53) : warning C4259: 'class A __thiscall B::myNumber(void)
const' : pure virtual function was not defined
testcpp.cpp(15) : see declaration of 'myNumber'
testcpp.cpp(55) : error C2264: 'myNumber' : error in function
definition or declaration; function not called
testcpp.cpp(55) : error C2228: left of '.getNumber' must have
class/struct/union type
---------- o ----------



Thank You All
Jonathan Browns

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ulrich Eckhardt
Guest





PostPosted: Thu Sep 25, 2003 9:21 pm    Post subject: Re: Solution to "cannot instantiate abstract class due to fo Reply with quote



Jonathan Browns wrote:
Quote:
class A {
public:
virtual void setNumber(int) const = 0;
virtual int getNumber() const = 0;
};

A is pure virtual and cannot be instantiated. Furthermore, in order to avoid
slicing, you should apply some control to how it can be copied...

Quote:
class B {
public:
virtual A myNumber() const = 0;
};

Returns an A 'by value', but A can't be instantiated as is. Redesign to use
either pointers, references or a self-defined handle-type
(boost::shared_ptr also comes to mind).

cheers

Uli


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ron
Guest





PostPosted: Thu Sep 25, 2003 11:42 pm    Post subject: Re: Solution to "cannot instantiate abstract class due to fo Reply with quote



Quote:
Using MS Visual C++, I am trying to create two abstract classes, one
is a member of another, after that I am trying to use these classes as
base of two concrete clases.

Trying to use it in the following brief sample code give me a lot of
cannot "instantiate abstract class due to following members" errors.

---------- o ----------
class A {
public:
virtual void setNumber(int) const = 0;
virtual int getNumber() const = 0;
};

class B {
public:
virtual A myNumber() const = 0;
};

class C: public virtual A {
public:
virtual void setNumber(int a);
^

Warning I'mNotGeorge: supposedly overriding function lacks "const" qualifier

Quote:
virtual int getNumber();

Ibid.

Quote:
private:
int n;
};

class D: public virtual B {
public:
virtual C myNumber();

Ibid.

Quote:
};

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.