Arduino error: 'xyz' does not name a type

Hello everyone!!

I’m quite new to this community. The reason I joined is the following. I’m developing a multi-agent system with several robots. And a part of the code gives the ’ does not name a type’ error. I read a lot about it, but it didn’t helped. So i am turning to you with the hope that you would see something that I could not.
Here are the codes and the error message.

BasicMotionFnc.cpp

#include <Arduino.h>
#include <MeAuriga.h>
#include "BasicMotionFnc.h"

//Motor Left
const int pwmMotor1 = 11;
const int inMotor1_1 = 49;
const int inMotor1_2 = 48;

//Motor Right
const int pwmMotor2 = 10;
const int inMotor2_1 = 47;
const int inMotor2_2 = 46;


BasicMotionFnc::BasicMotionFnc() {
  setupMotorForBasicFunctions();
}

void BasicMotionFnc::setupMotorForBasicFunctions()
{
  pinMode(pwmMotor1, OUTPUT);   //We have to set PWM pin as output
  pinMode(inMotor1_1, OUTPUT); //Logic pins are also set as output
  pinMode(inMotor1_2, OUTPUT);
  pinMode(pwmMotor2, OUTPUT);   //We have to set PWM pin as output
  pinMode(inMotor2_1, OUTPUT); //Logic pins are also set as output
  pinMode(inMotor2_2, OUTPUT);
}

void BasicMotionFnc::MoveFWD(int motorSpeed) {
  digitalWrite(inMotor1_1, LOW);
  digitalWrite(inMotor1_2, HIGH);
  analogWrite(pwmMotor1, motorSpeed);
  digitalWrite(inMotor2_1, HIGH);
  digitalWrite(inMotor2_2, LOW);
  analogWrite(pwmMotor2, motorSpeed);
  Serial.println("Full speed mode"); //For debugging
}

void BasicMotionFnc::MoveBKW(int motorSpeed) {
  digitalWrite(inMotor1_1, HIGH);
  digitalWrite(inMotor1_2, LOW);
  analogWrite(pwmMotor1, motorSpeed);
  digitalWrite(inMotor2_1, LOW);
  digitalWrite(inMotor2_2, HIGH);
  analogWrite(pwmMotor2, motorSpeed);
  Serial.println("Full speed mode"); //For debugging
}

void BasicMotionFnc::StopMotion() {
  analogWrite(pwmMotor1, 0);
  analogWrite(pwmMotor2, 0);
  Serial.println("Stop");
  delay(100);
}


void BasicMotionFnc::MoveRight(int lPwm, int rPwm) {
  if (rPwm <= 60) rPwm = 60;
  if (lPwm >= 180) lPwm = 180;
  if (lPwm < rPwm)
  {
    int temp = lPwm;
    lPwm = rPwm;
    rPwm = temp;
  }

  digitalWrite(inMotor1_1, LOW);
  digitalWrite(inMotor1_2, HIGH);
  analogWrite(pwmMotor1, lPwm); //Set speed via PWM
  digitalWrite(inMotor2_1, HIGH);
  digitalWrite(inMotor2_2, LOW);
  analogWrite(pwmMotor2, rPwm); //Set speed via PWM
  //Serial.println("Turn the robot right"); //FOR DEBUGGING  <======
}
[...]

BasicMotionFnc.h

#ifndef BasicFnc_H
#define BasicFnc_H

#include <Arduino.h>
#include <MeAuriga.h>

class BasicMotionFnc
{
  public:
    BasicMotionFnc();
    void setupMotorForBasicFunctions();
    void MoveFWD(int motorSpeed);
    void MoveBKW(int motorSpeed);
    void StopMotion();
    void MoveRight(int leftPwm, int rightPwm);
    void MoveLeft(int leftPwm, int rightPwm);
    void TurnRight(int turnPwm);
    void TurnLeft(int turnPwm);
  private:
    const int pwmMotor1 = 11;
    const int inMotor1_1 = 49;
    const int inMotor1_2= 48;
    const int pwmMotor2 = 10;
    const int inMotor2_1 = 47;
    const int inMotor2_2 = 46;

};

#endif

DecisionMaker.cpp

#include <Arduino.h>
#include <MeAuriga.h>

#include "DecisionMaker.h"
#include "DecisionMakerBasicFnc.h"

DecisionMakerBasicFnc decisionMaker;

int decisionNr = 0;


DecisionMaker::DecisionMaker() {
}

void DecisionMaker::makeDecision() {

  bool objAtLeft[3] = {0, 0, 0};
  bool objAtRight[3] = {0, 0, 0};

  decisionMaker.leftDecision(objAtLeft);
  decisionMaker.rightDecision(objAtRight);

  decisionMaker.decide(objAtLeft, objAtRight);

  //return decisionNr;
}

DecisionMaker.h

#ifndef BasicFnc_H
#define BasicFnc_H

