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 

virtual function problem

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
foodic
Guest





PostPosted: Sat Feb 26, 2005 12:00 pm    Post subject: virtual function problem Reply with quote



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 < }

};
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
Back to top
Alf P. Steinbach
Guest





PostPosted: Sat Feb 26, 2005 12:10 pm    Post subject: Re: virtual function problem Reply with quote



* foodic:
Quote:

#include <iostream.h

Don't. It's not a standard C++ header. Use


#include


Quote:
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.


Quote:
}
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).


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

};
int main(){
im l(10);


Quote:
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?

Back to top
Rolf Magnus
Guest





PostPosted: Sat Feb 26, 2005 12:21 pm    Post subject: Re: virtual function problem Reply with quote



foodic wrote:

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

#include <iostream.h

Note that avoided. Use <iostream> instead.

Quote:
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.

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

};
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.

Quote:
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. Wink
And if it was supposed to be a cast, drop it. You don't need one here.

Quote:
How to solve this problem,

Give your variables different names:

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


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) 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.