Declaring variables outside of class in .h file for use in int function in .cpp

Hi all,

I have a .cpp file that has

void function{
}
void function {
}
void function {
}
int function {
/* GET OR STORE THE HTTPS CODE */
int storeHttpsCode(long code) {
  if (code == 201) { //if we are to store the code
    result = code;
    Serial.println("201 Code");
    Serial.println(result);
  }
  else if (code == 1) { //if we are to return the code
    Serial.println("Return Code");
    Serial.println(result);
    return result; //return the code
    code = NULL; //clear the https code
  }
}

In my .h file I currently have:

#ifndef MyLibrary_h
#define MyLibrary_h

#include "Arduino.h"

int storeHttpsCode(long code);
long result;

class classname
{
  public:
    variable
    variable
    void function
  private:
};

#endif

But it doesn't like a variable being declared outside the class for the int function.

Does anyone know how to successfully initialize a variable for an int function outside a class?

Any help would be much appreciated!

Thanks,

Zeb

you should have posted the error

you shouldn't define variables, "result", in .h files because if the .h file is included in more than one other .cpp/.ino file there is an error because the definition is redundant.

Hi gcjr, thanks for your help! Unfortunately there is no "error" as such it just says it "couldn't compile".

So where do I initialize the variable "long result" for use in the .cpp file? (in an int function)

Thanks again,

Zeb

Sorry there is an error, here it is:

sketch\ESP32-testcode.ino.cpp.o:(.bss.result+0x0): multiple definition of `result'
sketch\testcode.cpp.o:(.bss.result+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
Multiple libraries were found for "Adafruit_SSD1306.h"
 Used: C:\Users\E543-PC\Documents\Arduino\libraries\Adafruit_SSD1306
 Not used: C:\Users\E543-PC\Documents\Arduino\libraries\arduino_37042
Multiple libraries were found for "WiFi.h"
 Used: C:\Users\E543-PC\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\WiFi
 Not used: C:\Program Files (x86)\Arduino\libraries\WiFi
exit status 1
Error compiling for board ESP32 Dev Module.

Apologies,

Zeb

this can't work

void function{
}
void function {
}
void function {
}

nor this:

public:
variable
variable
void function
private:

on the other hand, the error message might show another problem.

So either

  • post your complete project, including all files and links to all used libraries. You can ZIP and post your files. or
  • make a minimum sketch which shows the problem - and again - post the full project

Hi all,

Sorry, I tried to simplify it to much!

I have written a very small piece of code.

.ino:

#include "TestCode.h"

long statusCode = 201;
testCode testcode;

void setup() {
  Serial.begin(115200);
}

void loop() {
  testcode.generateCode(statusCode);
  long httpsCode = storeHttpsCode(1);
  Serial.println("HTTPS Code Main Loop");
  Serial.println(httpsCode);
  if (httpsCode == 201) {
    Serial.println("dequeueing");
  }
  delay(1000);
}

.cpp

#include "Arduino.h"
#include "TestCode.h"

void testCode::generateCode(long statusCode) {
  switch (statusCode)
  {
    case 200:
      {
        Serial.println("Request succeeded");
        Serial.println("======================================================");
        break;
      }
    case 201:
      {
        Serial.println("New object created");
        Serial.println("======================================================");
        storeHttpsCode(201);
        break;
      }
    case 400:
      {
        Serial.println("Invalid request");
        Serial.println("======================================================");
        break;
      }
  }
}

/* GET OR STORE THE HTTPS CODE */
int storeHttpsCode(long code) {
  if (code == 201) { //if we are to store the code
    result = code;
    Serial.println("201 Code");
    Serial.println(result);
  }
  else if (code == 1) { //if we are to return the code
    Serial.println("Return Code");
    Serial.println(result);
    return result; //return the code
    code = NULL; //clear the https code
  }
}

.h

#ifndef TestCode_h
#define TestCode_h

#include "Arduino.h"

int storeHttpsCode (long code);
long result;

class testCode
{
  public:
    void generateCode(long statusCode);
  private:
};

#endif

And my error is:

sketch\TestCode.cpp.o:(.bss.result+0x0): multiple definition of `result'
sketch\ESP32-Test-Code.ino.cpp.o:(.bss.result+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board ESP32 Dev Module.

Thank you and apologies,

Zeb

"result" should be defined in one file, either .cpp or ,ino.

it should be declared as extern in the .h

that .h should be included in any .cpp/.ino that uses variables or functions defined in a different .cpp/.ino

Sweet thanks :slight_smile:

Here is the full code for anyone with the same problem :slight_smile:

.ino

#include "TestCode.h"

long statusCode = 201;
testCode testcode;

void setup() {
  Serial.begin(115200);
}

void loop() {
  testcode.generateCode(statusCode);
  long httpsCode = testcode.storeHttpsCode(1);
  Serial.println("HTTPS Code Main Loop");
  Serial.println(httpsCode);
  if (httpsCode == 201) {
    Serial.println("dequeueing");
  }
  delay(1000);
}

.cpp

#include "Arduino.h"
#include "TestCode.h"

void testCode::generateCode(long statusCode) {
  switch (statusCode)
  {
    case 200:
      {
        Serial.println("Request succeeded");
        Serial.println("======================================================");
        break;
      }
    case 201:
      {
        Serial.println("New object created");
        Serial.println("======================================================");
        storeHttpsCode(201);
        break;
      }
    case 400:
      {
        Serial.println("Invalid request");
        Serial.println("======================================================");
        break;
      }
  }
}

/* GET OR STORE THE HTTPS CODE */
int testCode::storeHttpsCode(long code) {
  if (code == 201) { //if we are to store the code
    result = code;
    Serial.println("201 Code");
    Serial.println(result);
  }
  else if (code == 1) { //if we are to return the code
    Serial.println("Return Code");
    Serial.println(result);
    return result; //return the code
    code = NULL; //clear the https code
  }
}

.h

#ifndef TestCode_h
#define TestCode_h

#include "Arduino.h"

class testCode
{
  public:
    void generateCode(long statusCode);
    int storeHttpsCode (long code);
    long result;
  private:
};

#endif

Thanks very much,

Zeb