473,505 Members | 15,212 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

virtual abstract functions

Hi

if a have the following classes

class A
{
public:
A();
virtual ~A();
virtual string someFunction() const = 0;
}

class B:public A
{}

int main(void)
{
A* aa = new B();
}

I get this compiler error

main.cpp: In function `int main(int, char**)':
main.cpp:10: error: cannot allocate an object of type `A'
main.cpp:10: error: because the following virtual functions are
abstract:
B.h:30: error: virtual std::string B::someFunction() const
does anyone know whats this abou, because if i remove the 'const' in
the Base class it works but i wont the Base class to be abstract !
Thanks in advance

Oct 11 '05 #1
12 21203
* placid:

if a have the following classes

class A
{
public:
A();
virtual ~A();
virtual string someFunction() const = 0;
}

class B:public A
{}

int main(void)
{
A* aa = new B();
}

I get this compiler error
No you don't.

If you try to compile the above you get some other error messages.

Here's an example of what you actually get, using g++ 3.4.2:

koko.cpp:6: error: `string' does not name a type
koko.cpp:6: error: extra semicolon
koko.cpp:13: error: new types may not be defined in a return type
koko.cpp:13: error: two or more data types in declaration of `main'
koko.cpp:13: error: extraneous `int' ignored
koko.cpp:13: error: `main' must return `int'
koko.cpp:13: error: return type for `main' changed to `int'
koko.cpp: In function `int main(...)':
koko.cpp:14: warning: unused variable 'aa'

Here's another example, when '#include <string>' and 'using namespace std;'
are added on the top:

koko.cpp:16: error: new types may not be defined in a return type
koko.cpp:16: error: two or more data types in declaration of `main'
koko.cpp:16: error: extraneous `int' ignored
koko.cpp:16: error: `main' must return `int'
koko.cpp:16: error: return type for `main' changed to `int'
koko.cpp: In function `int main(...)':
koko.cpp:17: error: cannot allocate an object of type `B'
koko.cpp:17: error: because the following virtual functions are abstract:
koko.cpp:9: error: virtual std::string A::someFunction() const
koko.cpp:17: warning: unused variable 'aa'

Now for the error message you _claim_ for the above code, but which is
actually for code that you haven't shown:

main.cpp: In function `int main(int, char**)':
main.cpp:10: error: cannot allocate an object of type `A'
main.cpp:10: error: because the following virtual functions are
abstract:
B.h:30: error: virtual std::string B::someFunction() const
Study the error message.

What does it say?

That's one thing that's wrong in your actual code.

does anyone know whats this abou, because if i remove the 'const' in
the Base class it works
No it does not.

but i wont the Base class to be abstract !


Why?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 11 '05 #2

placid wrote:
Hi

if a have the following classes

class A
{
public:
A();
virtual ~A();
virtual string someFunction() const = 0;
}

class B:public A
{}

int main(void)
{
A* aa = new B();
}

I get this compiler error

main.cpp: In function `int main(int, char**)':
main.cpp:10: error: cannot allocate an object of type `A'
main.cpp:10: error: because the following virtual functions are
abstract:
B.h:30: error: virtual std::string B::someFunction() const
does anyone know whats this abou, because if i remove the 'const' in
the Base class it works but i wont the Base class to be abstract !
Thanks in advance


Class B must implement someFunction for the class to be instantiable:

class B : public A
{
public:
virtual std::string someFunction() const
{
return "";
}
};

Note also the proper syntax to allocate a B object:

int main()
{
A* aa = new B;
...
}

Greg

Oct 11 '05 #3
"Greg" <gr****@pacbell.net> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com
placid wrote:

int main(void)
{
A* aa = new B();
}


Note also the proper syntax to allocate a B object:

int main()
{
A* aa = new B;
...
}

A* aa = new B();

is also correct. See section 5.3.4/15 of the standard. The use of () means
that the object is value initialized.

--
John Carson

Oct 11 '05 #4
John Carson wrote:
"Greg" <gr****@pacbell.net> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com
placid wrote:

int main(void)
{
A* aa = new B();
}


Note also the proper syntax to allocate a B object:

int main()
{
A* aa = new B;
...
}

A* aa = new B();

is also correct. See section 5.3.4/15 of the standard. The use of () means
that the object is value initialized.


Yes, the parentheses are legal in this new expression. But my point was
that the parentheses are completely superfluous when allocating an
instance of a class type. The object is default-initialized without the
parentheses, the object is default-initialized with the parentheses, so
their presence in the statement has no effect on its meaning.

The economy of expression principle holds that only those operators
needed to complete an expression should appear in it. In other words,
it is not a good idea to pepper one's code with aesthetically pleasing
operators that otherwise contribute nothing to its meaning. Doing so
makes the program harder to understand, since someone examining the
code must first decide whether an operator in a statement is performing
some necessary operation, or whether it can be excluded from analysis.

