未知类型名称 'class'



我正在为几个几何形状创建一个小库。这样做,我将原型写入shapes.h文件,并将方法写入shapes.cpp文件。这是标题:

#ifndef __shapeslib
#define __shapeslib
class Shape{
protected:
  struct dimensions{
    double heigth;
    double width;
  };
  double radius;                        // for circle class to be inherited
public:
  Shape(double heigth, double width);   // Constructor
  Shape(const Shape & shape);           // copy constructor for class
  ~Shape();                             // Destructor
  virtual double area(double heigth, double width);
  virtual double perimeter(double heigth, double width);
  void height();
  void width();
  double rotate(double heigth, double width);
};

但是当在Atom软件中保存文件时,我得到class Shape{

行这两个错误

unknown type name 'class'

expected ';' after top level declarator

我在这里读到这可能是因为我在用C而不是c++编译。我真的不知道如何避免这种情况(仍然是初学者)。

我还试图将文件名从.h更改为.hpp,并且似乎有效。不幸的是,我必须有一个.h头文件。

任何反馈都非常感谢。谢谢每一个人。

实际上,Atom似乎自动将.h头文件检测为c语言文件。这里解释了解决这个问题的几种方法。我尝试使用ctrl+shift+L从C手动切换到c++,现在我没有任何错误。我可能仍然有一个红点旁边的字class和这样的错误显示:

expected ';' after top level declarator

但是代码运行正常

最新更新