[solved] error: return type specification for constructor invalid

Hay everyone,

I am prety new to programming with c++. Expesialy with classes. I am writing my first library. But if I want to compile it I get this message:

error: return type specification for constructor invalid

I've already searched the internet about that specific error, but the only thing I am getting is that it could be a missing semi-colon. I've already checked that. This is the code:
S88.h

#ifndef S88_h
#define S88_h

#include "Arduino.h"

class S88
{
  public:
    S88(int load, int clock, int data, int reset);
    void S88Routine();
    int S88Modules[32];
  private:
    byte _readS88Module();
    void _loadS88();
    void _S88Low();
    void _S88Reset();
    void _S88Clock();
    int _load;
    int _clock;
    int _data;
    int _reset;
};

#endif

s88.cpp

#include "Arduino.h"
#include "s88.h"

S88::S88(int load, int clock, int data, int reset)
{
  pinMode(_load, OUTPUT);
  pinMode(_clock, OUTPUT);
  pinMode(_reset, OUTPUT);
  pinMode(_data, INPUT);
  this->_load = load;
  this->_clock = clock;
  this->_data = data;
  this->_reset = reset;
  for(int x=0; x<32; x++)
  {
    S88Modules[x] = B00000000;
  }
}

void S88::S88Routine()
{
  _loadS88();
  for(int y=0; y<32; y++)
  {
    S88Modules[y] = _readS88Module();
  }
}

byte S88::_readS88Module()
{
  _S88Low();
  byte result = B00000000;
  for (int x=1; x <= 8; x++)
  {
    _S88Clock();
    delayMicroseconds(100);
    int readedValue = digitalRead(_data);
    delayMicroseconds(50);
    if(readedValue == HIGH)
    {
      bitSet(result, x);
    } else
    {
      bitClear(result, x);
    }
  }
  return result;
}

void S88::_loadS88()
{
  _S88Low();
  digitalWrite(_load, HIGH);
  delayMicroseconds(150);
  _S88Clock();
  delayMicroseconds(150);
  _S88Reset();
  delayMicroseconds(150);
  digitalWrite(_load, LOW);
  delayMicroseconds(150);
}

void S88::_S88Reset()
{
  //functie om S88 Reset puls te geven
  digitalWrite(_reset, HIGH);
  delayMicroseconds(150);
  digitalWrite(_reset, LOW);
}

void S88::_S88Clock()
{
  //functie om S88 Clock puls te geven
  digitalWrite(_clock, HIGH);
  delayMicroseconds(150);
  digitalWrite(_clock, LOW);
}

void S88::_S88Low()
{
  //Functie om alle uitgangen laag te zetten
  digitalWrite(_clock, LOW);
  digitalWrite(_load, LOW);
  digitalWrite(_reset, LOW);
}

I hope someone can help me out. Already a lot of thanks!

Erik

Hi Erik

Could you post an example program that gives the compilation error when calling the library?

And does it help if you change to this?

S88::S88(int load, int clock, int data, int reset): _load(load), _clock(clock), _data(data), _reset(reset)
{
  pinMode(_load, OUTPUT);
  pinMode(_clock, OUTPUT);
  pinMode(_reset, OUTPUT);
  pinMode(_data, INPUT);
  for(int x=0; x<32; x++)
  {
    S88Modules[x] = B00000000;
  }
}

Regards

Ray

#include <S88.h>

S88 s88(A2, A1, A0, A3);

void setup() 
{
  Serial.begin(9600);
  Serial.println("S88-Arduino V:02 ");
  delay(250); 
}

void loop() 
{
  s88.S88Routine();
  Serial.println("---------------------------------");
  for(int z=0; z<32; z++)
  {
    Serial.println(s88.S88Modules[z]);
  }
}

Hi Erik

I just copied your .h and .cpp files (without my suggested change) and installed them into the Arduino IDE - I'm using 1.0.5 on Windows.

Then I tried compiling the example program you posted. I did not get any errors.

How are you installing your library files?

Regards

Ray

I've placed the .h and .cpp file in a folder called S88Library in the libraries folder. After that did I import the library in the IDE. I am also using Arduino 1.0.5 on windows 8.1.

Erik

Could you please copy and paste all the messages you get when you compile?

Does the directory name have to match the class name ? Or the .h file name ? Or both.

I can't see anything wrong with your code. There error message sounds like it is telling you
that constructors don't have a return statement, but you don't have one.

According to your first post, the header file is S88.h and the #include in your sketch is for s88.h
But I don't think it is case-sensitive ? You should check that there are not two .h files there. You may
be editing one, but the compiler is picking up a different one.

Does the bitSet and bitClear functions use 0 to 7 or 1 to 8 ?

You are assigning byte values to ints but that should not be an error.

S88::S88(int load, int clock, int data, int reset)
{
  pinMode(_load, OUTPUT);
  pinMode(_clock, OUTPUT);
  pinMode(_reset, OUTPUT);
  pinMode(_data, INPUT);
  this->_load = load;
  this->_clock = clock;
  this->_data = data;
  this->_reset = reset;

In your constructor here, you appear to be using the _load variables before you assign values to them. It seems to me, that should be the other way around, like

S88::S88(int load, int clock, int data, int reset)
{
  this->_load = load;
  this->_clock = clock;
  this->_data = data;
  this->_reset = reset;
  pinMode(_load, OUTPUT);
  pinMode(_clock, OUTPUT);
  pinMode(_reset, OUTPUT);
  pinMode(_data, INPUT);

But I would be surprised if you got a compiler error for that, because the compiler probably doesn't care what value it uses.

Make sure there is nothing on the line before this line in the cpp file.

Make sure there are no invisible characters on the preceding line, which the compiler might be reading as a return type for this function.

Put something else there, like define a global variable of something. If the end of the preceding #include file is defective in some way, that could cause something extraneous to be parsed as a putative return type specifier for the constructor, which would be invalid.

S88::S88(int load, int clock, int data, int reset)

This is the whole error I am getting:

In file included from S88_center.ino:1:
C:\Users\Erik\Documents\Arduino\libraries\S88Library/S88.h:9: error: return type specification for constructor invalid

I've alse changed the constructer to Hackscribbles example but that did'nt work.

michinyon:
Does the bitSet and bitClear functions use 0 to 7 or 1 to 8 ?

This function was working before I placed evererything in a class. But in the Reference it is telling that the right most bit is 0.

Erik

As Hackscribble said, the code you posted worked.

Post the code you used to get the error.

I have attached your project in one file, see if it compiles for you.

s88.ino (2.08 KB)

The file you've post is compiling.
I've post my files in the zip file below.

s88.zip (1.43 KB)

void S88(int load, int clock, int data, int reset);

Just like the error says, you have a return type on the constructor, which is invalid.

EDIT:

Next errors:

#include "s88.h"

Needs return types on the definitions.

<?php  //don't mind this

 void _loadS88();
 void _S88Reset();
 void _S88Clock();
void _S88Low();

//... Needs return types on the definitions.

S88::_loadS88(){}
S88::_S88Reset(){}
S88::S88Clock(){}
S88::_S88Low(){}

I've already changed all those errors. But it seemed that arduino saved the eddited files in the project folder and not the library folder. I've copied the files I eddited and it is now compiling.

You are setting the mode for pins in your constructor. Your constructor is called before the init() function has been called, to set up the hardware. Your pinMode() calls are, therefore, useless.

You should never diddle with the hardware in your constructor.

Add a begin() method, similar to Serial.begin(), where you put the hardware-diddling code. Call the begin() method for your instance in setup().

Thanks for the tip!
I've implemented it.

Erik