More importantly, superfluous operators in one context may suddenly
become meaningful in slightly different contexts and do so in
surprising ways. Even in a new expression, the Standard warns that the
presence of parentheses can have surprising effects (§5.3.4/3) - which
is another argument not to use parentheses indiscriminately in a C++
program, but to use them only on an as-needed basis.

Greg

Oct 11 '05 #5

"placid" <Bu****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hi

if a have the following classes

class A
{
public:
A();
virtual ~A();
virtual string someFunction() const = 0;
}

class B:public A
{}

int main(void)
{
A* aa = new B();
}

I get this compiler error

main.cpp: In function `int main(int, char**)':
main.cpp:10: error: cannot allocate an object of type `A'
main.cpp:10: error: because the following virtual functions are
abstract:
B.h:30: error: virtual std::string B::someFunction() const
does anyone know whats this abou, because if i remove the 'const' in
the Base class it works but i wont the Base class to be abstract !
Thanks in advance


virtual string someFunction() const = 0;

This is called "pure virtual". Pure virtual means there is no definition of
the method in the base class, and any class that is derived from the base
class MUST define this method. Your class B did not define this method.
Which is what the compile error is yelling about.

Either:
1. Don't make the method pure virtual so you dont' have to define it in the
derived class (remove the = 0 ) or
2. Define the method in your derived class B.
Oct 11 '05 #6

Greg wrote:
A* aa = new B;
...

A* aa = new B();
is also correct.

Yes, the parentheses are legal in this new expression. But... Doing so
makes the program harder to understand, since someone examining the
code must first decide whether an operator in a statement is performing
some necessary operation, or whether it can be excluded from analysis.

More importantly, superfluous operators in one context may suddenly
become meaningful in slightly different contexts and do so in
surprising ways. Even in a new expression, the Standard warns that the
presence of parentheses can have surprising effects (§5.3.4/3) - which
is another argument not to use parentheses indiscriminately in a C++
program, but to use them only on an as-needed basis.


are you saying that B::B() will not be called if I do new B; ?

Oct 11 '05 #7
* Greg:
John Carson wrote:
"Greg" <gr****@pacbell.net> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com
placid wrote:
>
> int main(void)
> {
> A* aa =3D new B();
> }
>

Note also the proper syntax to allocate a B object:

int main()
{
A* aa =3D new B;
...
}

A* aa =3D new B();

is also correct. See section 5.3.4/15 of the standard. The use of () means
that the object is value initialized.


Yes, the parentheses are legal in this new expression. But my point was
that the parentheses are completely superfluous when allocating an
instance of a class type. The object is default-initialized without the
parentheses, the object is default-initialized with the parentheses, so
their presence in the statement has no effect on its meaning.


In this particular case, yes. For a POD, no. The problem is that popular
compilers such as Microsoft's do not adhere to the standard in this respect.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 11 '05 #8

Jim Langston wrote:
"placid" <Bu****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hi

if a have the following classes

class A
{
public:
A();
virtual ~A();
virtual string someFunction() const = 0;
}

class B:public A
{}

int main(void)
{
A* aa = new B();
}

I get this compiler error

main.cpp: In function `int main(int, char**)':
main.cpp:10: error: cannot allocate an object of type `A'
main.cpp:10: error: because the following virtual functions are
abstract:
B.h:30: error: virtual std::string B::someFunction() const
does anyone know whats this abou, because if i remove the 'const' in
the Base class it works but i wont the Base class to be abstract !
Thanks in advance

virtual string someFunction() const = 0;

This is called "pure virtual". Pure virtual means there is no definition of
the method in the base class, and any class that is derived from the base
class MUST define this method. Your class B did not define this method.
Which is what the compile error is yelling about.


Neither point is completely accurate. There may or may not be a
definition of the pure virtual function in the base class. If the
program ever calls the pure virtual method in the base class
explicitly, then the pure virtual function must be defined.
Furthermore, a derived class is not obligated to define a pure virtual
method it inherits. A derived class can declare also declare the method
pure virtual and thereby become an abstract class like its base.
Either:
1. Don't make the method pure virtual so you dont' have to define it in the
derived class (remove the = 0 ) or
2. Define the method in your derived class B.


There is also a third option just mentioned: B declares the method pure
virtual itself.

Greg

Oct 11 '05 #9

Jim Langston wrote:
"placid" <Bu****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hi

if a have the following classes

class A
{
public:
A();
virtual ~A();
virtual string someFunction() const = 0;
}

class B:public A
{}

int main(void)
{
A* aa = new B();
}

I get this compiler error

