Question

I need to create a virtual function in one of my classes. Admittedly I have very little experience doing this, so I'm not sure what I'm doing wrong in the definition, but something is causing errors. My header file definition looks like this:

void myVirtualFunction() virtual;

And my class definition looks like this:

void myClass::myVirtualFunction()
{
  //code here...
}

However, when I run this, I get six errors, specifically three pairs of these two errors:

C2144: syntax error: 'int' should be preceded by ';'

and

C2208: 'int': no members defined by using this type

I'm really confused by this, since no where in the class (no other functions) use int as a parameter type. Does anyone know why this may happen. Thanks!

(PS: any colored formatting in the error message examples can be ignored, I wasn't sure how to undo SO's autoformatting)

Was it helpful?

Solution

In C++, virtual functions are declared by prepending the virtual keyword to the function's declaration in the class. Thus your declaration should be:

virtual void myVirtualFunction();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top