class method - problem with operations on arrays

Hello everyone,

I'm trying to make class method which operates on arrays (one is array of int, another char). Unfortunatelly I'm getting several errors/warnings
as an output from compiler. Can someone help :slight_smile: ? I'm mainli asking about errors: " invalid types 'int[int]' for array subscript"

Thanks in advance :slight_smile:

compiler output:

In function 'void loop()':

 warning: invalid conversion from 'int (*)[10]' to 'int' [-fpermissive]

           handleClient.parseRequest(incomming,&conParameters,&netParameters);

                                                                            ^



sketch\handleClient.h:17:10: note: initializing argument 2 of 'void handleClient::parseRequest(char*, int, char)'

     void parseRequest(char parametersArray[], int connectionParameters, char networkParameters);

          ^

warning: invalid conversion from 'char (*)[2]' to 'char' [-fpermissive]

           handleClient.parseRequest(incomming,&conParameters,&netParameters);

                                                                            ^



sketch\handleClient.h:17:10: note: initializing argument 3 of 'void handleClient::parseRequest(char*, int, char)'

     void parseRequest(char parametersArray[], int connectionParameters, char networkParameters);

          ^

sketch\handleClient.cpp: In member function 'void handleClient::parseRequest(char*, int, char)':

handleClient.cpp:71: error: invalid types 'int[int]' for array subscript

                     connectionParameters[conParArrayIndex]=atoi(command);

                                                          ^

handleClient.cpp:76: error: invalid types 'int[int]' for array subscript

                 connectionParameters[conParArrayIndex]=atoi(input);

                                                      ^

handleClient.cpp:79: error: invalid types 'char[int]' for array subscript

                 networkParameters[conParArrayIndex]=input;

                                                   ^

exit status 1
invalid types 'int[int]' for array subscript

main.ino (snipped)

#include "handleClient.h"

#define NUMBER_OF_INCOMMING 115

char incomming[NUMBER_OF_INCOMMING];
int conParameters[10];
char netParameters[2];




if(!handleClient.isSubmitted()){
  WiFiEspClient client = server.available();  // listen for incoming clients
  if (client) {
    int index=0;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if(index<numberOfIncomming){
          incomming[index]=c;
          index++;
        }
        if (c=="\n") {
          handleClient.parseRequest(incomming,&conParameters,&netParameters);
          break;
        }  
      } 
    }
  }   
}

handleClient.h

#include "Arduino.h"

class handleClient
{
public:
    handleClient();
    bool isSubmitted();
    void parseRequest(char parametersArray[], int connectionParameters, char networkParameters);

private:
    bool _submitted=false;
};

handleClient.cpp

#include "Arduino.h"
#include "handleClient.h"

handleClient::handleClient()
{

}

bool handleClient::isSubmitted(){
    return _submitted;
}

void handleClient::parseRequest(char parametersArray[], int connectionParameters, char networkParameters){

    char *input = parametersArray;
    String parameters[6]; //only 6 params can be injected to client
    char *token,*token2,*token3,*token4,*saved1,*saved2,*saved3,*saved4;
  
    token = strtok_r(input, "?",&saved1);

    while (token != NULL) {
      token2=strtok_r(token," ",&saved2);
      if(token2!=NULL){
        while(token2!=NULL){

          if(strcmp("/submit",token2)==0)
            _submitted=true; 
          
            token3=strtok_r(token2,"&",&saved3);
            while(token3!=NULL){
  
                if(strstr(token3,"selfIp")!=NULL)
                    parameters[0]=token3;
                if(strstr(token3,"selfPort")!=NULL)
                    parameters[1]=token3;
                if(strstr(token3,"hubIp")!=NULL)
                    parameters[2]=token3;
                if(strstr(token3,"hubPort")!=NULL)
                    parameters[3]=token3;
                if(strstr(token3,"newSsid")!=NULL)
                    parameters[4]=token3;
                if(strstr(token3,"newPass")!=NULL)
                    parameters[5]=token4;    
                   
              token3=strtok_r(NULL,"&",&saved3);
            }
          token2=strtok_r(NULL," ",&saved2);
        }
      }
      token = strtok_r(NULL,"?",&saved1);
    }

    if(_submitted){
        int conParArrayIndex=0;
        for(int i=0;i<6;i++){

            int len=(parameters[i].substring(parameters[i].indexOf("=")+1).length())+1;
            String par=parameters[i].substring(parameters[i].indexOf("=")+1);
            char input[len];
            par.toCharArray(input,len);
            
            if(i==0 || i==2){ //self IP || hubIP
                char* command = strtok(input, ".");
                while (command != NULL){
                    connectionParameters[conParArrayIndex]=atoi(command);
                    conParArrayIndex++;
                    command = strtok(NULL, ".");
                }
            }else if(i==1 || i==3){//selfPort || hubPort
                connectionParameters[conParArrayIndex]=atoi(input);
                conParArrayIndex++;
            }else if(i==4 || i==5){ //ssid || pass
                networkParameters[conParArrayIndex]=input;
                conParArrayIndex++;
            }
        }
    }
}

simple....

your function

void parseRequest(char parametersArray[], int connectionParameters, char networkParameters)

expects an int

you are trying to pass the address of the array.

handleClient.parseRequest(incomming,&conParameters,&netParameters);

BTW, it really helps if you include compilable examples, easier to offer a solution.

Is that means, in declaration I should place array address reference ? e.g. :

void parseRequest(char parametersArray[], int &connectionParameters, char &networkParameters)

BTW, it really helps if you include compilable examples, easier to offer a solution.

I included all I try to develop. I don't have compilable examples :confused:

Changing declaration as shown in previous post, brings different compiler error:

no matching function for call to 'handleClient::parseRequest(char [115], int (*)[10], char (*)[2])'

typically, you pass an array by pointer... along with its size...

class Example{
  public:
    void parseRequest(int* intArray, size_t size){
      for (size_t i = 0; i < size; i++)
      {
        Serial.println(intArray[i]);
      }
    }
};

Example example;

int someArray[10];

void setup() 
{
  Serial.begin(9600);
  example.parseRequest(someArray, sizeof(someArray)/sizeof(someArray[0]));
}

void loop() {
  // put your main code here, to run repeatedly:

}

you should learn and practice creating all of your function types before learning classes.

Ok, now it is working fine. Thanks for support and hints :slight_smile:

you should learn and practice creating all of your function types before learning classes.

Learning classes is not a problem, as I know it from higher level languages. In my case issues are typically related to memory alocation and all other embedded systems related aspects. I try practising it on examples.

One more time thanks for support.

// In handleClient.h:
    void parseRequest(char parametersArray[], int connectionParameters, char networkParameters);

// In main.cpp
char incomming[NUMBER_OF_INCOMMING];
int conParameters[10];
char netParameters[2];

          handleClient.parseRequest(incomming,&conParameters,&netParameters);

So you are declaring a function that takes:
1: An array of characters
2: a single integer
3: a single character

And you are passing to that function:
1: An array of characters (that works)
2: A pointer to an array of integers (NOT a single integer)
3: A pointer to an array of characters (NOT a single character)

That means you are either declaring you function wrong and it should be:

// In handleClient.h:
    void parseRequest(char parametersArray[], int connectionParameters[], char networkParameters[]);

or your usage is wrong and it should be something like:

// In main.cpp
char incomming[NUMBER_OF_INCOMMING];
int conParameters[10];
char netParameters[2];

          handleClient.parseRequest(incomming, conParameters[0], netParameters[0]);