Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

c++ - unknown type name 'class'

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...