Reputation: 249
I am trying pure virtual function. So I have written code accordingly. But I ma not getting any problem because of that. I am getting "cout does not name a type" error in my code, even I have included proper header file and namespace as well. Please give your suggestions for this.
#include<iostream>
using namespace std;
struct S {
virtual void foo();
};
void S::foo() {
// body for the pure virtual function `S::foo`
cout<<"I am S::foo()" <<endl;
}
struct D : S {
cout <<"I am inside D::foo()" <<endl;
};
int main() {
S *ptr;
D d;
ptr=&d;
//ptr->foo();
d.S::foo(); // another static call to `S::foo`
cout <<"Inside main().." <<endl;
return 0;
}
Upvotes: 0
Views: 3338
Reputation: 56863
You tried to define a struct with direct code, but it looks like you wanted a method around the code:
struct D : S {
cout <<"I am inside D::foo()" <<endl;
};
should probably be
struct D : S {
virtual void foo() {
cout <<"I am inside D::foo()" <<endl;
}
};
Upvotes: 4