Go Down

Topic: Making a library: I must be missing some steps... (Read 389 times) previous topic - next topic

Old_guy

I looked at the example at http://www.arduino.cc/playground/Code/Library and there is some mention there about compiling the .h and .cpp file and an object file that must be deleted if the source is changed. Now, compile with what? I guess the IDE does not generate object files...

I coded a simple .h and .cpp file in notepad then tried to import it into the sketch I have in Arduino IDE 022. Is this not possible?

I get these errors when I try to run the below sketch:

testclass:-1: error: new types may not be defined in a return type
testclass.cpp:3: note: (perhaps a semicolon is missing after the definition of 'testclass')
testclass:-1: error: two or more data types in declaration of 'setup'
testclass.cpp: In function 'void setup()':
testclass:4: error: request for member 'addnum' in 'tc', which is of non-class type 'testclass ()()'


testclass.h:
Code: [Select]
#ifndef testclass_h
#define testclass_h

#include <WProgram.h>

class testclass{
   public:
      testclass();
      int addnum(int a, int b);
}
#endif


testclass.cpp:
Code: [Select]
#include <testclass.h>

testclass::testclass(){}

int testclass::addnum(int a, int b)
{
   return (a+b);
}


Sketch:
Quote

#include <testclass.h>
testclass tc();

void setup(){
  tc.addnum(1,1);
}

void loop(){
  
}



Somebody please enlighten me!

Old guy.

Nick Gammon

Quote
Code: [Select]
testclass tc();


Do you mean:

Code: [Select]
testclass tc;

drone

... and don't ignore this error:



testclass:-1: error: new types may not be defined in a return type
testclass.cpp:3: note: (perhaps a semicolon is missing after the definition of 'testclass')



You're missing a semi-colon:


testclass.h:
Code: [Select]
#ifndef testclass_h
#define testclass_h

#include <WProgram.h>

class testclass{
   public:
      testclass();
      int addnum(int a, int b);
}; // semi-colon required
#endif




!c

Old_guy

Thanks guys! Another set of eyes is always a good thing! I will eventually get up to speed...

Old guy.

Go Up