Pages: [1]   Go Down
Author Topic: how to make and use a class  (Read 1059 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 183
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is it possible to make a class in a .pde file? Something like this processing example? http://webzone.k3.mah.se/projects/arduino-workshop/projects/arduino_meets_processing/instructions/accelero.html
When I make a new tab in which I make a class arduino say's: "error: 'OneWireLib' does not name a type" Here is "OneWireLib" the name of the .pde file and the class.
For now the class is virtually empty:

Code:
class OneWireLib
{
OneWireLib()
{
  int y =1;
}
void ConvertTemp()
{
  int x =1;
}
}

In my main program I call the class like this:
Code:
OneWireLib owl;
Logged

Florida, USA
Offline Offline
Full Member
***
Karma: 0
Posts: 146
meow!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I see a few things that might help:
- 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:

Code:
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
}
« Last Edit: January 06, 2007, 12:02:52 pm by CosineKitty » Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 183
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried you're example using a second tab and it doesn't work. This is the error:
error: 'OneWireLib' does not name a type In function 'void test()':


But, if I try it in one file it does work. What's the difference? I tried to #include the file but it is not found.
Logged

Florida, USA
Offline Offline
Full Member
***
Karma: 0
Posts: 146
meow!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Please post your code and I will see if I can help.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 183
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The mainfile testclass.pde:
Code:
// #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:
Code:
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.
Logged

Florida, USA
Offline Offline
Full Member
***
Karma: 0
Posts: 146
meow!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

http://www.arduino.cc/en/Main/Libraries
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 183
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley

Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 9
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 183
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: January 06, 2007, 04:36:39 pm by bigengineer » Logged

Pages: [1]   Go Up
Jump to: