1

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;
      }

6 Answers 6

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I corrected the case error but still While Compiling i get some linker error . Is it due to memset code that i am using ? Can you explain that . I have also defined the Fun1() {cout << "LOL" ;} in class X but still compilation error.
There shouldn't be a link error if you've defined Foo::Fun1. What is the error exactly?
@Ritesh: You'll get that error if you haven't defined Foo::Fun1 (or perhaps if there's another virtual function that you haven't defined). Are you sure you've defined it?
I guess your answer :-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. Is very correct the only thing is me getting a compiler linking error instead of Run time error
@Ritesh: I just noticed in your first comment, you say you defined X::Fun1. You need to define Foo::Fun1 as well to fix the link error.
|
2

The Class should be class.

Comments

1

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

Comments

1

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.

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.