Class Template, error

I'm doing something wrong somewhere, :grinning: I can't figure it out, and my brain doesn't work anymore. :roll_eyes:

file ex.h

template <uint16_t _AMOUNT>
class Terminal
{
private:
    uint32_t TMRS[_AMOUNT], prds[_AMOUNT], loopTime{}, loopTimeMax{};
    bool states[_AMOUNT];

    uint32_t uptime();
    void (*callbacks[10])() = {};
public:
    void setMicros(bool mode);
    void tick();
    void attach(int num, void (*f)(void), uint32_t prd = 0);
    void detach(int num);
    void setPeriod(int num, uint32_t prd);
    void start(int num);
    void restart(int num);
    void stop(int num);
    void exec(int num);
    uint32_t getLeft();

    void terminal();
};

file ex.cpp

template <uint16_t _AMOUNT>
void Terminal<_AMOUNT>::terminal()
{
  TIMER = millis();
  
  _gfx.render(calcTerminal);

  if (Serial.available() != 0)
  {
    char text[20]{};
    Serial.readBytesUntil('\n', text, sizeof(text));

    for (Command &command : commands)
    {
      if (not strncmp(command.text, text, 20))
      {
        command.active = true;
      }
    }
  }
}

Run sketch

Terminal <5> trm;

void setup()
{
    gfx.initializationSystem();
}

void loop()
{
    trm.terminal();
}

An error is coming out :face_with_symbols_over_mouth:

Starting] Verifying sketch 'src\src.ino'

[Warning] Output path is not specified. Unable to reuse previously compiled files. Build will be slower. See README.

c:/users/student/appdata/local/arduino15/packages/esp32/tools/xtensa-esp32-elf-gcc/esp-2021r2-patch5-8.4.0/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: C:\Users\student\AppData\Local\Temp\arduino\sketches\99CDBD730A8D8BAE8ACA9CBC2BC49A5C\sketch\src.ino.cpp.o:(.literal._Z4loopv+0x4): undefined reference to `Terminal<(unsigned short)5>::terminal()'

c:/users/student/appdata/local/arduino15/packages/esp32/tools/xtensa-esp32-elf-gcc/esp-2021r2-patch5-8.4.0/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: C:\Users\student\AppData\Local\Temp\arduino\sketches\99CDBD730A8D8BAE8ACA9CBC2BC49A5C\sketch\src.ino.cpp.o: in function `loop()':

C:\Users\student\Documents\GitHub\EX\src/src.ino:17: undefined reference to `Terminal<(unsigned short)5>::terminal()'

collect2.exe: error: ld returned 1 exit status

Error during build: exit status 1

IntelliSense configuration already up to date. To manually rebuild your IntelliSense configuration run "Ctrl+Alt+I"

[Error] Verifying sketch 'src\src.ino': Exit with code=1
  1. The implementation of a template class must be in the same file as the declaration.

  2. The main .ino sketch must include the above file.

  3. You have multiple undefined variables (i.e. TIMER & gfx).

2,3 - с этим проблем нет
1 - это строгое правило?

English, please.

2.3 - there are no problems with this
1 - is this a strict rule?

Of course there is. It won't compile with undefined variables. If you want complete help, then post complete code.

Only if you want it to compile successfully.

:sweat_smile: ok,ok,ok

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