0

I'm reasonably new to c++. I'm currently trying to create and internet radio alarm clock on the ESP32. I've managed to play a station but I was wanting to test out changing between two stations. I've tried creating an array of Station structs to hold relevant info for each Station but I keep getting the same errors when trying to assign to the different elements in the array. It could be that it is quite a simple error but I can't figure it out and I can't see anything too similar anywhere else.

#include <Arduino.h>
#include "WiFi.h"
#include <Audio.h> 
#include <string.h>
#include <SPI.h> 
#define I2S_DOUT     25
#define I2S_BCLK      27
#define I2S_LRC        26  
Audio audio;

#define SSID ""
#define PASSWORD "" 



//function to change station    
void changeStat() {
  int statIndex = (statIndex+1)%2; 
};



void connectToWiFi(){
  Serial.print("Connecting to Wifi"); 
  WiFi.mode(WIFI_STA);
  WiFi.begin(SSID, PASSWORD);

  while(WiFi.status() != WL_CONNECTED){
    Serial.print('.'); 
    delay(1000); 
  }

  Serial.print("\n Connected \n");
  Serial.println(WiFi.localIP());
}

void setup() {
  Serial.begin(9600);   

  //Initialising array of station structures
  struct Station{
    char name[15];  
    char URL[130]; 
  };  

  Station Stats[2]; 
  int statIndex = 0;

  Stats[0] = {"BBC 6Music", "http://stream.live.vc.bbcmedia.co.uk/bbc_6music"};
  Stats[1] = {"BBC Rad4", "http://stream.live.vc.bbcmedia.co.uk/bbc_radio_fourlw"};

  connectToWiFi(); 
  audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT); 
  audio.setVolume(5); 
  audio.connecttohost(Stats[statIndex].URL);
}

void loop() {  

  audio.loop();
}

Specifically, the error I get on the lines Stats[0] and Stats[1] is no match for 'operator=' (operand types are 'setup()::Station' and '<brace-enclosed initializer list>') but I have seen lots of other people using this sort of assignment

FinSmy
  • 3
  • 2
  • [Cannot reproduce](https://coliru.stacked-crooked.com/a/e6dbfbc3de38c133) – YSC Jun 25 '21 at 08:51
  • Maybe if you had [specified you were compiling in C++98 mode...](https://coliru.stacked-crooked.com/a/004c42e1778517cd) – YSC Jun 25 '21 at 08:52
  • It depends on what standard you have configured your compiler (e.g. using command line options) to support. You're trying to use C++11 or later, but the code is invalid with standards predating C++11. For older compilers use `Stats[0] = Station("BBC 6Music", "http://stream.live.vc.bbcmedia.co.uk/bbc_6music")` instead (similarly for `Stats[1]`). Or, better, do it in the definition/initialisation of `Stats`, viz `Station Stats[2] = {{"BBC 6Music", "http://stream.live.vc.bbcmedia.co.uk/bbc_6music"}, {"BBC Rad4", "http://stream.live.vc.bbcmedia.co.uk/bbc_radio_fourlw"}};` – Peter Jun 25 '21 at 09:01

1 Answers1

1

This syntax uses initializer list, a feature added to C++ with it's 2011 version.

struct Station
{
    char name[15];  
    char URL[130]; 
};  

Station station = { "BBC 6Music", "http://stream.live.vc.bbcmedia.co.uk/bbc_6music" };

If you can, do switch to a more up to date compiler, at least handling C++14.

If not, you can still resort to C-style:

Station station;
strcpy(station.name, "BBC 6Music");
//...

An other solution would be to provide a user-defined constructor to Station:

struct Station
{
    char name[15];  
    char URL[130]; 
    Station(char const* _name, char const* _url) { /* ... */ }
};

Station station("BBC 6Music", "http://stream.live.vc.bbcmedia.co.uk/bbc_6music");
YSC
  • 38,212
  • 9
  • 96
  • 149
  • why "at least C++14" ? – 463035818_is_not_an_ai Jun 25 '21 at 09:03
  • 1
    C++14 is a fix of C++11. I wont recommand to switch to C++11 if C++14 is a possibility. C++17 or 20 are a possibility, but not a necessity. – YSC Jun 25 '21 at 10:34
  • Okay I've not really touched on constructors but I can kind of see how that works. Would you still say it's an easier route to update my compiler? I think it's a bit more elegant to store all of the stations in an array rather than have them floating. The thing is I'm using platformio so I am unsure of how to update the compiler with that. – FinSmy Jun 25 '21 at 11:10
  • @FinSmy Having `Station`s in an array or in free vraiables is almost orthogonal to the way it is constructed. If you intend to seriously code something in C++, get your hands on a [good book](https://stackoverflow.com/a/388282/5470596) and teach yourself how classes work. – YSC Jun 25 '21 at 11:38
  • I definitely will. I'm very interested in the field. That's why I'm so interested in getting stuck in and doing a project like this. As one last follow on question... in your opinion, what would be the best way of storing all of the stations then? – FinSmy Jun 25 '21 at 11:42
  • When unsure, go for `std::vector`. Only divert from that principle when you can explain why to a stubborn colleague. – YSC Jun 25 '21 at 11:57
  • Lovely. I'll give that a go. Cheers for all the help and advice. – FinSmy Jun 25 '21 at 11:59