Go Down

Topic: how to make and use a class (Read 1 time) previous topic - next topic

bigengineer

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: [Select]
class OneWireLib
{
OneWireLib()
{
 int y =1;
}
void ConvertTemp()
{
 int x =1;
}
}


In my main program I call the class like this:
Code: [Select]
OneWireLib owl;

CosineKitty

#1
Jan 06, 2007, 05:43 pm Last Edit: Jan 06, 2007, 06:02 pm by CosineKitty Reason: 1
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: [Select]

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
}

bigengineer

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.

CosineKitty

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

bigengineer

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

Go Up