Make sure you put a semicolon ';' after the final '}' in the class declaration.
you will need to make some things "public" if you access them from outside the class. By default, members of a class are "private".
You should declare variables as members of the class if they are part of the object.
Here is an example of what I mean:
class OneWireLib
{
public:
OneWireLib()
{
int y =1; // this 'y' will go away once the constructor returns (it is a local variable)
somevar = 99; // initialize class member variables here in the constructor
}
void ConvertTemp()
{
int x =1;
}
int GetSomeVar() // example of method for using private data
{
return somevar;
}
private:
int somevar; // put variables that are part of the object here
}; // <=== note semicolon here... terminates the class declaration
OneWireLib onewire; // declare an instance of the OneWireLib class
void setup()
{
int x = onewire.GetSomeVar(); // example of calling a class method
}
// #include "onewire.pde"
OneWireLib onewire; // declare an instance of the OneWireLib class
void setup()
{
int x = onewire.GetSomeVar(); // example of calling a class method
}
void loop()
{
}
and the class file onewire.pde:
class OneWireLib
{
public:
OneWireLib()
{
int y =1; // this 'y' will go away once the constructor returns (it is a local variable)
somevar = 99; // initialize class member variables here in the constructor
}
void ConvertTemp()
{
int x =1;
}
int GetSomeVar() // example of method for using private data
{
return somevar;
}
private:
int somevar; // put variables that are part of the object here
}; // <=== note semicolon here... terminates the class declaration
the onewire.pde file is in the same directory as the testclass.pde file.
And again, all this code in one file does work.
OK, now I understand. I too have had problems trying to #include a header file in the same directory as my sketch. Looks like Arduino does NOT put the sketch directory inside the C++ include path. But it looks like maybe you are trying to make a reusable library. Try taking a look at this:
I didn't start the thread for a reusable library. It was more to get my code more readable. So I figured that a class in another tab would be helpful. After reading this http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1167857708 thread I figured it would be easy to do.
If the #include statement uses "double quotes", the compiler will check the directory that the including file is in (i.e. the sketch directory). You just need to add to your sketch a .h file with the appropriate contents, and place the #include statement at the top of the main sketch file, and you should be fine.
But is this .h necessary? If you start a new tab the file will be saved as a .pde file. So, I expected that a #include "onewire.pde" would work, but it doesn't.
Okay, I tried it. If I rename the onewire.pde to onewire.h and I use "#include "onewire.h" it works. And it gets better, if I save the file als foo.h it won't get the pde extension. But this might be something platform specific? (I am on OSX)
So, only one question left: why does .h work and .pde not?