0

This seems to be a very common error, I am struggling to understand what exactly the solution to this is. I am attaching everything, library files, code, and errors. If you have a moment, I need help with these two errors which appear to be caused by the same problem. Fixing these errors may however not be the end of my problems so if you notice something else please point it out!! I recognize my own incompetence regarding this topic any help will be appreciated.

This is a Morse Code Encoder, my goal is to transform the original functional code into a library so that I can insert it into another code more easily.

The Following is the Morse.h Library File (I created this with the Arduino Library Tutorial)

/* Morse.h - Library for flashing Morse code.
Created by Young on Sept. 1, 2016 */

#ifndef Morse_h
#define Morse_h

#include "Arduino.h"

class Morse
{
public:
Morse(int LED, int BEEP, int delayTime, char tmpChar);

void MorseDot();
void MorseDash();
void LightsOff(int delayTime);
void GetChar(char tmpChar);

private:
int _LED;
int _BEEP;
int _delayTime;
char _tmpChar;
int _note;
int _dotLen;
int _dashLen;
int _elemPause;
int _Spaces;
int _wordPause;
};

#endif

The following is the Morse.cpp Library (I created this with the Arduino Library Tutorial)

/* Morse.cpp - Library for flashing Morse code.
Created by Young on Sept. 1, 2016 */

#include "Arduino.h"
#include "Morse.h"

Morse::Morse(int LED);
{
pinMode(LED, OUTPUT);
_LED = LED;
}
Morse::Morse(int BEEP);
{
pinMode(BEEP, OUTPUT);
_BEEP = BEEP;
}

_note = 587;

_dotLen = 75;

_dashLen = dotLen * 7;

_elemPause = dotLen * 4;

_Spaces = dotLen * 10;

_wordPause = dotLen * 14;

void MorseDot();
void MorseDash();
void LightsOff(int delayTime);
void GetChar(char tmpChar);


void Morse::MorseDot()
{
digitalWrite(led13, HIGH);    // turn the LED on
tone(BEEP, note, dotLen);     // start playing a tone
delay(dotLen);                // hold in this position
}

void Morse::MorseDash()
{
digitalWrite(led13, HIGH);    // turn the LED on
tone(BEEP, note, dashLen);    // start playing a tone
delay(dashLen);               // hold in this position
}

void Morse::LightsOff(int delayTime)
{
digitalWrite(led13, LOW);     // turn the LED off
digitalWrite(BEEP, LOW);      // Silence the Beep
delay(delayTime);             // hold in this position
}

digitalWrite(led13, LOW);     // turn the LED off
digitalWrite(BEEP, LOW);      // Silence the Beep
delay(delayTime);             // hold in this position

