unknown type name 'class'

2019-09-13 23:58发布

问题:

I'm creating a small library for several geometric shapes. Doing so, I'm writing down prototypes into a shapes.h file and the methods into a shapes.cpp file. This is the 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);
};

But when saving the file in Atom software, I get these two errors for the line class Shape{

unknown type name 'class'

expected ';' after top level declarator

I read here that could be because I'm compiling in C rather than C++. I sincerely have no idea about how to avoid this (still a beginner).

I also tried to change the file name from .h to .hpp and seems working. Unfortunately, I must have a .h header file.

Any feedback is really appreciated. Thanks everyone.

回答1:

Actually, seems that Atom detects a .h header file as a C-language file automatically. Several ways to resolve this are explained here. I tried with a manual switch from C to C++ using ctrl+shift+L and now I don't have any error left. I may still have a red point next to the word class and such an error is showed:

expected ';' after top level declarator

but the code runs normally though.