未知类型名称 'class'

unknown type name 'class'

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

#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

我读到 here 这可能是因为我使用 C 而不是 C++ 进行编译。我真的不知道如何避免这种情况(还是初学者)。

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

非常感谢任何反馈。 谢谢大家。

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

expected ';' after top level declarator

但是代码运行正常。