简体   繁体   English

编译此C ++代码时出错

[英]Error While Compiling this C++ code

I got the following error wwhile compiling this c++ code . 编译此c ++代码时出现以下错误。 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;
      }

Class应该是class

The keyword class begins with a lower-case c . 关键字class以小写c开头。 That will fix the errors you reported, but more errors remain. 这样可以修复您报告的错误,但是仍然存在更多错误。

You declare Foo::Fun1 , but don't define it. 您声明Foo::Fun1 ,但未定义它。

Finally you'll need to include <cstring> for the declaration of std::memset . 最后,您需要在std::memset的声明中包含<cstring> 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. 然后,您将具有未定义的运行时行为,因为使用memset覆盖非POD对象是无效的Foo具有虚函数,POD也没有。

Class X: public Foo应该是class X: public Foo ,这应该修复这两个错误。

C++ language is case sensitive and requires its keywords to be written in lowercase. C ++语言区分大小写,并且要求其关键字以小写形式编写。 class is valid C++ keyword but Class is not. class是有效的C ++关键字,但Class不是。 Rename Class to class when declaring class X. 在声明X class时,将Class重命名为class。

Class X以大写C命名。这就是问题所在。

您的错误实际上始于: Class X: public Foo // Error class does not name a type Class必须为class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM