Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error compiling .ino files with functions returning certain types #1683

Closed
cjastone opened this issue Jun 14, 2018 · 4 comments
Closed

Error compiling .ino files with functions returning certain types #1683

cjastone opened this issue Jun 14, 2018 · 4 comments
Labels
Milestone

Comments

@cjastone
Copy link

cjastone commented Jun 14, 2018

Configuration

Operating system: Windows 10

PlatformIO Version (platformio --version): 3.6.0a2

Description of problem

When building an .ino file with functions returning reference types, the file is not correctly preprocessed. If the functions are not declared in the order in which they are called, the build fails with "function was not declared in this scope". The same code compiles without error in the Arduino IDE.

Other types of functions are not affected by this and can be declared in any order without breaking anything.

Steps to Reproduce

  1. Create a function that returns a reference type (see below for an example)
  2. Create a function that calls the function that returns the reference type. Ensure this second function is declared before the function that it calls
  3. Build project - failure will result with "function not declared in this scope" error
  4. Swap order of functions so declaration occurs before call
  5. Build project - will build successfully.

Actual Results

bug_report.ino: In function 'void normalFunction()':
bug_report.ino:5:35: error: 'jsonFunction' was not declared in this scope
JsonObject& json = jsonFunction();

Expected Results

Project should build successfully.

If problems with PlatformIO Build System:

The content of platformio.ini:

[env:esp12e]
platform = espressif8266
board = esp12e
framework = arduino

[platformio]
src_dir = bug_report
lib_dir = ~\Documents\Arduino\libraries

Source file to reproduce issue:

#include "Arduino.h"
#include "ArduinoJson.h"

void normalFunction() {
  JsonObject& json = jsonFunction();
}

JsonObject& jsonFunction() {
  return JsonObject::invalid();
}

void setup() {}
void loop() {}

Additional info

Playing around, with this a bit more, I've discovered that this also affects functions returning a float.

@cjastone cjastone changed the title Error compiling .ino files with functions returning reference types Error compiling .ino files with functions returning certain types Jun 14, 2018
@ivankravets ivankravets added this to the 3.5.4 milestone Jun 15, 2018
ivankravets added a commit that referenced this issue Jun 15, 2018
* hotfix/v3.5.4:
  Fix preprocessor for Arduino sketch when function returns certain type // Resolve #1683
@ivankravets
Copy link
Member

Please re-test with pio upgrade --dev. Thanks!

@cjastone
Copy link
Author

cjastone commented Jun 17, 2018

Brilliant - thanks @ivankravets! Compiles without error now.

I really appreciate the fast response on this; keep up the amazing work!

@Legion2
Copy link

Legion2 commented Jun 6, 2020

This error still occurs if the & is aligned to the right:

#include "Arduino.h"
#include "ArduinoJson.h"

void normalFunction() {
  JsonObject &json = jsonFunction();
}

JsonObject &jsonFunction() {
  return JsonObject::invalid();
}

void setup() {}
void loop() {}

@ivankravets
Copy link
Member

Please use CPP instead INO if possible => https://docs.platformio.org/en/latest/faq.html#convert-arduino-file-to-c-manually

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants