Trying to create and store an instance of a class
hi all,
i'm having trouble creating , storing instance of newsoftserial class inside own relay class.
i'm experienced in java , php, haven't done lot in c++. i think i've grasped pointers, there must quite fundamental i'm missing here?
i have tried 2 methods, post code both...
method 1:
--- relay.h ---
--- relay.cpp ---
this failes compile following error:
/home/bob/arduino/arduino-0018/libraries/atmeganet/relay.cpp: in constructor 'relay::relay(int, int, int)':
/home/bob/arduino/arduino-0018/libraries/atmeganet/relay.cpp:5: error: no matching function call 'newsoftserial::newsoftserial()'
/home/bob/arduino/arduino-0018/libraries/newsoftserial/newsoftserial.h:82: note: candidates are: newsoftserial::newsoftserial(uint8_t, uint8_t, bool)
/home/bob/arduino/arduino-0018/libraries/newsoftserial/newsoftserial.h:45: note: newsoftserial::newsoftserial(const newsoftserial&)
/home/bob/arduino/arduino-0018/libraries/atmeganet/relay.cpp:7: error: no match 'operator=' in '((relay*)this)->relay::_ss = (((newsoftserial*)operator new(18u)), (<anonymous>->newsoftserial::newsoftserial(((uint8_t)dpin_rx), ((uint8_t)dpin_tx), false), <anonymous>))'
/home/bob/arduino/arduino-0018/libraries/newsoftserial/newsoftserial.h:45: note: candidates are: newsoftserial& newsoftserial::operator=(const newsoftserial&)
it looks trying call newsoftserial constructor without passing in args, because i've defined in .h , gets instantiated @ same time relay is? how go declaring property correctly?
method 2:
i have tried using pointer compiles seems crash arduino , cause print out junk , reset.
--- relay.h ---
--- relay.cpp ---
i guess instance created gets destroyed after constructor exits leaving pointer pointing @ invalid object?
as say, theres something, quite simple i'm missing here... point me in right direction please?
i have tried searching cant seem find combination of keywords!
many in advance
i'm having trouble creating , storing instance of newsoftserial class inside own relay class.
i'm experienced in java , php, haven't done lot in c++. i think i've grasped pointers, there must quite fundamental i'm missing here?
i have tried 2 methods, post code both...
method 1:
--- relay.h ---
code: [select]
#ifndef atmeganet_relay
#define atmeganet_relay
#include "wconstants.h"
#include "module.h"
#include <newsoftserial.h>
class relay : public module{
public:
relay(int dpin_tx,int dpin_rx,int timeout);
void loop();
void poll();
private:
int _timeout;
newsoftserial _ss;
};
#endif--- relay.cpp ---
code: [select]
#include "wprogram.h"
#include "relay.h"
#include <newsoftserial.h>
relay::relay(int dpin_rx,int dpin_tx,int timeout) : module("relay"){
_timeout=timeout;
_ss=new newsoftserial(dpin_rx,dpin_tx);
_ss.begin(9600);
}
void relay::loop(){
}
void relay::poll(){
_ss.println("p");
int timeout=millis()+_timeout;
while(millis()<timeout){
if(_ss.available()>0){
char c=_ss.read();
if(c=='#'){
return;
}
serial.print(c);
}
}
serial.println("!relay timeout");
}
this failes compile following error:
/home/bob/arduino/arduino-0018/libraries/atmeganet/relay.cpp: in constructor 'relay::relay(int, int, int)':
/home/bob/arduino/arduino-0018/libraries/atmeganet/relay.cpp:5: error: no matching function call 'newsoftserial::newsoftserial()'
/home/bob/arduino/arduino-0018/libraries/newsoftserial/newsoftserial.h:82: note: candidates are: newsoftserial::newsoftserial(uint8_t, uint8_t, bool)
/home/bob/arduino/arduino-0018/libraries/newsoftserial/newsoftserial.h:45: note: newsoftserial::newsoftserial(const newsoftserial&)
/home/bob/arduino/arduino-0018/libraries/atmeganet/relay.cpp:7: error: no match 'operator=' in '((relay*)this)->relay::_ss = (((newsoftserial*)operator new(18u)), (<anonymous>->newsoftserial::newsoftserial(((uint8_t)dpin_rx), ((uint8_t)dpin_tx), false), <anonymous>))'
/home/bob/arduino/arduino-0018/libraries/newsoftserial/newsoftserial.h:45: note: candidates are: newsoftserial& newsoftserial::operator=(const newsoftserial&)
it looks trying call newsoftserial constructor without passing in args, because i've defined in .h , gets instantiated @ same time relay is? how go declaring property correctly?
method 2:
i have tried using pointer compiles seems crash arduino , cause print out junk , reset.
--- relay.h ---
code: [select]
#ifndef atmeganet_relay
#define atmeganet_relay
#include "wconstants.h"
#include "module.h"
#include <newsoftserial.h>
class relay : public module{
public:
relay(int dpin_tx,int dpin_rx,int timeout);
void loop();
void poll();
private:
int _timeout;
newsoftserial *_ss;
};
#endif--- relay.cpp ---
code: [select]
#include "wprogram.h"
#include "relay.h"
#include <newsoftserial.h>
relay::relay(int dpin_rx,int dpin_tx,int timeout) : module("relay"){
_timeout=timeout;
newsoftserial ss(dpin_rx,dpin_tx);
_ss=&ss;
_ss->begin(9600);
}
void relay::loop(){
}
void relay::poll(){
_ss->println("p");
int timeout=millis()+_timeout;
while(millis()<timeout){
if(_ss->available()>0){
char c=_ss->read();
if(c=='#'){
return;
}
serial.print(c);
}
}
serial.println("!relay timeout");
}
i guess instance created gets destroyed after constructor exits leaving pointer pointing @ invalid object?
as say, theres something, quite simple i'm missing here... point me in right direction please?
i have tried searching cant seem find combination of keywords!
many in advance

there issue arduino ide in order of constructor calls not defined or guaranteed.
you need quit trying construct other things in constructor.
you need add begin method class, similar serial.begin(), instance, put code have in constructor.
the new operator not defined use classes, can't use pointer notation.
you need quit trying construct other things in constructor.
you need add begin method class, similar serial.begin(), instance, put code have in constructor.
the new operator not defined use classes, can't use pointer notation.
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Trying to create and store an instance of a class
arduino
Comments
Post a Comment