Laawanya
Laawanya

Reputation: 249

"cout" does not name a type error

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

Answers (1)

Daniel Frey
Daniel Frey

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

Related Questions