Problem with writing my own library

I am writing my first library and keep getting error:
expected constructor, destructor, or type conversion before ‘;’ token

This is my .h

#ifndef Planet
#define Planet

#include "Arduino.h"

Class Planet {
  public:
    Planet();
    long axis();
    long excenter();
    long inclination();
    long longitude();
    long perihe();
    long node();
    long axisCorr();
    long excenterCorr();
    long inclinationCorr();
    long longitudeCorr();
    long perihelCorr();
    long nodeCorr();
  private:
    float elements[][12];
    
}

#endif

.cpp

#include "Arduino.h"
#include "Planet.h"

enum plane_t {
      MERCURY,
      VENUS,
      EARTH,
      MARS,
      JUPITER,
      SATURN,
      URANUS,
      NEPTUNE,
      PLUTO
    };

Planet::Planet(){
  plane_t = VENUS;
}

float elements[][12] = {
  
    {0.38709927,      0.20563593,     7.00497902,     252.25032350,     77.45779628,      48.33076593,    //Mercury
    0.00000037,       0.00001906,     -0.00594749,    149472.67411175,  0.16047689,       -0.12534081},
    {0.72333566,      0.00677672,     3.39467605,     181.97909950,     131.60246718,     76.67984255,    //Venus
    0.00000390,       -0.00004107,    -0.00078890,    58517.81538729,   0.00268329,       -0.27769418},
    {1.00000261,      0.01671123,     -0.00001531,    100.46457166,     102.93768193,     0.0,            //Earth
    0.00000562,       -0.00004392,    -0.01294668,    35999.37244981,   0.32327364,       0.0},
    {1.52371034,      0.09339410,     1.84969142,     -4.55343205,      -23.94362959,     49.55953891,    //Mars
    0.00001847,       0.00007882,     -0.00813131,    19140.30268499,   0.44441088,       -0.29257343},
    {5.20288700,      0.04838624,     1.30439695,     34.39644051,      14.72847983,      100.47390909,   //Jupiter
    -0.00011607,      -0.00013253,    -0.00183714,    3034.74612775,    0.21252668,       0.20469106},
    {9.53667594,      0.05386179,     2.48599187,     49.95424423,      92.59887831,      113.66242448,   //Saturn
    -0.00125060,      -0.00050991,    0.00193609,     1222.49362201,    -0.41897216,      -0.28867794},
    {19.18916464,     0.04725744,     0.77263783,     313.23810451,     170.95427630,     74.01692503,    //Uranus
    -0.00196176,      -0.00004397,    -0.00242939,    428.48202785,     0.40805281,       0.04240589},
    {30.06992276,     0.00859048,     1.77004347,     -55.12002969,     44.96476227,      131.78422574,   //Neptune
    0.00026291,       0.00005105,     0.00035372,     218.45945325,     -0.32241464,      -0.00508664},
    {39.48211675,     0.24882730,     17.14001206,    238.92903833,     224.06891629,     110.30393684,   //Pluto
    -0.00031596,      0.00005170,     0.00004818,     145.20780515,     -0.04062942,      -0.01183482}

  };


long Planet::axis(){
  long a = elements[plane_t][0];
  return a;
}

long Planet::excenter(){
  long e = elements[plane_t][1];
  return e;
}

long Planet::inclination(){
  long i = elements[plane_t][2];
  return i;
}

long Planet::longitude(){
  long l = elements[plane_t][3];
  return l;
}

long Planet::perihel(){
  long p = elements[plane_t][4];
  return p;
}

long Planet::node(){
  long o = elements[plane_t][5];
  return o;
}

long Planet::axisCorr(){
  long da = elements[plane_t][6];
  return da;
}

long Planet::excenterCorr(){
  long de = elements[plane_t][7];
  return de;
}

long Planet::inclinationCorr(){
  long di = elements[plane_t][8];
  return di;
}

long Planet::longitudeCorr(){
  long dl = elements[plane_t][9];
  return dl;
}

long Planet::perihelCorr(){
  long dp = elements[plane_t][10];
  return dp;
}

long Planet::nodeCorr(){
  long dn = elements[plane_t][11];
  return dn;
}

And sketch with error in line 3:

#include <Planet.h>

Planet planet();

void setup() {  
  Serial.begin(9600);
  Serial.println("Calculating mean anomaly:");
}

