0

I have the following code:

template<size_t N>
size_t sendProgmem(WiFiClient client, const String prog_char(&str)[N])
// size_t sendProgmem(WiFiClient client, const prog_char (&str)[N])
{
  return sendProgmem(client, str, N);
}

size_t sendProgmem(WiFiClient client, const String prog_char progmem[], size_t size) {
    size_t maxSize=2920; // this seems to be the max that write() will ever actually send
    size_t sent=0;

    while(sent < size) {
      size_t sendSize = (maxSize < (size - sent)) ? maxSize : (size - sent);
      sent += client.write(progmem+sent, sendSize); // write() may send less than you asked it to
    }
    return sent;
}

The original line (3rd line of code, now commented) produced an error:

 "ISO C++ forbids declaration with no type"

so I added String (see 2nd line of code).

Now I get,

error: expected ',' or '...' before '(' token
    2 | size_t sendProgmem(WiFiClient client, const String prog_char(&str)[N])

The original code is some 5+ years old. I'm not a C++ programmer so I don't even know where to start.

Any help as to where I can start looking or what is wrong with this syntax?

Also, what is the basic explanation of '(&str)[N]'?

markh
  • 59
  • 8

1 Answers1

3

Supposed prog_char is a type, the original signature was correct:

struct prog_char {};
struct WiFiClient {};

auto sendProgmem(auto,auto,auto){}

template<size_t N>
size_t sendProgmem(WiFiClient client, const prog_char(&str)[N])
{
  return sendProgmem(client, str, N);
}

Live Demo.

The function is taking an array of prog_char by reference. The size of the array, N, is the template argument and deduced from the parameter passed to the function. See Passing an array by reference for more on that (scroll down for an answer that also mentions deducing the size).

This

size_t sendProgmem(WiFiClient client, const String prog_char(&str)[N])

as well as this:

size_t sendProgmem(WiFiClient client, const String prog_char progmem[], size_t size) 

is wrong syntax. The compiler gets confused and spits out confusing error messages. Moreover String is not a standard C++ type.

The error message

"ISO C++ forbids declaration with no type"

for the original code can be caused by different things. It could be that prog_char is not declared, or that prog_char is declared but not as a type, or something else.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Thank you for your great explanations and help. I'll go back to the project and do some more digging - now that I have a better understanding. – markh Aug 23 '21 at 14:54