Trying to get a simple object to work


i'm trying object working. class called message, , supposed object hold data: 1 byte called flag , variable length array of bytes called data (the length different each object).

code:
message.h:
code: [select]
#ifndef morse_h
#define morse_h
#include "message.h"
class message
{
  public:
    message(byte flag, byte[] data);
  private:
    byte _flag;
    byte _data[];
};
#endif

message.cpp:
code: [select]
#include "arduino.h"
#include "message.h"

message::message(byte flag, byte[] data)
{
  _flag = flag;
  _data = data;
}


main file:
code: [select]

#include <message.h>

void setup() {
  serial.begin(9600);
 
  byte b[3] = {1, 2, 3};
 
  message msg(0, b);
}

void loop() {
 
}


i'm getting bunch of compiler errors, , don't understand single 1 of them. seems c++ doesn't unknown length array... , stuff "byte doesn't name type"?

errors:
code: [select]

arduino: 1.6.3 (windows 7), board: "arduino uno"

in file included sketch_apr20a.ino:1:0:

d:\install\programming\arduino\libraries\message/message.h:7:18: error: expected ')' before 'flag'

     message(byte flag, byte[] data);

                  ^

d:\install\programming\arduino\libraries\message/message.h:9:5: error: 'byte' not name type

     byte _flag;

     ^

d:\install\programming\arduino\libraries\message/message.h:10:5: error: 'byte' not name type

     byte _data[];

     ^

sketch_apr20a.ino: in function 'void setup()':

sketch_apr20a.ino:8:19: error: no matching function call 'message::message(int, byte [3])'

sketch_apr20a.ino:8:19: note: candidates are:

in file included sketch_apr20a.ino:1:0:

d:\install\programming\arduino\libraries\message/message.h:4:7: note: message::message()

 class message

       ^

d:\install\programming\arduino\libraries\message/message.h:4:7: note:   candidate expects 0 arguments, 2 provided

d:\install\programming\arduino\libraries\message/message.h:4:7: note: message::message(const message&)

d:\install\programming\arduino\libraries\message/message.h:4:7: note:   candidate expects 1 argument, 2 provided

error compiling.


i'm coming java & lua, i'm confused how arrays work in c++.

you need include arduino.h  start - automatically included in sketch iirc


Arduino Forum > Using Arduino > Programming Questions > Trying to get a simple object to work


arduino

Comments