unknown type name 'class'

狂风中的少年 提交于 2019-12-25 07:49:11

问题


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.



来源:https://stackoverflow.com/questions/40311370/unknown-type-name-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!