473,412 Members | 3,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

virtual function problem

Hi all,
I am new to c++ I have a problem compiling this program,

#include <iostream.h>
class iq
{
protected :
int k;
public :
iq(int k){
this->k=k;
}
virtual void printiq(){}
};
class im : public iq
{
int l;
public :
im(int k):iq(k) {
this->l=k;
}
void printiq(){
cout <<l;
}

};
int main(){
im l(10);
iq *l;
((l)iq*)->printiq();
}
Basically I want to acess the derived class member function
through pointer to the Base class. When i compile this program
it gives error like,
inherit.c: In function `int main ()':
inherit.c:26: conflicting types for `iq *l'
inherit.c:25: previous declaration as `im l'
inherit.c:27: parse error before `*'
How to solve this problem,

divya
Jul 23 '05 #1
2 1069
* foodic:

#include <iostream.h>
Don't. It's not a standard C++ header. Use
#include <iostream>
class iq
{
protected :
int k;
public :
iq(int k){
this->k=k;
This indicates you're using a bad tutorial or book. Technically okay
but bad style.

}
virtual void printiq(){}
};
class im : public iq
{
int l;
Don't use lowercase ell for a name (easy to confuse with 1).

Don't use uppercase oh for a name (easy to confuse with 0).

public :
im(int k):iq(k) {
this->l=k;
}
void printiq(){
cout <<l;
}

};
int main(){
im l(10);
iq *l;
((l)iq*)->printiq();
}

int main
{
im myIq( 10 );
iq& myMoreAbstractIq = im;

myMoreAbstractIq.printiq();
}
Steer well clear of raw pointers, and burn that book (or whatever).

Try e.g. "Accelerated C++".

Cheers, & hope this help,

- Alf

--
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?
Jul 23 '05 #2
foodic wrote:
Hi all,
I am new to c++ I have a problem compiling this program,

#include <iostream.h>
Note that <iostream.h> is an obsolete non-standard header that should be
avoided. Use <iostream> instead.
class iq
{
protected :
int k;
public :
iq(int k){
this->k=k;
}
Better use initialization instead of assignment. For built-in types like
int, it doesn't make much of a difference, but for class types, it does. So
write:

iq(int k)
: k(k)
{
}

And though it's possible to have the same name for the parameter as for the
member variable, the code is usually considered more readable if they have
different names.
virtual void printiq(){}
};
class im : public iq
{
int l;
public :
im(int k):iq(k) {
this->l=k;
}
void printiq(){
cout <<l;
}

};
int main(){
im l(10);
iq *l;
((l)iq*)->printiq();
}
Basically I want to acess the derived class member function
through pointer to the Base class. When i compile this program
it gives error like,
inherit.c: In function `int main ()':
inherit.c:26: conflicting types for `iq *l'
inherit.c:25: previous declaration as `im l'
The compiler says it quite clearly. You're trying to declare two variables
of different type (one of type im, the other of type pointer to iq) that
have the same name.
inherit.c:27: parse error before `*'
What is ((l)iq*) supposed to do? It looks kind of like a C style cast, but
with type and value swapped. That doesn't make sense, neither to me, nor to
the compiler. ;-)
And if it was supposed to be a cast, drop it. You don't need one here.
How to solve this problem,


Give your variables different names:

int main(){
im l(10);
iq *m = &l;
m->printiq();
}

Jul 23 '05 #3

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

Similar topics

18
by: nenad | last post by:
Wouldn't it be nice if we could do something like this: class Funky{ public: auto virtual void doStuff(){ // dostuff } };
3
by: Mark Turney | last post by:
Problem: I have a vector full of two different derived class objects (class B and class C) that are derived from the same base class A. I want to loop through vector and invoke a member function...
25
by: Stijn Oude Brunink | last post by:
Hello, I have the following trade off to make: A base class with 2 virtual functions would be realy helpfull for the problem I'm working on. Still though the functions that my program will use...
6
by: Dumitru Sipos | last post by:
Hello everybody! is there possible to have a function that is both static and virtual? Dumi.
11
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...
5
by: Luke Dalessandro | last post by:
Code: Thread -> U -> T public class Thread { protected: thread_t _tid; virtual void foo() = 0; public: // Static entry function for the internal thread
8
by: Floogle | last post by:
how do i create a virtual == operator. I've tried the following but it's incorrect... class Interface { ... public: virtual bool operator==(const Interface& rhs)const=0;
6
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than calling a virtual function in base class? Thanks
17
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;"...
3
by: Markus Dehmann | last post by:
I have an abstract base class which contains a function that I would like to template, but virtual template functions are illegal. I put a mini code example below, which doesn't do anything great,...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.