Error when creating library

The example library, Morse.h, is this:

/*
  Morse.h - Library for flashing Morse code.
  Created by David A. Mellis, November 2, 2007.
  Released into the public domain.
*/
#ifndef Morse_h
#define Morse_h

#include "Arduino.h"

class Morse
{
  public:
    Morse(int pin);
    void dot();
    void dash();
  private:
    int _pin;
};

#endif

I am making my own library for recursive functions. Whenever I use it in a sketch, the sketch has this compilation error:

arduino-1.6.4-windows\arduino-1.6.4\libraries\Recursives/Recursives.h:6:2: error: invalid preprocessing directive #ifindef
 #ifindef Recursives_h
  ^

Skip a little bit…

arduino-1.6.4-windows\arduino-1.6.4\libraries\Recursives/Recursives.h:21:2: error: #endif without #if
 #endif
  ^

I left out part if it, but I don’t think it matters, say, what folder arduino-1.6.4-windows is in.

What’s happening? Does anyone know how to fix it?

bbrk24:

arduino-1.6.4-windows\arduino-1.6.4\libraries\Recursives/Recursives.h:6:2: error: invalid preprocessing directive #ifindef

#ifindef Recursives_h
 ^



What's happening? Does anyone know how to fix it?

change #ifindef to #ifndef

pert:
change #ifindef to #ifndef

That solved part of the problem. Now it just tells me that the function I’m trying to use from the library “was not declared in this scope.”

Please provide:

  • Your library
  • Your sketch
  • Complete error message

Use code tags(</> button) for all 3.

Library header (.h):

/*
  Recursives.h - Library containing recursive functions 
  not included by default - often  with large outputs.
  Created by Billy Baker (bbrk24) on March 12th, 2016.
*/
#ifndef Recursives_h
#define Recursives_h

#include "Arduino.h"

class Recursives
{
  public:
    unsigned long ack(byte m, byte n);
    unsigned long Fib(byte iter);
    unsigned long fact(byte n);
    unsigned int gcd(unsigned int a, unsigned int b);
    unsigned int lcm(unsigned int a, unsigned int b);
};

#endif

Library source code (.ccp):

#include "Arduino.h"
#include "Recursives.h"

unsigned long Recursives::ack(byte m, byte n)
{
  if(m==0){return n+1;}
  else{if(n==0){return ack(m-1,1);}
  else{return ack(m-1,ack(m,n-1));}}
}

unsigned long Recursives::Fib(byte iter) // technically primitive recursive, but whatever
{
  unsigned long prev = 1;
  unsigned long curr = 0;
  for(iter; iter; iter--) {
    curr = curr + prev;
    prev = curr - prev;
  }
  return curr;
}

unsigned long Recursives::fact(byte n)
{
  if(n) {
    return fact(n - 1);
  } else {
    return 1;
  }
}


unsigned int Recursives::gcd(unsigned int a, unsigned int b)
{
  if (b)
    return gcd(b, a % b);
  } else {
    return a;
  }
}

unsigned int Recursives::lcm(unsigned int a, unsigned int b)
{
  return (a * b) / gcd(a, b); // Someone call Staples because that was easy.
}

Sketch (.ino):

#include "Recursives.h"

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  for (byte b = 0; b < 48; b ++) {
    Serial.println(Fib(b));
  }
  Serial.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); // Adds a bunch of newlines
}

void loop() {
  // put your main code here, to run repeatedly:
  while(!Serial.available()){}
  byte i = 0;
  char next = Serial.read();
  while ((next - '0') >= 0 && ('9' - next) >= 0) {
    i = (i * 10) + (next - '0');
    next = Serial.read();
    if (next == 0 || next == -1 || next == 255) {
      delay(3);
      next = Serial.read();
    }
  }
  for (byte b = 0; b < i; b ++) {
    Serial.println(Fib(b));
    delay(1000);
  }
  Serial.println();
}

/*
unsigned long Fib(byte iter) { // technically primitive recursive, but whatever
  unsigned long prev = 1;
  unsigned long curr = 0;
  for(iter; iter; iter--) {
    curr = curr + prev;
    prev = curr - prev;
  }
  return curr;
}
*/

Complete error message:

Arduino: 1.6.4 (Windows 7), Board: "Arduino Uno"

sketch_mar12b.ino: In function 'void setup()':
sketch_mar12b:7: error: 'Fib' was not declared in this scope
sketch_mar12b.ino: In function 'void loop()':
sketch_mar12b:26: error: 'Fib' was not declared in this scope
'Fib' was not declared in this scope

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

Try this sketch:

#include "Recursives.h"
Recursives recursives;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  for (byte b = 0; b < 48; b ++) {
    Serial.println(recursives.Fib(b));
  }
  Serial.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); // Adds a bunch of newlines
}

void loop() {
  // put your main code here, to run repeatedly:
  while(!Serial.available()){}
  byte i = 0;
  char next = Serial.read();
  while ((next - '0') >= 0 && ('9' - next) >= 0) {
    i = (i * 10) + (next - '0');
    next = Serial.read();
    if (next == 0 || next == -1 || next == 255) {
      delay(3);
      next = Serial.read();
    }
  }
  for (byte b = 0; b < i; b ++) {
    Serial.println(recursives.Fib(b));
    delay(1000);
  }
  Serial.println();
}