main.cpp: In function `int main(int, char**)':
main.cpp:10: error: cannot allocate an object of type `A'
main.cpp:10: error: because the following virtual functions are
abstract:
B.h:30: error: virtual std::string B::someFunction() const
does anyone know whats this abou, because if i remove the 'const' in
the Base class it works but i wont the Base class to be abstract !
Thanks in advance

virtual string someFunction() const = 0;

This is called "pure virtual". Pure virtual means there is no definition of
the method in the base class, and any class that is derived from the base
class MUST define this method. Your class B did not define this method.
Which is what the compile error is yelling about.


yes i have already defined the pure virtual function in the derived
class but the compiler still complains about not being able to make a
A* aa = new B();

Either:
1. Don't make the method pure virtual so you dont' have to define it in the
derived class (remove the = 0 ) or
2. Define the method in your derived class B.


Oct 12 '05 #10

placid wrote:
Jim Langston wrote:
"placid" <Bu****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hi

if a have the following classes

class A
{
public:
A();
virtual ~A();
virtual string someFunction() const = 0;
}

class B:public A
{}

int main(void)
{
A* aa = new B();
}

I get this compiler error

main.cpp: In function `int main(int, char**)':
main.cpp:10: error: cannot allocate an object of type `A'
main.cpp:10: error: because the following virtual functions are
abstract:
B.h:30: error: virtual std::string B::someFunction() const
does anyone know whats this abou, because if i remove the 'const' in
the Base class it works but i wont the Base class to be abstract !
Thanks in advance


virtual string someFunction() const = 0;

This is called "pure virtual". Pure virtual means there is no definition of
the method in the base class, and any class that is derived from the base
class MUST define this method. Your class B did not define this method.
Which is what the compile error is yelling about.


yes i have already defined the pure virtual function in the derived
class but the compiler still complains about not being able to make a
A* aa = new B();


Then show us the code :).

It's likely that you have actually overloaded and not overriden the
pure virtual function. Did you remember the const in the method's
declaration, for instance?

Greg

Oct 12 '05 #11

Greg wrote:
placid wrote:
Jim Langston wrote:
"placid" <Bu****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
> Hi
>
> if a have the following classes
>
> class A
> {
> public:
> A();
> virtual ~A();
> virtual string someFunction() const = 0;
> }
>
> class B:public A
> {}
>
> int main(void)
> {
> A* aa = new B();
> }
>
> I get this compiler error
>
> main.cpp: In function `int main(int, char**)':
> main.cpp:10: error: cannot allocate an object of type `A'
> main.cpp:10: error: because the following virtual functions are
> abstract:
> B.h:30: error: virtual std::string B::someFunction() const
>
>
> does anyone know whats this abou, because if i remove the 'const' in
> the Base class it works but i wont the Base class to be abstract !
>
>
> Thanks in advance
>

virtual string someFunction() const = 0;

This is called "pure virtual". Pure virtual means there is no definition of
the method in the base class, and any class that is derived from the base
class MUST define this method. Your class B did not define this method.
Which is what the compile error is yelling about.
yes i have already defined the pure virtual function in the derived
class but the compiler still complains about not being able to make a
A* aa = new B();


Then show us the code :).

It's likely that you have actually overloaded and not overriden the
pure virtual function. Did you remember the const in the method's
declaration, for instance?


dude you have just fixed my problem. Thanks a lot man..

(i forgot the const in the member function definition.I thought it
didnt matter if you had the keyword const in the derived class method
definition)
Greg


Oct 12 '05 #12
> Did you remember the const in the method's declaration, for instance?

btw, what does it mean?

Oct 12 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
5002
by: richard.forrest1 | last post by:
I have a problem with an abstract interface class whose implementation classes need to return different iterator types (but with the same value_types etc). Classes A and B both conform to the...
62
3309
by: christopher diggins | last post by:
Since nobody responded to my earlier post , I thought I would try to explain what I am doing a bit differently. When multiply inheriting pure virtual (abstract) base classes, a class obviously...
11
4329
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
37
4121
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
6
3126
by: Alden Pierre | last post by:
Hello, http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7 As per the link above it's wise to have a virtual deconstructor when creating an abstract class. Here is when I'm...
7
3077
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
4
1705
by: Arne Schmitz | last post by:
If i have an abstract base class, that only contains pure virtual methods (and maybe some non-virtual methods), is a vtable still being generated, for the first derived class that implements those...
14
4166
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
7
1760
by: v4vijayakumar | last post by:
Is it possible to implement member object's virtual functions, in the containing class? If not, is it possible to simulate this behavior? ex: class test { protected: virtual void fun() = 0;...
17
3510
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
0
7216
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7303
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5613
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5028
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4699
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.