Need Syntax Help: Return Pointer to Struct from Class
i wish return pointer structure function within class, can't seem find right syntax/signature compile. i've searched these forums, , googled it, , way typing looks correct, , i've experimented place *'s , compile errors no matter what.
i made simple test project try , figure out, have posted below. have class, wrapper structure. structure private member of class. have function gettmptr() supposed return pointer private structure. yes, realize simpler make structure public. yes, realize i'm trying bit screwball , not programming practice. please don't preach @ me that, i've been programming in other languages 30 years. 'real' project bigger testbed posted below, many classes, many structures, , many complex functions. purpose returning pointer structure rather making public force level of abstraction. oh yes, , know standard time.h library.
the line @ issue near end of datewiz.cpp file.
structreturntest.ino
datewiz.h
datewiz.cpp
thanks in advance help!
i made simple test project try , figure out, have posted below. have class, wrapper structure. structure private member of class. have function gettmptr() supposed return pointer private structure. yes, realize simpler make structure public. yes, realize i'm trying bit screwball , not programming practice. please don't preach @ me that, i've been programming in other languages 30 years. 'real' project bigger testbed posted below, many classes, many structures, , many complex functions. purpose returning pointer structure rather making public force level of abstraction. oh yes, , know standard time.h library.
the line @ issue near end of datewiz.cpp file.
structreturntest.ino
code: [select]
////////////////////////////////////////////////////////////////////////
// * * * * structure return test * * * *
// file: structreturntest.ino
// (u) uncopyright 2014 dr. wizard, except noted.
///////////////////////////////////////////////////////
#include <stdint.h>
#include "datewiz.h"
class date_d date1(22,6,1963);
struct tmd* tmp;
struct tmd tmc;
void setup() {
serial.begin(9600);
tmp = date1.gettmptr();
tmc = date1.gettmcopy();
date1.setmonth(8);
serial.println(tmp->tm_month);
serial.println(tmc.tm_month);
} // end setup()
void loop() {
} // end loop()
datewiz.h
code: [select]
////////////////////////////////////////////////////////////////////////
// * * * * structure return test * * * *
// file: datewiz.h
// (u) uncopyright 2014 dr. wizard, except noted.
///////////////////////////////////////////////////////
#include <stdint.h>
#ifndef _datewiz_h
#define _datewiz_h
// structures //
struct tmd {
uint8_t tm_mday; // day of month 1-31
uint16_t tm_year; // years 1900-2100
uint8_t tm_month; // month number 1-12
};
// classes //
// date_d class //
class date_d {
public:
// constructors, destructor
date_d();
date_d(uint8_t dayofmonth, uint8_t month, uint16_t years);
// member functions
uint8_t getdayofmonth();
uint8_t getmonth();
uint16_t getyear();
// member set functions
void setdayofmonth(uint8_t days);
void setmonth(uint8_t months);
void setyear(uint16_t years);
// other functions
struct tmd gettmcopy();
struct tmd* gettmptr();
private:
struct tmd _tmd;
}; // end date_d class definition
#endif
datewiz.cpp
code: [select]
////////////////////////////////////////////////////////////////////////
// * * * * structure return test * * * *
// file: datewiz.cpp
// (u) uncopyright 2014 dr. wizard, except noted.
///////////////////////////////////////////////////////
#include <stdint.h>
#include "datewiz.h"
// date_d constructors, destructor //
date_d::date_d() {
// constructor no parameters defaults unix epoch - jan 1st, 1970
_tmd.tm_mday = 1; // day of month 1-31
_tmd.tm_year = 1970; // years since 1900 0-255
_tmd.tm_month = 1; // month number 1-12
} // end constructor date_d()
date_d::date_d(uint8_t days, uint8_t months, uint16_t years) {
_tmd.tm_mday = days; // day of month 1-31
_tmd.tm_month = months; // month number 1-12
_tmd.tm_year = years; // full year number 0-? (such 2014)
} // end constructor date_d(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t days, uint8_t months, uint16_t years)
// date_d member functions //
uint8_t date_d::getdayofmonth() {
return _tmd.tm_mday;
} // end getdayofmonth
uint8_t date_d::getmonth() {
return _tmd.tm_month;
} // end getmonth
uint16_t date_d::getyear() {
return _tmd.tm_year;
} // end getyear
// date_d member set functions //
void date_d::setdayofmonth(uint8_t days) {
_tmd.tm_mday = days;
} // end setdayofmonth
void date_d::setmonth(uint8_t months) {
_tmd.tm_month = months;
} // end setmonth
void date_d::setyear(uint16_t years) {
_tmd.tm_year = years;
} // end setyear
// date_d other functions //
struct tmd date_d::gettmcopy() {
return _tmd;
} // end gettmcopy
struct tmd* date_d::gettmptr() {
return _tmd*; // error: expected primary-expression before ';' token
} // end gettmptr
thanks in advance help!
code: [select]
class date_d date1(22,6,1963);
that's pretty skimpy definition of class. not how create instance of class.
quote
the structure private member of class. have function gettmptr() supposed return pointer private structure.
code: [select]
return _tmd*; // error: expected primary-expression before ';' token
not surprising. try
return &_tmd;
even better have member pointer, , make pointer point data, , return pointer.
but, best not try return pointer struct. have 3 methods return data. way, can ditch struct altogether, , functions still work.
of course, caller able understand struct, struct definition shouldn't hidden in class.
Arduino Forum > Using Arduino > Programming Questions > Need Syntax Help: Return Pointer to Struct from Class
arduino
Comments
Post a Comment