2

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.

Community
  • 1
  • 1
Mattia Paterna
  • 1,268
  • 3
  • 15
  • 31
  • 2
    There is no `class` in C. – 001 Oct 28 '16 at 18:46
  • Yes, agree. But this code is for C++ (or should be). – Mattia Paterna Oct 28 '16 at 18:47
  • If you are compiling with gcc try to compile with g++. – OutOfBound Oct 28 '16 at 18:48
  • 1
    Perhaps Atom is incorrectly auto-detecting the language using the file extension, and you need to figure out how to get it to assume .h files are C++ instead of C? [This](https://github.com/atom/atom/issues/4156) seems like it'd help with that when you get around to it. – jaggedSpire Oct 28 '16 at 18:49
  • I will. I'd just like to understand why I get such errors. I have another code like that (`vec.cpp` and `vec.h` and I don't get any errors for them). – Mattia Paterna Oct 28 '16 at 18:50
  • @MattiaPaterna so your question is more "why Atom think this file is invalid C, when it detects this other .h file as valid C++?" What does your vec.h look like? – jaggedSpire Oct 28 '16 at 18:52
  • 2
    This isn't the problem, but names that contain two consecutive underscores (`__shapeslib`) and names that begin with an underscore followed by a capital letter are reserved for use by the implementation. Don't use them. – Pete Becker Oct 28 '16 at 19:35
  • 1
    When you have an error on the first token of a header file, the error is probably in the header file that is preceding shape.h in the compilation of your translation unit. You can use `g++ -E` to inspect the result of the preprocessing. Maybe a missing `;`. – Franck Oct 28 '16 at 20:24
  • @jaggedSpire personally, I don't know whether is merely a problem of Atom or a problem in my code. The other header file, `vec.h`, looks like very similar: I have an initial `#ifndef [] #define` and a class declaration then. I'm assuming it could be an error of mine first, rather than an error in Atom. – Mattia Paterna Oct 28 '16 at 20:33
  • @PeteBecker, good to know, I'll rewrite it. I was told it's a good rule to write such a thing, especially if I implement my own header files. is that strictly necessary according to you? – Mattia Paterna Oct 28 '16 at 20:35
  • @Franck, thanks a lot for the suggestion. – Mattia Paterna Oct 28 '16 at 20:36
  • 1
    @MattiaPaterna - include guards are a good thing. Using reserved names to implement them isn't. – Pete Becker Oct 28 '16 at 20:50

1 Answers1

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.

Mattia Paterna
  • 1,268
  • 3
  • 15
  • 31