void Morse::GetChar(char tmpChar)
{
// Take the passed character and use a switch to find the morse code for that character
switch (tmpChar) {
    case 'a':
        MorseDot();
        LightsOff(elemPause);
        MorseDash();
        LightsOff(elemPause);
        break;
    case 'b':
        MorseDash();
        LightsOff(elemPause);
        MorseDot();
        LightsOff(elemPause);
        MorseDot();
        LightsOff(elemPause);
        MorseDot();
        LightsOff(elemPause);
        break;
    case 'c':
        MorseDash();
        LightsOff(elemPause);
        MorseDot();
        LightsOff(elemPause);
        MorseDash();
        LightsOff(elemPause);
        MorseDot();
        LightsOff(elemPause);
        break;
//This continues through z and then 0-9, I have shortened it hear for ease of 
//reading.  Each break statement functions fine in the original code.

The following is the test code

#include <Morse.h>

int delayTime;
int tmpChar;

Morse dot();
Morse dash();
Morse dark(int delayTime); 
Morse set(char tmpChar);

char stringToMorseCode[] = "String to be converted to morse code";

// Create variable to define the output pins
int LED = 13;      // blink an led on output 12
int BEEP = 14;      // output audio on pin 8
int i;

void setup() 
{

}

void loop() 
{ 
// Loop through the string and get each character one at a time until the end is reached
for (int i = 0; i < sizeof(stringToMorseCode) - 1; i++);
{
// Get the character in the current position
char tmpChar = stringToMorseCode[i];
// Set the case to lower case
tmpChar = toLowerCase(tmpChar);
// Call the subroutine to get the morse code equivalent for this character
set.GetChar(tmpChar);
}

// At the end of the string long pledause before looping and starting again

dark.LightsOff(8000);            // hold in this position     

}

These are the errors I get when I run this code.

/Users/Young1/Documents/Arduino/Morse_Library_Test/Morse_Library_Test.ino: In function 'void loop()':
Morse_Library_Test:31: error: request for member 'GetChar' in 'set', which is of non-class type 'Morse()'
set.GetChar(tmpChar);
    ^
Morse_Library_Test:36: error: request for member 'LightsOff' in 'dark', which is of non-class type 'Morse()'
dark.LightsOff(delayTime);            // hold in this position     
     ^
exit status 1
request for member 'GetChar' in 'set', which is of non-class type 'Morse()'

This is an exercise for me in improving my own skills. I have spent a day since my last issue (trying to call a class function without an object) researching my problems, however I have been unable to relate the variety of similar questions and answers to my own code. It seems that most of them deal with a misplaced (). If you find something that is wrong, please assume that I know nothing. Post as though you are trying to teach a child how to use Arduino.

I am adding the original code which I am trying to transform into a library for the purpose of inserting it into another code without inserting all 453 lines.

/*
Morse Code Project

This code will loop through a string of characters and convert these to morse code.  
It will blink two LED lights and play audio on a speaker.  
*/

char stringToMorseCode[] = "String to convert to Morse";

// Create variable to define the output pins
int led13 = 1;      // blink an led on output 12
//int led6 = 0;        // blink an led on output 6
int BEEP = 0;      // output audio on pin 8
int note = 587;      // music note/pitch

/*
Set the speed of your morse code
Adjust the 'dotlen' length to speed up or slow down your morse code
(all of the other lengths are based on the dotlen)

Here are the ratios code elements:
Dash length = Dot length x 3
Pause between elements = Dot length
(pause between dots and dashes within the character)
Pause between characters = Dot length x 3
Pause between words = Dot length x 7
*/
int dotLen = 75;     // length of the morse code 'dot'
int dashLen = dotLen * 7;    // length of the morse code 'dash'
int elemPause = dotLen * 4;  // length of the pause between elements of a character
int Spaces = dotLen * 10;     // length of the spaces between characters
int wordPause = dotLen * 14;  // length of the pause between words

// the setup routine runs once when you press reset:
void setup() {                
// initialize the digital pin as an output for LED lights.
pinMode(led13, OUTPUT); 
pinMode(BEEP, OUTPUT);

}

// Create a loop of the letters/words you want to output in morse code (defined in string at top of code)
void loop()
{ 
// Loop through the string and get each character one at a time until the end is reached
for (int i = 0; i < sizeof(stringToMorseCode) - 1; i++)
{
// Get the character in the current position
char tmpChar = stringToMorseCode[i];
// Set the case to lower case
tmpChar = toLowerCase(tmpChar);
// Call the subroutine to get the morse code equivalent for this character
GetChar(tmpChar);
}

// At the end of the string long pause before looping and starting again
LightsOff(8000);      
}

// DOT
void MorseDot()
{
digitalWrite(led13, HIGH);    // turn the LED on
tone(BEEP, note, dotLen);   // start playing a tone
delay(dotLen);              // hold in this position
}

// DASH
void MorseDash()
{
digitalWrite(led13, HIGH);    // turn the LED on 
tone(BEEP, note, dashLen);   // start playing a tone
delay(dashLen);               // hold in this position
}

// Turn Off
void LightsOff(int delayTime)
{
digitalWrite(led13, LOW);     // turn the LED off
digitalWrite(BEEP, LOW);      // Silence the Beep
delay(delayTime);             // hold in this position

}

// *** Characters to Morse Code Conversion *** //
void GetChar(char tmpChar)
{
// Take the passed character and use a switch case to find the morse code for that character
switch (tmpChar) {
case 'a': 
MorseDot();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
break;
case 'b':
MorseDash();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
break;

//This continues through z and then 0-9, but it adds a few hundred lines we 
//don't need to read here.
  • The problem lies with your object instantiation. I will explain shortly - I am eating right now. – Majenko Sep 7 '16 at 21:49
  • That's fine, I am not in a rush, after all I can't figure it out on my own!! – user26383 Sep 7 '16 at 22:35
0

The main problem lies with your object instatiation:

int delayTime;
int tmpChar;

Morse dot();
Morse dash();
Morse dark(int delayTime); 
Morse set(char tmpChar);

Morse has a constructor format thus:

Morse(int LED, int BEEP, int delayTime, char tmpChar);

When you instantiate an object from a class and call the constructor you have to pass all the parameters to the constructor (unless they have been given a default value, in which case you can leave them out from the right-most only).

You never provide () if there are no parameters needed for the constructor - this is a little idiom of C++ that I have never understood. To break it down:

Morse dot();             
Morse dash();           

These have no parameters being passed. Lose the () if that is intentional (and you have a matching constructor with no parameters) or add the four parameters for the constructor.

Morse dark(int delayTime); 
Morse set(char tmpChar);

In these two you are trying to pass one parameter only, and for some reason you are also trying to give it a type at the same time. I can only think that it may be to try and indicate which parameter you mean. That is not right. You must provide all the parameters. Also you are passing values from variables that haven't been given a value yet, so it is actually completely meaningless.

I am not entirely sure what you are hoping to achieve with those last two there...

A further issue, digging into your actual class, is these odd constructors:

Morse::Morse(int LED);
{
pinMode(LED, OUTPUT);
_LED = LED;
}
Morse::Morse(int BEEP);
{
pinMode(BEEP, OUTPUT);
_BEEP = BEEP;
}

They have no matching prototype in the class header, and even if they did, the compiler will have no way of differentiating between them. They both have the same type (int). Overloading operates on the type, not the name of the variable.

Oh, and the one constructor you do have a prototype for just plain doesn't exist in your code.

  • I have no idea what I am doing, I have just tried to compare this to the Library tutorial, and well, my ignorance is obvious, thank you for the help. If you don't mind, could you explain to me how to use parameters? I understand I can pass a value, but not much more than that. I will upload the original code, but essentially, it takes a string, assigns a temporary character to each value incrementally, then calls each switch break command that corresponds with the character in the string thus outputting an encoded morse string which represents the original string. – user26383 Sep 7 '16 at 22:43
  • Wish I had stumbled upon this yesterday!! – user26383 Sep 7 '16 at 22:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.