-3

I'm a bit lost here and don't really get why i run into this compile error.

I have a Arduino Nano Project set up with platform.io

My main.cpp looks like this:

#include <WiFiNINA.h>
#include "MainController.h"

void setup()
{
    MainController mainController;
    pinMode(LEDR, OUTPUT);
    pinMode(LEDG, OUTPUT);
    pinMode(LEDB, OUTPUT);
    mainController.begin();
    digitalWrite(LEDR, HIGH);
    digitalWrite(LEDG, HIGH);
    digitalWrite(LEDB, LOW);
    delay(1000);
}

void loop()
{
  //mainController.update();
  digitalWrite(LEDR, LOW);
  digitalWrite(LEDG, HIGH);
}

MainController.cpp:

#include <MotorController.h>

class MainController {
  public:
    MainController() {
    }

    void begin() {
      motorController.begin();
    }

    void update() {
    }

  private:
    MotorController motorController;
};

MainController.h:

#ifndef MAINCONTROLLER_H
#define MAINCONTROLLER_H

#include "MotorController.h"
// #include "ServoController.h"

class MainController
{
public:
    MainController();
    void begin();
    void update();

private:
    MotorController motorController;
    // ServoController servoController;
};
#endif

So, when i try to compile it i get this compile error:

/Users/mneuhaus/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld: .pio/build/nanorp2040connect/src/main.cpp.o: in function `setup':
main.cpp:(.text.setup+0x4): undefined reference to `MainController::MainController()'
/Users/mneuhaus/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld: main.cpp:(.text.setup+0x28): undefined reference to `MainController::begin()'
collect2: error: ld returned 1 exit status
*** [.pio/build/nanorp2040connect/firmware.elf] Error 1

as far as i can tell all the classes, methods, etc are named correctly, no idea why i get these errors.

  • 1
    You are not defining your class's functions properly inside your cpp file. I'd recommend [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/) – ChrisMM Jan 25 '23 at 01:30
  • 1
    You don't compile MainController.cpp, otherwise you get errors about a multiple class definition. – 273K Jan 25 '23 at 01:33
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – ChrisMM Jan 25 '23 at 02:12

1 Answers1

0

You don't need to repeat the class definition inside the cpp file. The implementation should loook like this:

#include "MainController.h"

void MainController::begin() {
    motorController.begin();
}

// ...

I would also recommend removing the constructor if it does not do anythng.

Armandas
  • 2,276
  • 1
  • 22
  • 27