/*
unsigned long Fib(byte iter) { // technically primitive recursive, but whatever
  unsigned long prev = 1;
  unsigned long curr = 0;
  for(iter; iter; iter--) {
    curr = curr + prev;
    prev = curr - prev;
  }
  return curr;
}
*/

And there was a missing bracket on line 34 of Recursives.cpp:

#include "Arduino.h"
#include "Recursives.h"

unsigned long Recursives::ack(byte m, byte n)
{
  if(m==0){return n+1;}
  else{if(n==0){return ack(m-1,1);}
  else{return ack(m-1,ack(m,n-1));}}
}

unsigned long Recursives::Fib(byte iter) // technically primitive recursive, but whatever
{
  unsigned long prev = 1;
  unsigned long curr = 0;
  for(iter; iter; iter--) {
    curr = curr + prev;
    prev = curr - prev;
  }
  return curr;
}

unsigned long Recursives::fact(byte n)
{
  if(n) {
    return fact(n - 1);
  } else {
    return 1;
  }
}


unsigned int Recursives::gcd(unsigned int a, unsigned int b)
{
  if (b) {
    return gcd(b, a % b);
  } else {
    return a;
  }
}

unsigned int Recursives::lcm(unsigned int a, unsigned int b)
{
  return (a * b) / gcd(a, b); // Someone call Staples because that was easy.
}

Also a couple of warnings that you might want to look into:

C:\Users\per\AppData\Local\Temp\arduino_modified_sketch_613212\sketch_mar13a.ino: In function 'void loop()':

C:\Users\per\AppData\Local\Temp\arduino_modified_sketch_613212\sketch_mar13a.ino:21:44: warning: comparison is always false due to limited range of data type [-Wtype-limits]

     if (next == 0 || next == -1 || next == 255) {

                                            ^

E:\Stuff\misc\electronics\arduino\libraries\Recursives\Recursives.cpp: In member function 'long unsigned int Recursives::Fib(byte)':

E:\Stuff\misc\electronics\arduino\libraries\Recursives\Recursives.cpp:15:11: warning: statement has no effect [-Wunused-value]

   for(iter; iter; iter--) {

           ^

Also, note that you are supposed to install libraries to the libraries folder in your sketchbook folder instead of the libraries folder in your Arduino IDE installation folder otherwise when you update the IDE your libraries will be lost.

pert:
Also, note that you are supposed to install libraries to the libraries folder in your sketchbook folder instead of the libraries folder in your Arduino IDE installation folder otherwise when you update the IDE your libraries will be lost.

I never updated to or from 1.6.4.

The line " if (next == 0 || next == -1 || next == 255) {" is to detect for ascii null - 0, or sometimes it acts like -1, which wraps around to 255.

What does the line "Recursives recursives;" do?
It still doesn't work.

bbrk24:
What does the line "Recursives recursives;" do?

Creates an instance of the Recursives class called recursives. It is an analog of:

int myVariable;

both are challenging names, not unlike Fib() or fact().

for what its worth, if all you want to do is add recursive functions to the base IDE, why use a Class to describe them? You then need to go through the trouble of having a class instance to operate the function... if you want to add a Fibonacci function, would you only operate it over class instance?

Just create the functions in a header file and include them in your sketch.

It still doesn't work.

That is just too lame to bother replying to.

BulldogLowell:
both are challenging names, not unlike Fib() or fact().

for what its worth, if all you want to do is add recursive functions to the base IDE, why use a Class to describe them? You then need to go through the trouble of having a class instance to operate the function… if you want to add a Fibonacci function, would you only operate it over class instance?

I just want to create a library so that I don’t have to define these functions in every single sketch I make that has one of these.

Just create the functions in a header file and include them in your sketch.

That is, as far as I’m aware, what I’m trying to do. It gives me errors because it, for some reason, treats the Fib function like a variable or something and tells me I never declared it!

like this:

#ifndef Recursives_h
#define Recursives_h

#include "Arduino.h"

unsigned long ack(byte m, byte n)
{
  if (m == 0) {
    return n + 1;
  }
  else {
    if (n == 0) {
      return ack(m - 1, 1);
    }
    else {
      return ack(m - 1, ack(m, n - 1));
    }
  }
}

unsigned long Fib(byte iter) // technically primitive recursive, but whatever
{
  unsigned long prev = 1;
  unsigned long curr = 0;
  for (iter; iter; iter--) {
    curr = curr + prev;
    prev = curr - prev;
  }
  return curr;
}

unsigned long fact(byte n)
{
  if (n) {
    return fact(n - 1);
  } else {
    return 1;
  }
}


unsigned int gcd(unsigned int a, unsigned int b)
{
  if (b)
  {
    return gcd(b, a % b);
  }
  else
  {
    return a;
  }
}


unsigned int lcm(unsigned int a, unsigned int b)
{
  return (a * b) / gcd(a, b); // Someone call Staples because that was easy.
}

#endif

and in a sketch:

#include"Recursives.h"

int myVar = 44;
void setup()
{
  Serial.begin(9600);
  Serial.println(Fib(myVar));
}

void loop() 
{

}