Problem with Class Constructor trying to emulate YUN Console.print

I am trying to port a Sketch I wrote for the Arduino using the YUN Shield. I used the Console.h and Console.print class in a lot of places for debugging. I thought I would create a class that wraps the Particle Publish but look like the Console class.

I am getting an error that I don’t understand which is:
" ISO C++ forbids the declaration of ‘Console’ with no type."

My Console.cpp looks like this:

#ifndef  Console_h
#define  Console_h
#define  EVENTNAME "Tracker 1"
#include <stdlib.h>
#include <Particle.h>

class PublishConsole {
   public:
      Console();
      void print(int i);
      void print(double  f);
      void print(char* c);
      void print(String* c);
      void println(int i);
      void println(long i);
      void println(double  f);
      void println(char* c);
      void print(unsigned long i);
};
#endif
#include <Console.h>

void PublishConsole::Console(void)
{}

void PublishConsole::print(int i) {
  char data[33];
  itoa(i,data,10);
  Particle.publish(EVENTNAME,data, 60, PRIVATE);
}

void PublishConsole::print(double  f) {
  String data = String(f);
  Particle.publish(EVENTNAME,data, 60, PRIVATE);
}

void PublishConsole::print(char* c) {
  Particle.publish(EVENTNAME,c, 60, PRIVATE);
}
void PublishConsole::print(String* c) {
  Particle.publish(EVENTNAME,c, 60, PRIVATE);
}
void PublishConsole::println(int i) {
  char data[33];
  itoa(i,data,10);
  Particle.publish(EVENTNAME,data, 60, PRIVATE);
}
void PublishConsole::println(long i) {
  char data[33];
  itoa(i,data,10);
  Particle.publish(EVENTNAME,data, 60, PRIVATE);
}
void PublishConsole::print(unsigned long i) {
  char data[33];
  itoa(i,data,10);
  Particle.publish(EVENTNAME,data, 60, PRIVATE);
}

void PublishConsole::println(double  f) {
  String data = String(f);
  Particle.publish(EVENTNAME,data, 60, PRIVATE);
}

void PublishConsole::println(char* c) {
  Particle.publish(EVENTNAME,c, 60, PRIVATE);
}

The implementation of your constructor should not have a type in front of it.

PublishConsole::Console() {}

The error you're getting is not the same one I get if I do what you did, but I think this is the problem. However, I think that you don't need the constructor at all since your implementation does nothing. In C++, a default constructor is provided automatically if you don't create any constructors yourself.

That is not even a constructor.
Constructors have to have the same name as the class, so it should be

  PublishConsole::PublishConsole() { }
1 Like

Oh duh! I guess that explains why I didn't get the same error when I tested (I put a void in front of a proper constructor).

Thanks that was helpful. I looked at a lot of example but nothing about that was mentioned.

What are the forms of Particle.publish? Now I am getting an error:

no matching function call to CloudPublish::publish(String&, String*&, int, const PublishFlag in the class function :

void PublishConsole::print(String* c) {
  Particle.publish(EventName,c, 60, PRIVATE);
}

The way you have it written, you should pass in a String object or a char* as the event data, but not a pointer to a String as you’re doing (a String*). You could pass in a pointer to a String to your PublishConsole::print function if you need to, but you would have to dereference that pointer in the Particle.publish statement (Particle.publish(EventName, *c, 60, PRIVATE))

@ispybadguys, do remember that Particle.publish()'s are limited to one per second. You can’t feed it at the same rate you would a console.

Thanks Ric.

I do sometimes make some basic C mistakes. I f you don’t mind I have one more C error I don’t understand.

I have the following code in a header called Externals.h.


typedef struct sunposition {float Declination; float Elevation; float Azimuth; float HourAngle;};
extern sunposition Sun;

And then in a file called SunLoacation.h

include <math.h>
#include <Externals.h>
#include <Defines.h>
#include <Particle.h>
#include <functionPrototypes.h>
#include <Console.h>


struct sunposition Sun;

I get this error:
redefinition of ‘struct sunposition’
…Externals.h 27:16
previous definition of ‘struct sunposition’
…Externals.h 27:16

What I have read is that this is the correct way to define a global structure. I think I would like to really put this into a Class but I wanted to understand this error.

Thanks

Kurt

The typedef does define sunposition as struct already, so don’t use struct again.
The way you do it there would virtually expand to something like that

struct struct sunposition { ... } Sun;

Which isn’t possible, so it’s interpreted as redefinition of an existing symbol.

Just write

sunposition Sun;

Thanks ScruffR for pointing this out.

I changed the line you mentioned but I guess the complier hadn’t gotten around to revealing that error to me yet.

I must apologize to the readers for posting something that is not very interesting. I fixed the problem with this code. I haven’t found where it was being included twice but it is obvious it is somewhere.

#ifndef Externals_H
#define Externals_H