Virtual methods
Oct 18, 2009 at 5:13pm UTC
I have a class called Celula, which has a virtual method called simbolo
virtual char simbolo(void ) const ;
I want to declare the body of the method , outside the class. What is the syntax?
(here's an example of what i wanted to do)
1 2 3 4
//This header is wrong
char Celula::virtual simbolo(void ) const ; virtual {
return 'X' ;
}
Oct 18, 2009 at 5:22pm UTC
Sorry for the double post.
I tried to change the syntax according to an example i saw on internet (
http://codesyntax.netfirms.com/lang-cpp.htm )
1 2 3
virtual char Celula:: simbolo(void ) const {
return 'X' ;
}
but i receive error 74 v
irtual outside class declaration [Celula.o] Error 1
I was thinking i should include the code on the .hpp file for this to work
Oct 18, 2009 at 7:22pm UTC
So you basically want to define your function that is on the class Celula, called simbolo that returns a constant virtual char value.
class definition is on a header file , also the declaration of your method
virtual char simbolo(void ) const ;
definition of the function/method inside class Celula goes to .cpp
1 2 3 4 5
virtual char Celula::simbolo( ) const
{
return 'X' ;
}
I think you getting that error because you are declaring the fucntion/method again outside the class. You only want to define that method saying that is part of class Celula and that returns a constant virtual char.
May be try to declare and define the virtual on the class (.h)
I hope this helped...
Oct 18, 2009 at 8:44pm UTC
Inside the class:
virtual char simbolo() const ;
outside the class:
1 2 3 4
char Celula::simbolo() const // no virtual specifier, it isn't needed here
{
return 'X' ;
}
Oct 19, 2009 at 7:39pm UTC
Thanks!!
Topic archived. No new replies allowed.