#include <Arduino.h>
#include <MeAuriga.h>

class DecisionMaker
{
  public:
    DecisionMaker();
    void makeDecision();
    int decisionNr;
};

#endif

DacisionMakerBasicFnc.cpp

#include <Arduino.h>
#include <MeAuriga.h>

#include "DecisionMakerBasicFnc.h"
#include "BasicMotionFnc.h"
#include "BasicFnc.h"

BasicMotionFnc Motor;
BasicFnc Ultrasonic;
MeGyro gyro(0, 0x69);

int basePos = 0;
int currentPos = 0;

DecisionMakerBasicFnc::DecisionMakerBasicFnc() {
  gyro.begin();
}

void DecisionMakerBasicFnc::leftDecision(bool (&objAtLeft)[3]) {
  gyro.update();
  basePos = gyro.getAngle(3);
  currentPos = basePos;

  for (int i = 0; i < 3; i++) {
    gyro.update();
    currentPos = gyro.getAngle(3);

    while ((currentPos - (i + 2) * 15) != basePos)
    {
      Motor.TurnLeft(100);
    }
    Motor.StopMotion();
    //Serial.println("L "+(currentPos-(i+2)*15)+Ultrasonic.GetFrontDistance() );
    if (Ultrasonic.GetFrontDistance() >= 100)
      objAtLeft[i] = 1;
    else
      objAtLeft[i] = 0;
  }

  while (currentPos != basePos) {
    Motor.TurnRight(100);
    gyro.update();
    currentPos = gyro.getAngle(3);
  }

}

void DecisionMakerBasicFnc::rightDecision(bool (&objAtRight)[3]) {
  gyro.update();
  basePos = gyro.getAngle(3);
  currentPos = basePos;

  for (int i = 0; i < 3; i++) {
    gyro.update();
    currentPos = gyro.getAngle(3);

    while ((currentPos - (i + 2) * 15) != basePos)
    {
      Motor.TurnRight(100);
    }
    Motor.StopMotion();
    //Serial.println(Ultrasonic.GetFrontDistance());
    if (Ultrasonic.GetFrontDistance() >= 100)
      objAtRight[i] = 1;
    else
      objAtRight[i] = 0;
  }

  while (currentPos != basePos) {
    Motor.TurnLeft(100);
    gyro.update();
    currentPos = gyro.getAngle(3);
  }

}

void DecisionMakerBasicFnc::decide(bool (&objAtLeft)[3], bool (&objAtRight)[3]) {

  byte decision = B000000;
  if (objAtLeft[0] == 1 && objAtLeft[1] == 1 && objAtLeft[2] == 1)
    decision = decision + B100000;
  else
  {
    if (objAtLeft[0] == 1 && objAtLeft[1] == 1)
      decision = decision + B001000;

    if (objAtLeft[1] == 1 && objAtLeft[2] == 1)
      decision = decision + B010000;
  }

  if (objAtRight[0] == 1 && objAtRight[1] == 1 && objAtRight[2] == 1)
    decision = decision + B000001;
  else
  {
    if (objAtRight[0] == 1 && objAtRight[1] == 1)
      decision = decision + B000100;

    if (objAtRight[1] == 1 && objAtRight[2] == 1)
      decision = decision + B000010;
  }
  int decisionInDec = decision;
  //return decisionInDec;
}

DecisionMakerBasicFnc.h

#ifndef DecisionMakerBasicFnc_H
#define DecisionMakerBasicFnc_H

#include <Arduino.h>
#include <MeAuriga.h>
#include "BasicMotionFnc.h"
#include "BasicFnc.h"

class DecisionMakerBasicFnc
{
  public:
    DecisionMakerBasicFnc();
    void leftDecision(bool (&objAtLeft)[3]);
    void rightDecision(bool (&objAtRight)[3]);
    void decide(bool (&objAtLeft)[3], bool (&objAtRight)[3]);
  protected:
    BasicMotionFnc Motor;
    BasicFnc Ultrasonic;
    MeGyro gyro;
};

#endif

The error highlights this row (in the DacisionMakerBasicFnc.h, after the protected:):

BasicMotionFnc Motor;

And finally the error message:

Arduino: 1.8.5 (Windows 10), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

In file included from sketch\DecisionMaker.cpp:5:0:

DecisionMakerBasicFnc.h:17: error: 'BasicMotionFnc' does not name a type

     BasicMotionFnc Motor;

     ^

DecisionMakerBasicFnc.h:18: error: 'BasicFnc' does not name a type

     BasicFnc Ultrasonic;

     ^

exit status 1
'BasicMotionFnc' does not name a type

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

I am working in a different folder, then the arduino/libraries, but I tried to move there and the error was the same. I reinstalled the Arduino IDE, and the makeblock library I am using, but nothing changed.
I new to Arduino, I am working with it a half a year. Until this project I just made some small programs, so i didn’t create classes before. So probably something obvious for you, but not for me.

