简体   繁体   中英

Undefined Reference error for Constructor using Arduino IDE

I'm working on a student project to create a pair of robots that tell the time by climbing up and down, using Arduinos (including the Arduino IDE). Part of this project includes a simple ML AI that will auto adjust the speed of the robot to make sure it reaches the top at the correct rate. I've made a library to provide the necessary variables and functions to perform this task.

In an earlier version of this library (which compiled just fine), there was only one constructor that used an array to store the data, such that the array length is always a specific length. In the next version, another constructor is being made to allow for an array length specified upon construction, as the Arduino IDE does not allow for the usage of the vector library. However, when attempting to compile the source code with the setup and loop functions, an undefined reference error to the constructor being used. Ignoring the irrelevant code, my codebase looks like

Main program

#include "libraries/SpeedCorrector/SpeedCorrector.h"

const uint8_t MAX_HOURS = 12; //12 hour clock
const uint32_t CORRECT_TIME = MAX_HOURS*60*60*1000; //Num of milliseconds in 12 hours
const uint16_t INITIAL_PWM = 300; //dummy initial PWM value for testing

uint32_t climbTime;
uint16_t currentPwm;
uint16_t tempPwm;
bool photoInterruptMet = false;

SpeedCorrector speedCorrector(INITIAL_PWM, CORRECT_TIME);

SpeedCorrector.h

#ifndef SpeedCorrector_h
#define SpeedCorrector_h

#include "Arduino.h"

class SpeedCorrector {
    public:
        const static uint8_t MAX_NUM_OF_PWM = 10; //default value
        const static uint16_t MOTOR_SLIP_CORRECTION = 10; //default value

        SpeedCorrector(uint16_t initialPwm, uint32_t inCorrectTime);
        SpeedCorrector(uint16_t initialPwm, uint32_t inCorrectTime, uint8_t inMaxNumOfPwm, uint8_t inMotorSlipCorrection);
    private:
        uint8_t pwmIndex;
        uint8_t maxNumOfPwms;
        bool correctedPwmsFull;
        uint32_t correctTime;
        uint8_t motorSlipCorrection;
};

#endif

SpeedCorrector.cpp

#include "Arduino.h"
#include "SpeedCorrector.h"

SpeedCorrector::SpeedCorrector(uint16_t initialPwm, uint32_t inCorrectTime) {
    pwmIndex = 0;
    maxNumOfPwms = MAX_NUM_OF_PWMS;
    uint16_t correctedPwms[maxNumOfPwms];
    correctedPwmsFull = false;
    correctedPwms[0] = initialPwm;
    correctTime = inCorrectTime;
    motorSlipCorrection = MOTOR_SLIP_CORRECTION;
}

SpeedCorrector::SpeedCorrector(uint16_t initialPwm, uint32_t inCorrectTime, 
                          uint8_t inMaxNumOfPwms, uint8_t inMotorSlipCorrection) {
  pwmIndex = 0;
  maxNumOfPwms = inMaxNumOfPwms
  uint16_t correctedPwms[maxNumOfPwms];
  correctedPwmsFull = false;
  correctedPwms[0] = initialPwm;
  correctTime = inCorrectTime;
  motorSlipCorrection = inMotorSlipCorrection;
}

Unfortunately, I've had to declare the array in the constructor to allow for the length to be specified upon construction.

I've attempted to comment out the array to see if I get different error messages, but to no avail. I've also moved the library into a different directory and I get a no such file or directory error, so it's definitely detecting the library.

The error complains on the construction of the SpeedCorrector in the main program, stating

undefined reference to `SpeedCorrector::SpeedCorrector(unsigned int, unsigned long)'

I got the instruction on how to make the library from https://www.arduino.cc/en/Hacking/LibraryTutorial

According to File/Preferences in the arduino IDE, the default SketchBook location is /home/user_name/Arduino .

So the SpeedCorrector.h and SpeedCorrector.cpp files should be placed in /home/user_name/Arduino/libraries/SpeedCorrector , not in a subdirectory of the one where stands the main ( .ino ) program.

As stated by Juraj in the comments, the main program ( .ino ) should then use #include <SpeedCorrector.h> , without any full path to the header file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM