Puzzle about virtual function inheritance in C++ -


i'm noob in c++, , have following header file (header.h)

#ifndef header_h #define header_h  #include <iostream>  using namespace std;  class predicate { public:     virtual void print() = 0; };  class unaryintegerpredicate : public predicate  { public:     virtual void print(); };  class biaryintegerpredicate : public predicate { public:     virtual void print(); };  #endif 

and in separate .cpp file (source.cpp), tried implement print method, got "expected expression" error.

#include "stdafx.h" #include "header.h"  #include <iostream>  using namespace std;  unaryintegerpredicate::unaryintegerpredicate() : predicate() {     virtual void print()     {         cout << "int : boolean" << endl;     } } 

what wrong here, thanks!

i see coming java background. need is

void unaryintegerpredicate::print() {     cout << "int : boolean" << endl; } 

you don't need stuff surrounding that. since have declared in header unaryintegerpredicate derives predicate, don't mention again in implementation file. show print method writing print method of unaryintegerpredicate class prefixing name of method name of class have shown above.

you don't need virtual keyword in cpp file because specified in header file.


Comments

Popular posts from this blog

c++ - OpenMP unpredictable overhead -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

javascript - Wordpress slider, not displayed 100% width -