Compile Error With Class

Hi,

I try to create a class and fail somehow to compile it - I would be very happy if someone can help me.

Thank you all …

COMPILE ERROR:

/tmp/cc6pT0Gx.ltrans0.ltrans.o: In function `global constructors keyed to 65535_0_robard.ino.cpp.o.1735':
<artificial>:(.text.startup+0x64): undefined reference to `HCSRO4::HCSRO4(int, int)'
collect2: error: ld returned 1 exit status

Error during build: exit status 1

.ino - file

#include "libraries/HCSRO4/HCSRO4.h"

#define CatTriggerPin 30
#define CatEchoPin 31
HCSRO4 cat_sensor(CatTriggerPin, CatEchoPin);

void setup() {

  Serial.begin(9600);

}

void loop() {

  float hoho = cat_sensor.distincm;

  Serial.println(hoho);

}

.h - file

#ifndef HCSRO4_h
#define HCSRO4_h

#include "Arduino.h"

class HCSRO4
{

  public:

    HCSRO4(int trigpin, int echopin);
    void GetDistInCm();
    float distincm;

  private:

    float _distincm;
    float _duration;
    int _delay_ms = 10;
    int _trigpin;
    int _echopin;

};

#endif

.cpp - file

#include "Arduino.h"
#include "HCSRO4.h"

HCSRO4::HCSRO4(int trigpin, int echopin) {

  pinMode(trigpin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echopin, INPUT); // Sets the echoPin as an Input

  int _trigpin = trigpin;
  int _echopin = echopin;

}

HCSRO4::GetDistInCm() {

  digitalWrite(_trigpin,LOW);
  delayMicroseconds(2);
  digitalWrite(_trigpin,HIGH);
  delayMicroseconds(_delay_ms);
  digitalWrite(_trigpin,LOW);

  // Reads the echoPin, returns the sound wave travel time in microseconds
  _duration = pulseIn(_echopin,HIGH);
  _distincm = (_duration - _delay_ms)* 0.034 / 2; // Speed of sound wave divided by 2 (go and back)

  distincm = _distincm

}

The file structure is:

./libraries
./libraries/HCSRO4
./libraries/HCSRO4/HCSRO4.cpp
./libraries/HCSRO4/HCSRO4.h
./robard.ino

the message is the linker complaining, not the compiler. it's complaining that the constructor would be missing. but in the .cpp file you have the line "HCSRO4::GetDistInCm() {" without a return type, so i guess you missed the compiler error about this. that might be the reason why the .cpp isn't compiled and in turn the constructor is missing in the object file. at least that would be my guess. does adding "void " at the beginning of the line fix the problem?

Hi frozenrat

Thank you for the quick reply.
Im not quite shure if I understand you well - Do you mean the following?

VVVV ------------------------------- This ?
void HCSRO4::GetDistInCm() {

  digitalWrite(_trigpin,LOW);
  delayMicroseconds(2);
  digitalWrite(_trigpin,HIGH);
  delayMicroseconds(_delay_ms);
  digitalWrite(_trigpin,LOW);

I still get the same error:

tmp/ccYLgxMa.ltrans0.ltrans.o: In function `global constructors keyed to 65535_0_robard.ino.cpp.o.1735':
<artificial>:(.text.startup+0x64): undefined reference to `HCSRO4::HCSRO4(int, int)'
collect2: error: ld returned 1 exit status

Hi frozenrat

Thank you very much it was the described solution and I had to put the *.cpp and *.h files into the top directory to make it work ... And I also did som Copy&Pasting which was not very intelligent ... :slight_smile: Copy&Paste is not my friend.

Thank you 1000 * x times.

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