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?
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.
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.
#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.
Here is the full code for anyone with the same problem
.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