Error : does not name a type

Hello everyone,
I'm stuck on this issue. I tried to simplify my script as best as possible to reproduce the error. Can can some please explain why I am getting the following error. Thank you so much for your help.

In file included from sketch\Box.cpp:1:0:
Box.h:4:5: error: 'Debug' does not name a type
     Debug debug;

Source : Main

#include "Debug.h"
#include "Box.h"

Debug debug;
Box box;

void loop(){
  debug.message("Loop");
  debug.line();

  box.example();
}

void setup(){
  debug.init();
  box.init();
  
  debug.message("Setup Complete");
  debug.line();
}

Source : Debug.h

class Debug {
  private:
    const bool state = true;
  public:
    void init();
    void message(const char *str);
    void message(int);
    void line();
};

Source : Debug.cpp

#include <Arduino.h>
#include "Debug.h"

void Debug::init() {
  if (state){
    Serial.begin(115200);
  }
}
void Debug::message(const char *messageChar) {
  if (state){
    const char *p;
    p = messageChar;
    while (*p) {
        Serial.print(*p);
        p++;
    }
  }
}
void Debug::message(int messageInt) {
  if (state){
    Serial.print(messageInt);
  }
}
void Debug::line() {
  if (state){
    Serial.println();
  }
}

Source : Box.h

class Box {  
  private:
    unsigned long startTime = 0;
    Debug debug; 
  public:
    void init();
    void example();
};

Source : Box.cpp

#include "Box.h"

void Box::init() { }
void Box::example() { }
In file included from sketch\Box.cpp:1:0:
Box.h:4:5: error: 'Debug' does not name a type

Box.h appears to be missing a "#include "Debug.h""

Sorry...I should have mentioned that I tried that. It produces the following error

n file included from sketch\Box.h:1:0,
                 from D:\project\escape\puzzle box\source\Bug\Bug.ino:2:
Debug.h:1:7: error: redefinition of 'class Debug'
 class Debug {
       ^~~~~
In file included from D:\project\escape\puzzle box\source\Bug\Bug.ino:1:0:
sketch\Debug.h:1:7: note: previous definition of 'class Debug'
 class Debug {
       ^~~~~
exit status 1
redefinition of 'class Debug'

I believe this is because it is included already in main

Debug, its a lot like cheeseburger. Try to redefine cheeseburger and you will run into all sorts that think its already defined. Try a different name?

Oh, and what its the point of state? It looks like you default it to true. Then test for it everywhere and nowhere is it ever changed. Seeing that its private, no one else can change it either.

-jim lee

I had kind-of assumed you were omitting the guards in your .h files for clarity, but now it seems you really did just simply omit them.

Hi,

If what I say is wrong, I'm sorry, because
my main area of competence is electronics,
although I like it and use a lot of programming.

Copy all files and tested them here.
I commented the following line "// Debug debug;" in the file
Box.h. It compiled and worked correctly.

RV mineirin

As AWOL said, you have to add "include guards", see this pragma once - Wikipedia

Hello!
If you found the solution to the error it would be nice to share it. We could use it...

Regards :wink:

Thanks everyone for helping out. The issue was indeed that I needed to include the file AND I needed to start using include guards.

I really appreciate this community, it really keeps going...I don't know if I could struggle through this on my own.

Hi,
So can you please post your new revised code?
This is so this thread can be used to help solve a similar problem someone may have.

Tom.. :grinning: :+1: :coffee: :australia:

No problem...also note that the declaring of state in Debug in the header was an issue I created as well

Source : Main

#include "Debug.h"
#include "Box.h"

Debug debug;
Box box;

void loop(){
  debug.message("Loop");
  debug.line();

  box.example();
}

void setup(){
  debug.init();
  box.init();
  
  debug.message("Setup Complete");
  debug.line();
}

Source : Debug.h

#ifndef DEBUG_H
#define DEBUG_H

class Debug {
  private:
    const bool state;
  public:
    void init();
    void message(const char *str);
    void message(int);
    void line();
};


#endif

Source : Debug.cpp

#include <Arduino.h>

void Debug::init() {
  state = true;
  if (state){
    Serial.begin(115200);
  }
}
void Debug::message(const char *messageChar) {
  if (state){
    const char *p;
    p = messageChar;
    while (*p) {
        Serial.print(*p);
        p++;
    }
  }
}
void Debug::message(int messageInt) {
  if (state){
    Serial.print(messageInt);
  }
}
void Debug::line() {
  if (state){
    Serial.println();
  }
}

Source : Box.h

#ifndef BUTTON_H
#define BUTTON_H

#include "Debug.h"

class Box {  
  private:
    unsigned long startTime = 0;
    Debug debug; 
  public:
    void init();
    void example();
};

#endif

Source : Box.cpp

#include "Box.h"

void Box::init() { }
void Box::example() { }

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