-2

I browsed many similar questions but cannot find an answer fitting to my problem. I try to create a abstract class and extend that in a different .cpp file. If I try to compile i am getting this error.

In file included from src/main.cpp:6:0:
src/MQBIOTExecutor.cpp:4:7: error: redefinition of 'class MQBIOTExecutor'
 class MQBIOTExecutor
       ^
In file included from src/MQBIOT.cpp:3:0,
                 from src/main.cpp:5:
src/MQBIOTExecutor.cpp:4:7: error: previous definition of 'class MQBIOTExecutor'
 class MQBIOTExecutor
       ^
In file included from src/MyExecutor.cpp:3:0,
                 from src/main.cpp:7:
src/MQBIOTExecutor.cpp:4:7: error: redefinition of 'class MQBIOTExecutor'
 class MQBIOTExecutor
       ^
In file included from src/MQBIOT.cpp:3:0,
                 from src/main.cpp:5:
src/MQBIOTExecutor.cpp:4:7: error: previous definition of 'class MQBIOTExecutor'
 class MQBIOTExecutor
       ^

I have already tried many things like putting the abstract class into a header file or making all function pure virtual and not pure virtual etc.

My MQBIOTExecutor.cpp

#include <Arduino.h>
#include <ArduinoJson.h>

class MQBIOTExecutor
{
public:
    virtual void getState() = 0;

    virtual void getCommands() = 0;
    virtual void executeCommand(JsonDocument jdoc) = 0;

    virtual void getConfig() = 0;
    virtual void setConfig(JsonDocument jdoc) = 0;

    virtual void getSensor() = 0;

    virtual void extend(String topic, String payload, JsonDocument payloadJsonDoc) = 0;
};

My MyExecutor.cpp

#include <Arduino.h>
#include <ArduinoJson.h>
#include <MQBIOTExecutor.cpp>

class MyExecutor : public MQBIOTExecutor
{
public:
    virtual void getState()
    {
    }

    virtual void getCommands()
    {
    }
    virtual void executeCommand(JsonDocument jdoc)
    {
    }

    virtual void getConfig()
    {
    }
    virtual void setConfig(JsonDocument jdoc)
    {
    }

    virtual void getSensor()
    {
    }

    virtual void extend(String topic, String payload, JsonDocument payloadJsonDoc)
    {
    }
};

The rest is irrelevant. If I remove the inheritance the whole thing compiles.

epileptic
  • 27
  • 5
  • 7
    Rule of thumb: Never include a cpp file. Class declarations should be in a header file that gets included where needed and the cpp file where the implementation is should be compiled and linked to all of your other object files. – NathanOliver Oct 09 '19 at 14:49
  • 1
    Do you have Include guards? – ChrisMM Oct 09 '19 at 14:51
  • 1
    You have not seen examples of including .cpp files anywhere, and that's not because nobody else thought of it; it's because it's wrong. `#include` is not like `import` in Java. (C++ is not much like Java, except for punctuation and the spelling of some keywords. You might want to get a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list).) – molbdnilo Oct 09 '19 at 15:15

2 Answers2

1

You don't use any include guards:

#ifndef MQBIOTEXECUTER_H //only define this class the first time the pre-processor sees this definition
#define MQBIOTEXECUTER_H

#include <Arduino.h>
#include <ArduinoJson.h>

class MQBIOTExecutor
{
public:
    virtual void getState() = 0;

    virtual void getCommands() = 0;
    virtual void executeCommand(JsonDocument jdoc) = 0;

    virtual void getConfig() = 0;
    virtual void setConfig(JsonDocument jdoc) = 0;

    virtual void getSensor() = 0;

    virtual void extend(String topic, String payload, JsonDocument payloadJsonDoc) = 0;
};

#endif 

So this class gets compiled every time you include it and is therefore redefined.

RoQuOTriX
  • 2,871
  • 14
  • 25
0

I followed the hint given by @NathanOliver

Rule of thumb: Never include a cpp file. Class declarations should be in a header file that gets included where needed and the cpp file where the implementation is should be compiled and linked to all of your other object files.

and restructured my project. The problem still appeared so i continued searching the web and figured out that adding #pragma once at the beginning of my header file fixed the issue.

epileptic
  • 27
  • 5