A
A
Artem2016-07-01 12:19:29
Arduino
Artem, 2016-07-01 12:19:29

What is the correct way to encapsulate methods of one class into methods of another?

Hello. I need to encapsulate the methods of the Ethernet library, and the SPI running under it, into my class.
Here is how I tried to do it:
lib.h

#ifndef lib_h
#define lib_h

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

class lib {
  public: 
    void add_var(char *varname, int var_value); 
    bool connect_open();
    void send();
};
#endif

lib.cpp
#include <lib.h>

bool libr::connect_open() {
    #include <SPI.h>
    #include <Ethernet.h>
    EthernetClient client;
    byte addr[4]= {8,8,8,8};
    if (client.connect(addr, 80)) {
      Serial.println("Connect is done");
      return true;
    }
    else{
      Serial.println("Connect is failed");
      return false;
    }
}

And it gives me the following errors:
C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SPI\src/SPI.h: In member function 'bool lib::connect_open()':

C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SPI\src/SPI.h:313:18: error: local class 'class lib::connect_open()::SPIClass' shall not have static data member 'uint8_t lib::connect_open()::SPIClass::initialized' [-fpermissive]

   static uint8_t initialized;

                  ^

C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SPI\src/SPI.h:314:18: error: local class 'class lib::connect_open()::SPIClass' shall not have static data member 'uint8_t lib::connect_open()::SPIClass::interruptMode' [-fpermissive]

   static uint8_t interruptMode; // 0=none, 1=mask, 2=global

                  ^

C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SPI\src/SPI.h:315:18: error: local class 'class lib::connect_open()::SPIClass' shall not have static data member 'uint8_t lib::connect_open()::SPIClass::interruptMask' [-fpermissive]

   static uint8_t interruptMask; // which interrupts to mask

                  ^

C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SPI\src/SPI.h:316:18: error: local class 'class lib::connect_open()::SPIClass' shall not have static data member 'uint8_t lib::connect_open()::SPIClass::interruptSave' [-fpermissive]

   static uint8_t interruptSave; // temp storage, to restore state

                  ^

In file included from C:\Program Files (x86)\Arduino\libraries\Ethernet\src/Ethernet.h:7:0,

                 from C:\Users\lib\Documents\Arduino\libraries\lib\lib.cpp:5:

C:\Program Files (x86)\Arduino\libraries\Ethernet\src/EthernetClient.h:38:19: error: local class 'class lib::connect_open()::EthernetClient' shall not have static data member 'uint16_t lib::connect_open()::EthernetClient::_srcport' [-fpermissive]

   static uint16_t _srcport;

                   ^

In file included from C:\Users\lib\Documents\Arduino\libraries\lib\lib.cpp:5:0:

C:\Program Files (x86)\Arduino\libraries\Ethernet\src/Ethernet.h:18:37: error: local class 'class lib::connect_open()::EthernetClass' shall not have static data member 'uint8_t lib::connect_open()::EthernetClass::_state [4]' [-fpermissive]

   static uint8_t _state[MAX_SOCK_NUM];

                                     ^

C:\Program Files (x86)\Arduino\libraries\Ethernet\src/Ethernet.h:19:44: error: local class 'class lib::connect_open()::EthernetClass' shall not have static data member 'uint16_t lib::connect_open()::EthernetClass::_server_port [4]' [-fpermissive]

   static uint16_t _server_port[MAX_SOCK_NUM];

                                            ^

What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Zhilin, 2016-07-01
@proudmore

Never do #includeanywhere other than the beginning of a file.

V
vanyamba-electronics, 2016-07-07
@vanyamba-electronics

1 mistake. An include within a function. Place these includes at the top of the lib.h file.
2 error. In the lib::open() method, you are trying to create a local version of the EthernetClient object on the stack. When the method completes, its class destructor must be called.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question