Request for member ' ' in ' ', which is of non-class type 'Class()'

I've followed along with this Library Tutorial, and I thought I organized my library in the exact same manner, but I keep getting error messages.

Errors:

Arduino: 1.8.15 (Windows 10), Board: "Arduino Uno"

C:\Users\abeth\Documents\Arduino\sketch_TimerLibraryTest\sketch_TimerLibraryTest.ino: In function 'void setup()':

sketch_TimerLibraryTest:7:9: error: request for member 'set' in 'timer', which is of non-class type 'Timer()'

   timer.set();

         ^~~

C:\Users\abeth\Documents\Arduino\sketch_TimerLibraryTest\sketch_TimerLibraryTest.ino: In function 'void loop()':

sketch_TimerLibraryTest:11:9: error: request for member 'start' in 'timer', which is of non-class type 'Timer()'

   timer.start();

         ^~~~~

sketch_TimerLibraryTest:12:9: error: request for member 'print' in 'timer', which is of non-class type 'Timer()'

   timer.print();

         ^~~~~

exit status 1

request for member 'set' in 'timer', which is of non-class type 'Timer()'



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

.ino:

#include <Timer.h>

Timer timer();

void setup() {
  Serial.begin(9600);
  timer.set();
}

void loop() {
  timer.start();
  timer.print();
}

.cpp:

#include "Arduino.h"
#include "Timer.h"

Timer::Timer(){
  printInterval = 1000;
  previousTime = 0;
  
}

void Timer::set(){
  secondsTime = 0;
  minutesTime = 0;
  hoursTime = 0;
}
void Timer::set(int sec){
  secondsTime = sec;
  minutesTime = 0;
  hoursTime = 0;
}
void Timer::set(int sec, int mins){
  secondsTime = sec;
  minutesTime = mins;
  hoursTime = 0;
}
void Timer::set(int sec, int mins, long hrs){
  secondsTime = sec;
  minutesTime = mins;
  hoursTime = hrs;
}

void Timer::start(){
  unsigned long currentTime = millis();
  
    if(currentTime - previousTime >= printInterval){
      previousTime = currentTime;
      secondsTime = secondsTime + 1;
    if(secondsTime >= 60){
      secondsTime = secondsTime - 60;
      minutesTime = minutesTime + 1;
    }
    if(minutesTime >= 60){
      minutesTime = minutesTime - 60;
      hoursTime = hoursTime + 1;
    }
}

void Timer::print(){
  Serial.print(hoursTime);
  Serial.print(":");
  if(minutesTime < 10){
    Serial.print(0);
    Serial.print(minutesTime);
    }else{
    Serial.print(minutesTime);
    }
  Serial.print(":");
  if(secondsTime < 10){
    Serial.print(0);
    Serial.print(secondsTime);
    }else {
    Serial.print(secondsTime);
    }
}

void Timer::reset(){
  secondsTime = 0;
  minutesTime = 0;
  hoursTime = 0;
}

void Timer::count(unsigned long interval){
  intervalOther = interval;
  unsigned long currentOther = millis();
  unsigned long previousOther = 0;
    if(currentOther - previousOther >= intervalOther){
      previousOther = currentOther;
      countState = true;
  }else{
      countState = false;
  }
}

.h:

#ifndef Timer_h
#define Timer_h

#include "Arduino.h"

class Timer
{
  public:
    void set();
    void set(int sec);
    void set(int sec, int mins);
    void set(int sec, int mins, long hrs);
    void start();
    void print();
    void reset();
    void count(unsigned long interval);
    bool countState;
  private:
    long printInterval;
    unsigned long previousTime;
    int secondsTime;
    int minutesTime;
    int hoursTime;
    unsigned long currentTime;
};

#endif

Try making a src folder inside your library's subdirectory and putting Timer.h and Timer.cpp in there.

-libraries
  -timer_library_directory
    -src
      -Timer.h
      -Timer.cpp

I put them in a src folder, but it then gives me this:

Arduino: 1.8.15 (Windows 10), Board: "Arduino Uno"

sketch_TimerLibraryTest:1:10: fatal error: Timer.h: No such file or directory

 #include <Timer.h>

          ^~~~~~~~~

compilation terminated.

exit status 1

Timer.h: No such file or directory

Invalid library found in C:\Users\abeth\Documents\Arduino\libraries\Timer: no headers files (.h) found in C:\Users\abeth\Documents\Arduino\libraries\Timer



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Would having a keywords.txt file affect this?
If so, here's mine:

Timer	KEYWORD1
set		KEYWORD2
start	KEYWORD2
print	KEYWORD2
reset	KEYWORD2
count	KEYWORD2

Change

to

Timer timer;

The first is the declaration of a function returning a Timer, the second is a timer object.

Keywords just set syntax highlighting on constants or methods. It isn't actually necessary.

You should be able to solve the problem by adding a library.properties file in the root directory of your library. It should hold something like this:

name=Timer
version=1.0.0
author=azurablaze
maintainer=azurablaze
sentence=Timer Library
paragraph=Timer library
category=Data Processing
url=https://github.com/[timer_library_url]
architectures=*

Change the information to fit the stuff for your library. Unless you plan on releasing this as an official third-party library, the information in there doesn't really matter. The Arduino IDE just needs to be told that your library is a library, and that is done with a library.properties file.

Alternatively, if you want this library to be more project-based, move the src file from earlier into your project directory instead. You'll probably need to include the library in your .ino file like this then:

#include "src/Timer.h"

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.