How to create library to get String value?

I want to create my library but I cannot use String in library. This is my code.

Iot.h

#ifdef Iot_h
#define Iot_h
#include "Arduino.h"

Class Iot
{
	public:
		
		Iot() {}
		int timeplus(String time);
		String getValueFromString(String data, char separator, int index)
	
};
#endif

Iot.cpp

#include "Iot.h"

String getValueFromString(String data, char separator, int index)
{
  int found = 0;
  int strIndex[] = {0, -1};
  int maxIndex = data.length()-1;

  for(int i=0; i<=maxIndex && found<=index; i++){
    if(data.charAt(i)==separator || i==maxIndex){
        found++;
        strIndex[0] = strIndex[1]+1;
        strIndex[1] = (i == maxIndex) ? i+1 : i;
    }
  }

  return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}

int Iot::timeplus(String time){
	
      String h_str = getValueFromString(time, ':', 0);
      String m_str = getValueFromString(time, ':', 1);
	  
      int h = h_str.toInt();
      int m = m_str.toInt();
	  int t = 0;
	  t = h + m;
	  
	  return t;
}

It show error like this.

C:\Users\PC\Documents\Arduino\libraries\Iot\Iot.cpp:33:1: error: 'String' does not name a type
   33 | String getValueFromString(String data, char separator, int index)
      | ^~~~~~
C:\Users\PC\Documents\Arduino\libraries\Iot\Iot.cpp:51:5: error: 'Iot' has not been declared
   51 | int Iot::timeplus(String time){
      |     ^~~~~~~
C:\Users\PC\Documents\Arduino\libraries\Iot\Iot.cpp:51:23: error: 'String' was not declared in this scope
   51 | int Iot::timeplus(String time){
      |                       ^~~~~~

I change String to char* then it show error like this.

C:\Users\PC\Documents\Arduino\libraries\Iot\Iot.cpp:57:18: error: request for member 'toInt' in 'h_str', which is of non-class type 'char*'
   57 |    int h = h_str.toInt();
      |                  ^~~~~
C:\Users\PC\Documents\Arduino\libraries\Iot\Iot.cpp:58:21: error: request for member 'toInt' in 'm_str', which is of non-class type 'char*'
   58 |       int m = m_str.toInt();
      |                     ^~~~~
        ^~~~~
C:\Users\PC\Documents\Arduino\libraries\Iot\Iot.cpp: At global scope:
C:\Users\PC\Documents\Arduino\libraries\Iot\Iot.cpp:66:5: error: 'Iot' has not been declared
   66 | int Iot::timeplus(int h, int m){

How to fix it?

should it be?

#ifndef Iot_h

if you failed to include Arduino.h String would not be defined

Thank you. It works

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