未知类型名称' class'

时间:2016-10-28 18:44:16

标签: c++ class header-files atom-editor

我正在为几种几何形状创建一个小型库。这样做,我将原型写入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

我读了here,这可能是因为我在C而不是C ++中进行编译。我真的不知道如何避免这种情况(仍然是初学者)。

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

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

1 个答案:

答案 0 :(得分:0)

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

expected ';' after top level declarator

但代码运行正常。

相关问题