unknown type name 'class'

2019-09-14 00:28发布

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条回答
该账号已被封号
2楼-- · 2019-09-14 00:33

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.

查看更多
登录 后发表回答