Invictus
Invictus

Reputation: 4328

Error While Compiling this C++ code

I got the following error wwhile compiling this c++ code . What can be the reason behind this ?

     # include <iostream>
     # include <stdio.h>
     # include <conio.h>

     using namespace std;

     class Foo
     {
      int a;
      public :
      virtual void Fun1(); 

      Foo()
      {a=5;}
     };

     Class X: public Foo   // Error class does not name a type
     {
      Foo f;
      public:
      void Fun1() { }       
      X()
      {
       memset(&f,0x0,sizeof(f));
      }
     };

     int main()
     {
      X x; // Error 'X undeclared and expected ; before x, i guess because of first one
      getch();
      return 0;
      }

Upvotes: 1

Views: 110

Answers (6)

Mike Seymour
Mike Seymour

Reputation: 254431

The keyword class begins with a lower-case c. That will fix the errors you reported, but more errors remain.

You declare Foo::Fun1, but don't define it.

Finally you'll need to include <cstring> for the declaration of std::memset. It's possible that another header is including it indirectly, but you can't rely on that.

You'll then have undefined runtime behaviour, since it's not valid to use memset to overwrite non-POD objects - Foo has a virtual function, and so is not POD.

Upvotes: 2

macduff
macduff

Reputation: 4685

Your error really starts with the: Class X: public Foo // Error class does not name a type Class must be class.

Upvotes: 0

Bojan Komazec
Bojan Komazec

Reputation: 9526

C++ language is case sensitive and requires its keywords to be written in lowercase. class is valid C++ keyword but Class is not. Rename Class to class when declaring class X.

Upvotes: 1

simont
simont

Reputation: 72527

Class X: public Foo should be class X: public Foo, which should fix both errors.

Upvotes: 1

Boris Strandjev
Boris Strandjev

Reputation: 46943

Class X is named with capital C. This is the problem.

Upvotes: 0

sholsapp
sholsapp

Reputation: 16070

The Class should be class.

Upvotes: 2

Related Questions