Arduino Forum

Development => Other Software Development => Topic started by: qzshush on Jan 16, 2012, 10:29 am

Title: first time wrinting a library
Post by: qzshush on Jan 16, 2012, 10:29 am
Hey Guys,

I am in the process of writing my first library (sadly without having used c or c++ before) - I hope somebody can give me some assistance - I'd appreciate it:

That's what I wrote so far:

Code: [Select]

//IMCLibrary.h
#ifndef IMCLibrary
#define IMCLibrary

#if defined(ARDUINO) && ARDUINO >= 100
  #include "Arduino.h"
#else
  #include "WProgram.h"
  #include <pins_arduino.h>
#endif

class IMCLib
{
  public:
IMCLib(); //constructor
void Spass();
  private:
};
#endif


Code: [Select]

//IMCLibrary.ccp
#include "WProgram.h"
#include "IMCLibrary.h"


IMCLib::IMCLib(void)
{

}


void IMCLib::Spass()
{
      Serial.print("Spass!");
}


&

Code: [Select]

//Library_Test.ino
#include <IMCLibrary.h>
double TA=0.3;


IMCLib myLib();

void setup()
{
  Serial.begin(57600);
}

void loop()
{
  myLib.Spass();  // THIS IS THE PROBLEM
  Serial.print("Phi: ");
  Serial.println(TA);
}


When compiling I get the following error:

Quote

Library_Test.cpp: In function 'void loop()':
Library_Test:15: error: request for member 'Spass' in 'myLib', which is of non-class type 'IMCLib ()()'


Could someone please help me ?

Thanks
Title: Re: first time wrinting a library
Post by: PaulS on Jan 16, 2012, 11:58 am
Code: [Select]
#if defined(ARDUINO) && ARDUINO >= 100
  #include "Arduino.h"
#else
  #include "WProgram.h"
  #include <pins_arduino.h>
#endif

Good.

Code: [Select]
//IMCLibrary.ccp
#include "WProgram.h"

Not so good.

Code: [Select]
IMCLib myLib();
Bad. The parentheses should NOT be here.
Title: Re: first time wrinting a library
Post by: qzshush on Jan 16, 2012, 01:24 pm

Code: [Select]
IMCLib myLib();
Bad. The parentheses should NOT be here.


Thanks for looking at the code. But just neglecting the brackets doesn't help
Quote
undefined reference to `IMCLib::IMCLib()'


Do I need to change something in the header IMCLibrary.h as well ?

Best
Stefan
Title: Re: first time wrinting a library
Post by: PaulS on Jan 16, 2012, 01:32 pm
Quote
Thanks for looking at the code. But just neglecting the brackets doesn't help

It changed the error message. How does that not help?

The name of the class and the names of the files need to match. You have files named IMCLibrary.h and IMCLibrary.cpp containing a class named IMCLib.

I copied your sketch and files, renaming the header and source to match the class. Once I changed the <> on the include statement in the sketch to "" (since I have the files in the same directory as the sketch, I get only one "error":
Code: [Select]
Binary sketch size: 3356 bytes (of a 30720 byte maximum)

Title: Re: first time wrinting a library
Post by: qzshush on Jan 16, 2012, 02:43 pm
Ok changed the names of the files to IMCLib.h and IMCLib.ccp and got rid of the paraentheses after "'IMCLib myLib;" - But the error-message stays
Quote

undefined reference to `IMCLib::IMCLib()'


I just copied and pasted the Morse Library from the main page and it gives the same error... so it seems to be a more general problem...
Title: Re: first time wrinting a library
Post by: qzshush on Jan 16, 2012, 03:55 pm
ok I ashamed to admit that after 8 hours I found that the problem was that I used "IMCLib.ccp" instead of "IMC.cpp"  :smiley-roll-blue: