variable or field declared void

I copied a well known function into my sketch and it started throwing errors. It makes me think there is a typo somewhere, but I have been staring at this for hours and cant see it. what could I be missing?

Error:

WIFI_Functions:3:20: error: variable or field 'sendNTPpacket' declared void

 void sendNTPpacket(IPAddress &address){

                    ^

WIFI_Functions:3:20: error: 'IPAddress' was not declared in this scope

WIFI_Functions:3:31: error: 'address' was not declared in this scope

 void sendNTPpacket(IPAddress &address){

                               ^

code:

// send an NTP request to the time server at the given address
void sendNTPpacket(IPAddress &address){
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12] = 49;
  packetBuffer[13] = 0x4E;
  packetBuffer[14] = 49;
  packetBuffer[15] = 52;
  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer, NTP_PACKET_SIZE);
  Udp.endPacket();
}
void wifiSetup() {
  WiFi.mode(WIFI_STA);
  WiFi.begin(configuration.ssid, configuration.pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  localIp = WiFi.localIP();
  Udp.begin(localPort);
  setSyncProvider(getNtpTime);
  setSyncInterval(300);
}

I have a seperate "includes.h" for all of my defines and variables, and here is the relevant part of it:

//WiFi
WiFiUDP Udp;
unsigned int localPort = 8888;  // local port to listen for UDP packets
IPAddress localIp;
time_t getNtpTime();
time_t prevDisplay = 0;
static const char ntpServerName[] = "us.pool.ntp.org";
void digitalClockDisplay();
void printDigits(int digits);
void sendNTPpacket(IPAddress &address);
const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets

I have a seperate "includes.h" for all of my defines and variables, and here is the relevant part of it:

But you don't include it in your code. Is it possible that you didn't post the complete code you're using? Please don't waste our time.

Perhaps not your problem here, but, generally speaking, you should NOT be DEFINING (i.e. assigning memory space to) variables in a .h file. You should only DECLARE them in the .h file.

This is because if the .h file in is included into more than one source file, you will have multiple definitions of the same variable. You should use the ‘extern’ keyword and do it like this:

In MyHeaderFile.h:

extern uint32 globalVariable;

In EVERY .cpp file:

#include "MyHeaderFile.h"

In ONE and ONLY ONE .cpp file:

uint32 globalVariable;

posting brief sections was my attempt to not waste anyone's time. My sketch is across about 10 ino files with the includes.h file also. and 800-1000 lines of code. I felt that If I posted that, it would be a tl;dr moment.

here is my complete includes.h file:

actually i got an error trying to post it:

The message has the following error or errors that must be corrected before continuing:
The message exceeds the maximum allowed length (9000 characters).

I read a while back when you have a large sketch, you could rename one of the ino's to .h so that it is loaded first.

another thing i read was to prepend a number or letter so that it loads in order. Is there an industry "standard" with large sketches?

When you’re dealing with 1000+ lines of code spread across multiple files, IMHO, an “Arduino Modification” of “industry standard” is much preferred to the method recommended in the Arduino documentation.

I only every have a single .ino file. It contains setup(), loop(), and whatever other functions it seems logical to have grouped in the same file with them. Related functions of an ancillary nature are put into their own .cpp files (with associated .h files). I use C language file-level scope control to limit variable and function scope to the absolute minimum needed to provide the desired functionality.

Additionally, in .h files I typically just put the #define(s), variable/function declarations, and class declarations to support the .cpp file. A .h file should only contain declarations that are necessary for functions in OTHER files to use the allowed resources of the associated .cpp file.

That’s (more or less) how it’s taught in college-level programming courses and done in industry.

You can zip your project and attach it. Include links to all non-standard libraries. Which board are you compiling for ?

One reason for your problem might be a missing semicolon or curly just before what you modified.

I was under the assumption that i am missing a semicolon or something, just cant seem to find it. I attached my entire code thus far. I removed some testing stuff (that was commented out anyway) so that all that is left is my core bits. the includes are this:

#include <Wire.h>
#include <Adafruit_ADS1015.h>
#include <Adafruit_MCP23008.h>
#include <TimeLib.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include "Nextion.h"
#include <PID_v1.h>
#include <EEPROM.h>
#include <OneWire.h>
#include <DallasTemperature.h>

oh and its for an esp8266 generic module. (ESP-07)

BC_ESP8266.zip (10.4 KB)

If I comment out the function, and the reference to it, everything compiles. If i take that function out and put it in a new sketch, it compiles fine there too.

This might be a case where Arduino’s build process drops a stitch. When you compile a sketch with multiple ino files, they are all combined first into one big sketch (double extension .ino.cpp); on a windows system, you can find it in the appdata directory (in my case C:\Users\sterretje\AppData\Local\Temp[u]arduino_build_785936[/u]\sketch\multiple_ino_test.ino.cpp).

You need to switch on show verbose output during compilation in the IDE menu file->preferences to find the exact directory; the underlined part (as well as the user name) will be different.

Next you can analyse that file and check where sendNtpPacket is located in that file; I can not quite advise here as I do not have ESP stuff installed so can’t build/compile to see what is happening and what to expect; you can zip that file and attach it to a post and I will see if I can find time to spot something odd.

One solution that might work is to add

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

at the top of your Wifi_Functions.ino. This assumes that IPAddress is defined in one of those two files, else you might have to dig trough all the esp stuff.

And you can obviously follow @gfvalvo’s advise and approach it the usual C/C++ way with header files and source (cpp) files; it will be a bit of work, but you will no longer depend on what the IDE’s build process creates before it compiles.

PS
Most of us don’t feel like hunting for 3rd party libraries on the web that are needed to be able to compile (and optionally test); it’s a waste of time and there is a risk that we find the wrong one. Hence you need to provide links to every single one of them and not a simple list.

Thank you for your response, and I apologize for not including links to the 3rd party libs. Truth is, besides the Nextion library, i used the library manager and didnt know where they are. I can google it, but figured that those were very common.

I looked at the cpp file, and also created a new sketch that is one ino file. I did it in the same manner that the IDE stitches together the multiple inos. I attached them here. By the way, the single ino file actually compiles.

That leads me to believe that my includes.h is the issue. So i changed the includes.h into aa_includes.ino and tried again to no avail.

Now upon googling a lot, I ran into “prototyping”.

there is a line of code in my “includes.h”:

void sendNTPpacket(IPAddress &address);

and i believe this is a prototype. would that mean that my function needs to be in an “includes.cpp” file also? instead of the wifi_functions.ino?

i dont yet get class structures in c++. (Im a c# developer primarily)

Brew_Controller_ESP8266.ino.cpp (38.4 KB)

sketch_sep25a.ino.cpp (36.4 KB)

moving the sndNTPPacket function from WiFi_functions.ino to the main ino fixed it.

now, the question is why?!

Compare the files that were created by the Arduino builder (ino.cpp).

When the C/C++ compiler sees a call to sendNtpPacket, it wants to check if the arguments that you pass are of the correct type. Therefore you either place that function before the place in your code where it is first used or you place a prototype there.

Usually the Arduino builder does placing of prototypes correctly but sometimes not. I suspect that that is the case here.

// function definition
void somefunc()
{
...
...
}

void loop()
{
  somefunc();
}

or

// prototype
void somefunc();

void loop()
{
  somefunc();
}
// function definition
void somefunc()
{
...
...
}

ok, getting to the bottom of this…

on line 118 of the non-compiling file, there is this:

void sendNTPpacket(IPAddress& address); //this is what the IDE did
#line 1 "C:\\Users\\hicot\\OneDrive\\Documents\\Arduino\\Brew_Controller_ESP8266\\Z_Main.ino"

then on line 160 it’s here again

//WiFi
void sendNTPpacket(IPAddress &address); //This is where I defined it
WiFiUDP Udp;

then on line 965 you see the actual function

void sendNTPpacket(IPAddress& address){
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12] = 49;
  packetBuffer[13] = 0x4E;
  packetBuffer[14] = 49;
  packetBuffer[15] = 52;
  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer, NTP_PACKET_SIZE);
  Udp.endPacket();
}

then online 1023 you see the usage of the function:

sendNTPpacket(ntpServerIP);

Hi There,
Stumbled across this answer to my same issue.
I used multiple tabs in my arduino IDE to neaten things up.
One of the tabs called PROBESENSORS kept failing with "variable or field declared void". Copy tab to new arduino project and it worked fine.
Ended up moving my INCLUDE statements to the original first tab and project then compiled fine.

icehousedoo:
Hi There,
Stumbled across this answer to my same issue.
I used multiple tabs in my arduino IDE to neaten things up.
One of the tabs called PROBESENSORS kept failing with "variable MyPrepaidCenter
or field declared void". Copy tab to new arduino project and it worked fine.
Ended up moving my INCLUDE statements to the original first tab and project then compiled fine.

yes

i will try this method too and will let you know if anything happens .

FYI: I had EXACTLY the same issue as OP.
Problems started when I renamed a tab. I tested all ideas in this thread without success.
My solution was to remove the function sendNTPpacket() and move the content into getNtpTime(). I also had to change the word address to ntpServerIP.
After that it compiled…

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