void loop(){
  long a = planet.axis();  
  Serial.println(a);
  delay(5000);
}

It’s probably some basic rookie mistake, but I’ve been googling and researching for 2 days trying to avoid posting stupid questions, and now I’m exhausted :slight_smile:

I see quite a few errors:

  • When defining a class, the keyword is "class", not "Class". Capitalization counts.

  • The definition of a class ends with a ";" after the last "}", just like a struct.

  • plane_t is a TYPE, not the name of a variable.

  • "Planet planet();" is a prototype for a function named "planet" that takes not arguments and returns an object of the Planet class. "Planet planet;" defines an object of the class Planet named "planet". I think you want the latter.

There's probably more, but get those cleared up first.

Also, when first trying to get your class to compile and work, it’s better to put everything into one .ino file as below. This makes it easier to move through the code to fix errors. More importantly, it makes it easier for folks on the forum to help you. They just have to copy one code section and paste it into the IDE rather than creating 3 different files. You can break it in to separate .h and .cpp files once you get it working.

#include "Arduino.h"

Class Planet {
public:
  Planet();
  long axis();
  long excenter();
  long inclination();
  long longitude();
  long perihe();
  long node();
  long axisCorr();
  long excenterCorr();
  long inclinationCorr();
  long longitudeCorr();
  long perihelCorr();
  long nodeCorr();
private:
  float elements[][12];

}

enum plane_t {
  MERCURY,
  VENUS,
  EARTH,
  MARS,
  JUPITER,
  SATURN,
  URANUS,
  NEPTUNE,
  PLUTO
};

Planet::Planet() {
  plane_t = VENUS;
}

float elements[][12] = {

  { 0.38709927,      0.20563593,     7.00497902,     252.25032350,     77.45779628,      48.33076593,    //Mercury
    0.00000037,       0.00001906,     -0.00594749,    149472.67411175,  0.16047689,       -0.12534081
  },
  { 0.72333566,      0.00677672,     3.39467605,     181.97909950,     131.60246718,     76.67984255,    //Venus
    0.00000390,       -0.00004107,    -0.00078890,    58517.81538729,   0.00268329,       -0.27769418
  },
  { 1.00000261,      0.01671123,     -0.00001531,    100.46457166,     102.93768193,     0.0,            //Earth
    0.00000562,       -0.00004392,    -0.01294668,    35999.37244981,   0.32327364,       0.0
  },
  { 1.52371034,      0.09339410,     1.84969142,     -4.55343205,      -23.94362959,     49.55953891,    //Mars
    0.00001847,       0.00007882,     -0.00813131,    19140.30268499,   0.44441088,       -0.29257343
  },
  { 5.20288700,      0.04838624,     1.30439695,     34.39644051,      14.72847983,      100.47390909,   //Jupiter
    -0.00011607,      -0.00013253,    -0.00183714,    3034.74612775,    0.21252668,       0.20469106
  },
  { 9.53667594,      0.05386179,     2.48599187,     49.95424423,      92.59887831,      113.66242448,   //Saturn
    -0.00125060,      -0.00050991,    0.00193609,     1222.49362201,    -0.41897216,      -0.28867794
  },
  { 19.18916464,     0.04725744,     0.77263783,     313.23810451,     170.95427630,     74.01692503,    //Uranus
    -0.00196176,      -0.00004397,    -0.00242939,    428.48202785,     0.40805281,       0.04240589
  },
  { 30.06992276,     0.00859048,     1.77004347,     -55.12002969,     44.96476227,      131.78422574,   //Neptune
    0.00026291,       0.00005105,     0.00035372,     218.45945325,     -0.32241464,      -0.00508664
  },
  { 39.48211675,     0.24882730,     17.14001206,    238.92903833,     224.06891629,     110.30393684,   //Pluto
    -0.00031596,      0.00005170,     0.00004818,     145.20780515,     -0.04062942,      -0.01183482
  }

};


long Planet::axis() {
  long a = elements[plane_t][0];
  return a;
}

long Planet::excenter() {
  long e = elements[plane_t][1];
  return e;
}

long Planet::inclination() {
  long i = elements[plane_t][2];
  return i;
}

long Planet::longitude() {
  long l = elements[plane_t][3];
  return l;
}

long Planet::perihel() {
  long p = elements[plane_t][4];
  return p;
}

long Planet::node() {
  long o = elements[plane_t][5];
  return o;
}

long Planet::axisCorr() {
  long da = elements[plane_t][6];
  return da;
}

long Planet::excenterCorr() {
  long de = elements[plane_t][7];
  return de;
}

long Planet::inclinationCorr() {
  long di = elements[plane_t][8];
  return di;
}

long Planet::longitudeCorr() {
  long dl = elements[plane_t][9];
  return dl;
}

long Planet::perihelCorr() {
  long dp = elements[plane_t][10];
  return dp;
}

long Planet::nodeCorr() {
  long dn = elements[plane_t][11];
  return dn;
}

Planet planet();

void setup() {
  Serial.begin(9600);
  Serial.println("Calculating mean anomaly:");
}

void loop() {
  long a = planet.axis();
  Serial.println(a);
  delay(5000);
}

Thank you very much for looking into my code. I’m a bit embarrassed of so many mistakes :slight_smile:

Nevertheless, I changed everything as you said, but still have an error:

‘planet’ does not name a type

#include <Planet.h>

Planet planet;

void setup() {  
  Serial.begin(9600);
  Serial.println("Calculating mean anomaly:");
}

void loop(){
  long a = planet.axis();  
  Serial.println(a);
  delay(5000);
}

in .cpp

Planet::Planet(){
  plane_t _planet = VENUS;
}

//.....................................

long Planet::axis(){
  long a = elements[_planet][0];
  return a;
}

What am I missing?

gfvalvo:
Also, when first trying to get your class to compile and work, it's better to put everything into one .ino file as below. This makes it easier to move through the code to fix errors. More importantly, it makes it easier for folks on the forum to help you. They just have to copy one code section and paste it into the IDE rather than creating 3 different files. You can break it in to separate .h and .cpp files once you get it working.

Thank you! This is really nice advice. I am going to do so in the future.

Svemirko:
Thank you very much for looking into my code. I'm a bit embarrassed of so many mistakes :slight_smile:

Nevertheless, I changed everything as you said, but still have an error:

'planet' does not name a type

What am I missing?

Post the complete code - a single file as requested. Post the complete error message copied directly from the lower window of the IDE.

#include "Arduino.h"

class Planet {
  public:
    Planet();
    long axis();
    long excenter();
    long inclination();
    long longitude();
    long perihel();
    long node();
    long axisCorr();
    long excenterCorr();
    long inclinationCorr();
    long longitudeCorr();
    long perihelCorr();
    long nodeCorr();
  private:
    //float elements[][12];
};

enum plane_t {
      MERCURY,
      VENUS,
      EARTH,
      MARS,
      JUPITER,
      SATURN,
      URANUS,
      NEPTUNE,
      PLUTO
    };
    
plane_t _planet = MARS;

Planet::Planet(){
  
}

float elements[][12] = {
  
    {0.38709927,      0.20563593,     7.00497902,     252.25032350,     77.45779628,      48.33076593,    //Mercury
    0.00000037,       0.00001906,     -0.00594749,    149472.67411175,  0.16047689,       -0.12534081},
    {0.72333566,      0.00677672,     3.39467605,     181.97909950,     131.60246718,     76.67984255,    //Venus
    0.00000390,       -0.00004107,    -0.00078890,    58517.81538729,   0.00268329,       -0.27769418},
    {1.00000261,      0.01671123,     -0.00001531,    100.46457166,     102.93768193,     0.0,            //Earth
    0.00000562,       -0.00004392,    -0.01294668,    35999.37244981,   0.32327364,       0.0},
    {1.52371034,      0.09339410,     1.84969142,     -4.55343205,      -23.94362959,     49.55953891,    //Mars
    0.00001847,       0.00007882,     -0.00813131,    19140.30268499,   0.44441088,       -0.29257343},
    {5.20288700,      0.04838624,     1.30439695,     34.39644051,      14.72847983,      100.47390909,   //Jupiter
    -0.00011607,      -0.00013253,    -0.00183714,    3034.74612775,    0.21252668,       0.20469106},
    {9.53667594,      0.05386179,     2.48599187,     49.95424423,      92.59887831,      113.66242448,   //Saturn
    -0.00125060,      -0.00050991,    0.00193609,     1222.49362201,    -0.41897216,      -0.28867794},
    {19.18916464,     0.04725744,     0.77263783,     313.23810451,     170.95427630,     74.01692503,    //Uranus
    -0.00196176,      -0.00004397,    -0.00242939,    428.48202785,     0.40805281,       0.04240589},
    {30.06992276,     0.00859048,     1.77004347,     -55.12002969,     44.96476227,      131.78422574,   //Neptune
    0.00026291,       0.00005105,     0.00035372,     218.45945325,     -0.32241464,      -0.00508664},
    {39.48211675,     0.24882730,     17.14001206,    238.92903833,     224.06891629,     110.30393684,   //Pluto
    -0.00031596,      0.00005170,     0.00004818,     145.20780515,     -0.04062942,      -0.01183482}

  };


long Planet::axis(){
  long a = elements[_planet][0]*100000000;
  return a;
}

long Planet::excenter(){
  long e = elements[_planet][1]*100000000;
  return e;
}

long Planet::inclination(){
  long i = elements[_planet][2]*100000000;
  return i;
}

long Planet::longitude(){
  long l = elements[_planet][3]*100000000;
  return l;
}

long Planet::perihel(){
  long p = elements[_planet][4]*100000000;
  return p;
}

long Planet::node(){
  long o = elements[_planet][5]*100000000;
  return o;
}

long Planet::axisCorr(){
  long da = elements[_planet][6]*100000000;
  return da;
}

long Planet::excenterCorr(){
  long de = elements[_planet][7]*100000000;
  return de;
}

long Planet::inclinationCorr(){
  long di = elements[_planet][8]*100000000;
  return di;
}

long Planet::longitudeCorr(){
  long dl = elements[_planet][9]*100000000;
  return dl;
}

long Planet::perihelCorr(){
  long dp = elements[_planet][10]*100000000;
  return dp;
}

long Planet::nodeCorr(){
  long dn = elements[_planet][11]*100000000;
  return dn;
}


Planet planet;

void setup() {  
  Serial.begin(9600);
  Serial.println("Calculating mean anomaly:");
}

void loop(){
  long a = planet.inclination();  
  Serial.println(_planet);
  Serial.println("fdfs");
  Serial.println(a);
  delay(5000);
}

When I run it like this, from single .ino file everything is ok.
I get error when I run it like a library:

Arduino: 1.8.9 (Windows 10), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

In file included from C:\Users\denisov\Documents\Arduino\Planet_test\planet_test\planet_test.ino:1:0:

C:\Users\denisov\Documents\Arduino\libraries\Planet/Planet.h:8:12: error: expected unqualified-id before ')' token

     Planet();

            ^

C:\Users\denisov\Documents\Arduino\libraries\Planet/Planet.h:6:14: error: an anonymous struct cannot have function members

 class Planet {

              ^

C:\Users\denisov\Documents\Arduino\libraries\Planet/Planet.h:22:1: error: abstract declarator '<anonymous class>' used as declaration

 };

 ^

planet_test:4:8: error: 'planet' does not name a type

 Planet planet;

        ^

C:\Users\denisov\Documents\Arduino\Planet_test\planet_test\planet_test.ino: In function 'void loop()':

planet_test:12:12: error: 'planet' was not declared in this scope

   long a = planet.longitude();  

            ^

planet_test:13:18: error: '_planet' was not declared in this scope

   Serial.println(_planet);

                  ^

exit status 1
'planet' does not name a type

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Your #include guard in the .h file (#ifndef Planet) has the same name as the class (Planet). That’s not good.

Also, I think it’s rather odd what you’re doing with the variable “_planet”. Why are all the class’s methods using this variable like this:

long Planet::inclination(){
  long i = elements[_planet][2]*100000000;
  return i;
}

Why not pass the desired planet in as an argument to the function???

But, regardless, the following does compile despite being an odd way to do things:

.h File:

#ifndef PLANE_H
#define PLANE_H

#include "Arduino.h"
class Planet {
  public:
    Planet();
    long axis();
    long excenter();
    long inclination();
    long longitude();
    long perihel();
    long node();
    long axisCorr();
    long excenterCorr();
    long inclinationCorr();
    long longitudeCorr();
    long perihelCorr();
    long nodeCorr();
  private:
    static float elements[][12];
};

