Issue compiling library without constructor argument .. Confused

I am just learning and have started with simple examples, well mostly. The problem had me hunting in my first attempt at a library for literally hours without any progress.
I had taken a working sketch and transplanted it into a library using the Morse example as a template and it just wouldn’t compile …
I eventually replicated the error in the simple Morse class :-

test1:7: error: request for member ‘dot’ in ‘morse’, which is of non-class type ‘Morse ()()’ <<
This is repeated for all functions when they are referenced even though the compiler is happy with the include and init.

This is a snipet of my sketch followed by the top of the .h and .cpp files.
With the commented ‘A’ lines are in play all is well, while ‘B’'s give me the error :-

#include <Morse.h>
//Morse morse(13);         //A
//Morse morse();           //B
void setup(){}
void loop(){;; ......

The slightly modified Morse.h :-

class Morse
     // Morse(int pin);  // A
     //Morse();          // B

And the Morse.cpp

// Morse::Morse(int pin)        // A
// Morse::Morse()               // B
  // _pin = pin;                // A
  // _pin = 13;                 // B 
  pinMode(pin, OUTPUT);

I have played around with all sorts of configurations and even naming conventions, in the process generating all sorts of errors due to omission, mismatch and even poor construct. All could be traced to mistakes and generally were identified within verbose compilation comments.

However no matter what I do I cant get a library, any library, to work if the construct is devoid of arguments and I cant find a reason.
The error is always the same … request for member X in Y, which is of non-class type Y ()().
I assume this means that Y is not being recognised as a class when its constructor is Y() as opposed to Y(a)
Of course I could simply accept it and always put in a dummy argument, just to satisfy the construct constraint, but then I wouldn’t be learning.

I would very much appreciate any insight … Even if it just to point out how silly I am being.


The problem is here:

  _pin = 13;
  pinMode(pin, OUTPUT);

The pin variable isn't declared (as it was the argument), so either replace it with _pin, or simply with 13 (you don't need that additional, useless variable).

Edit: actually, the rest of the code may need that variable _pin, so don't remove it.

I appreciate the input but that isn't it .... pin is only used when it is declared as part of the Morse(int pin) construct. _pin is declared as 'private int' in the Morse.h file

The problem is more subtle than that ... any constructor I try like this :-

ConName::ConName(int iDummy) { }
ConName::SomemFunc(with or without args){}

will work just fine, assuming it matches in the header source and sketch. However this :-

ConName::ConName() { }
ConName::SomemFunc(with or without args){}

bombs out every time, with the same error, as soon as a function is referenced.

I have to be missing something stupid but it isn't bad variable scope or naming ... The error imply s that ConName isn't a class and cant therefore have a member function, the one that was just called!

I see...I have downloaded the Morse library and took a look.

In the example they are doing:

Morse morse(9, 12, 1);

With your modifications, that would be:

Morse morse;

Unless I'm wrong of course, can't test... but at least it compile :)

guix is correct. This line:

Morse morse();           //B

Tries to call a function, not instantiate a class.

Basically you don't call constructors. The compiler does that when you make an instance.

Not sure what you mean by (9,12,1) perhaps its a diferent example, here is the tiny one I am playing with.

all I need to do to se the error is arrange the constructor not to have an argument, nothing else matters.
well apart from all three file matching of course.

the actual functionality of the sketch is immaterial, even with all the functions empty so they do nothing the error still occurs if you reference one. (875 Bytes)

Yes but in your sketch, replace:

Morse morse();


Morse morse;

I have downloaded an updated version of that library here:

Dyslexicbloke: all I need to do to se the error is arrange the constructor not to have an argument, nothing else matters.

If the constructor does not take an argument, do not use brackets when making an instance. That's the rule.

You STARS .... Thanks a bunch. Just tried it and it works like a charm.

I am somewhat embarrassed by the fact that I just spent 12 hours or so mucking with this. It wasn't all because I left a couple of brackets in where there should have been any that accounted for way more than I like to admit to.

I suppose had I not been brand new to all of it I may have spotted the silliness .... Ah well.

Whilst we are on the subject of classes how do I go about creating a class global static .... I think that right. I want a variable defined in the class / library that is accessible to all instances. The requirement is so count the instances as they are created ....

Glad to help :)

You want something like this ?

Morse morse1;
Morse morse2;

morse1.getInstancesCount(); //return 2

It doesn't make much sense so I think I don't understand what you want :)

He means a static class variable. It can be done easily enough. Example:

class foo
  static int instances;

  foo ()  { instances++;  };  // constructor
  ~foo () { instances--;  };  // destructor

int foo::instances = 0;  // instantiate it

void setup ()
  Serial.begin (115200);
  foo a;
  foo b;
  Serial.println (foo::instances);

void loop () 
  foo c;
  Serial.println (foo::instances);
  while (true) ;



The constructor adds 1 to a static class variable. The destructor subtracts 1. Thus you know how many there are. The output is as expected.

Yes that makes sense I think ..... Thanks. assuming foo had the following structure ...

class foo
  static int ivalue;
  foo::foo ()  {};  // constructor
  int foo::GetValue(){return icount;};
  void foo::SetValue(int inewvalue){ivalue = inewvalue;};

Would the following be true?

foo fooa;
foo foob;

Serial.println(foo.GetValue);  >>> 10

If I have understood correctly and the above will work, would it also work if:-

  Functions ...
   static int ivalue;

Would the following be true?

That code is not correct. You don't have an instance called foo. You have a class called foo, with instances called fooa and foob. While you can access static data of the class using the scope resolution operator (Serial.print(foo::ivalue);), you need an instance name to call methods.





will print 10.

But, defining a class like that sucks. Really. Use some white space so it is readable.

Sorry ... silly mistake I meant foob.GetValue Thanks for the clarification.

Everything here answered I think .... Will start new topic with specifics, thanks all