Thank in advance for your help.

The compiler tells what it is doing.
It was reading "sketch\DecisionMaker.cpp" and included "DecisionMakerBasicFnc.h" and in that file at line 17 it noticed "BasicMotionFnc", but the compiler does not know that class.

It seems that the class "BasicMotionFnc" can not be found, so the file "BasicMotionFnc.cpp" was included somehow.

Can you tell where the files are ? For example a list of every file that you use. And also tell where the project location is pointing to in the settings.
A project file in the directory "sketch" is weird. Did you name the main *.ino file "sketch.ino" ? Why did you change the default locations ? Is the Arduino IDE running in portable mode ?

Your #include guards are not quite right:

BasicMotionFnc.h
#ifndef BasicFnc_H
#define BasicFnc_H

DecisionMaker.h
#ifndef BasicFnc_H
#define BasicFnc_H

Hello. My project is in a separate folder, in the E partition of my windows (E:\Vizsgamunka\UsefulCodes\DecisionMakerTest) with all of the classes (.h + .cpp files) in that folder.
My Arduino is installed on the C partition (C:\Program Files (x86)\Arduino)

Every file that I am using:
DecisionMakerTest.ino
DecisionMakerBasicFnc.h
DecisionMakerBasicFnc.cpp
BasicMotionFnc.h
BasicMotionFnc.cpp

(I tried to resolve the code, so now is looking somehow differently, but the error message still the same)

Have you fixed the #include guards?

Nope, because I do not know, what is wrong with them :slight_smile: If only you could give an explanation...

BasicMotionFnc.h
   #ifndef BasicFnc_H
   #define BasicFnc_H

DecisionMaker.h
   #ifndef BasicFnc_H
   #define BasicFnc_H

each relies on the same #define

try like this:

BasicMotionFnc.h
   #ifndef BasicFnc_H
   #define BasicFnc_H

DecisionMaker.h
   #ifndef DecisionMaker_H
   #define DecisionMaker_H

Thanks! But it didn't was the source of the problem. The problem is, that the class, created by me, BasicMotionFnc, has to be included in other class(es) and also in the main (.ino) file. And gives this error now

sketch\DecisionMakerTest.ino.cpp.o (symbol from plugin): In function `DecisionMaker':

(.text+0x0): multiple definition of `encoder_Port'

sketch\DecisionMakerBasicFnc.cpp.o (symbol from plugin):(.text+0x0): first defined here

c:/program files (x86)/arduino/hardware/tools/avr/bin/../lib/gcc/avr/4.9.2/../../../../avr/bin/ld.exe: Disabling relaxation: it will not work with multiple definitions

sketch\DecisionMakerTest.ino.cpp.o (symbol from plugin): In function `DecisionMaker':

(.text+0x0): multiple definition of `mePort'

sketch\DecisionMakerBasicFnc.cpp.o (symbol from plugin):(.text+0x0): first defined here

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino/Genuino Mega or Mega 2560.
[code]

[/code]

I am really hopeless ...

Assuming that you fixed the Include Guards, try ‘#include “BasicMotionFnc.h”’ inside DecisionMakerBasicFnc.h (near top).

Also, in DecisionMakerBasicFnc.h you have:

BasicFnc Ultrasonic;

But, I don’t see a BasicFnc class defined anywhere.

Finally, congratulations on your confusing choices of file names, class names, and method names.

On the way I modified my code. I deleted the BasicFnc class as it was unnecessarry. But I removed the inclusions for it as well.

I found out that the two variable 'mePort' and 'encoder_Port' (which are defined multiple times) are in the MeAuriga.h file

MePort_Sig mePort[17] =
 {
   { NC, NC }, {   5,   4 }, {   3,   2 }, {   7,   6 }, {   9,   8 }, 
   { 16, 17 }, { A10, A15 }, {  A9, A14 }, {  A8, A13 }, {  A7, A12 }, 
   //             LIGHT2        LIGHT1        TEMP          SOUND
   { A6,A11 }, {  NC,  A2 }, {  NC,  A3 }, {  NC,  A0 }, {  NC,  A1 },
   { NC, NC }, { NC, NC },
 };
 
Encoder_port_type encoder_Port[6] =
{
  { NC,     NC,     NC,     NC,     NC},
  //ENA A   ENA B   PWMA    DIR A2  DIR A1
  { 19,     42,     11,     49,     48},
  //ENB A   ENB B   PWMB    DIR B1  DIR B2
  { 18,     43,     10,     47,     46},
  { NC,     NC,     NC,     NC,     NC},
  { NC,     NC,     NC,     NC,     NC},
};
[code]

So somehow this library is included twice, but I do not see where.
At this link you could see the whole project (in comments I can't include all of it) as well as the MeAuiga and Wire libraries.

LINK: DecisionMaker - Google Drive