enum plane_t {
      MERCURY,
      VENUS,
      EARTH,
      MARS,
      JUPITER,
      SATURN,
      URANUS,
      NEPTUNE,
      PLUTO
    };
    
extern plane_t _planet;
#endif

.cpp File:

#include "Planet.h"

plane_t _planet = MARS;

Planet::Planet() {
}

float Planet::elements[][12] = {

  { 0.38709927,      0.20563593,     7.00497902,     252.25032350,     77.45779628,      48.33076593,    //Mercury
    0.00000037,       0.00001906,     -0.00594749,    149472.67411175,  0.16047689,       -0.12534081
  },
  { 0.72333566,      0.00677672,     3.39467605,     181.97909950,     131.60246718,     76.67984255,    //Venus
    0.00000390,       -0.00004107,    -0.00078890,    58517.81538729,   0.00268329,       -0.27769418
  },
  { 1.00000261,      0.01671123,     -0.00001531,    100.46457166,     102.93768193,     0.0,            //Earth
    0.00000562,       -0.00004392,    -0.01294668,    35999.37244981,   0.32327364,       0.0
  },
  { 1.52371034,      0.09339410,     1.84969142,     -4.55343205,      -23.94362959,     49.55953891,    //Mars
    0.00001847,       0.00007882,     -0.00813131,    19140.30268499,   0.44441088,       -0.29257343
  },
  { 5.20288700,      0.04838624,     1.30439695,     34.39644051,      14.72847983,      100.47390909,   //Jupiter
    -0.00011607,      -0.00013253,    -0.00183714,    3034.74612775,    0.21252668,       0.20469106
  },
  { 9.53667594,      0.05386179,     2.48599187,     49.95424423,      92.59887831,      113.66242448,   //Saturn
    -0.00125060,      -0.00050991,    0.00193609,     1222.49362201,    -0.41897216,      -0.28867794
  },
  { 19.18916464,     0.04725744,     0.77263783,     313.23810451,     170.95427630,     74.01692503,    //Uranus
    -0.00196176,      -0.00004397,    -0.00242939,    428.48202785,     0.40805281,       0.04240589
  },
  { 30.06992276,     0.00859048,     1.77004347,     -55.12002969,     44.96476227,      131.78422574,   //Neptune
    0.00026291,       0.00005105,     0.00035372,     218.45945325,     -0.32241464,      -0.00508664
  },
  { 39.48211675,     0.24882730,     17.14001206,    238.92903833,     224.06891629,     110.30393684,   //Pluto
    -0.00031596,      0.00005170,     0.00004818,     145.20780515,     -0.04062942,      -0.01183482
  }

};


long Planet::axis() {
  long a = elements[_planet][0] * 100000000;
  return a;
}

long Planet::excenter() {
  long e = elements[_planet][1] * 100000000;
  return e;
}

long Planet::inclination() {
  long i = elements[_planet][2] * 100000000;
  return i;
}

long Planet::longitude() {
  long l = elements[_planet][3] * 100000000;
  return l;
}

long Planet::perihel() {
  long p = elements[_planet][4] * 100000000;
  return p;
}

long Planet::node() {
  long o = elements[_planet][5] * 100000000;
  return o;
}

long Planet::axisCorr() {
  long da = elements[_planet][6] * 100000000;
  return da;
}

long Planet::excenterCorr() {
  long de = elements[_planet][7] * 100000000;
  return de;
}

long Planet::inclinationCorr() {
  long di = elements[_planet][8] * 100000000;
  return di;
}

long Planet::longitudeCorr() {
  long dl = elements[_planet][9] * 100000000;
  return dl;
}

long Planet::perihelCorr() {
  long dp = elements[_planet][10] * 100000000;
  return dp;
}

long Planet::nodeCorr() {
  long dn = elements[_planet][11] * 100000000;
  return dn;
}

.ino File:

#include "Planet.h"

Planet planet;

void setup() {  
  Serial.begin(9600);
  Serial.println("Calculating mean anomaly:");
}

void loop(){
  long a = planet.inclination();  
  Serial.println(_planet);
  Serial.println("fdfs");
  Serial.println(a);
  delay(5000);
}

Found it... It was in:

#define Planet_h

in my .h file... I forgot _h in the end.

Thank you very much for your help. I appreciate it.