Error cannot call member function without object

Hi every one

Hereafter the error message that I do not understand.
Someone could help me please ?

Thanks in advance for any comment and/or correction.

There is the error message and the relative code to this error :
The error during compilation :
Packet.cpp:140:45: error: cannot call member function 'void I2C_Command::I2C_SendData(int, int, int*) volatile' without object
I2C_Command::I2C_SendData(2, 1, *dataToI2C[]) volatile ;

The code : (there are 3 parts)
1st part - In I2C_Command .h file :

#include "Arduino.h"

#ifndef I2C_Command_h
#define I2C_Command_h

struct I2C_Command {
  static void I2C_Scan();
  void I2C_SendData(int, int, int[]) ;
  void I2C_Request(int, int, int[]);

}; // I2C_Command

#endif

2nd part - In I2C_Command.cpp file :

//  Library
//#include <Wire.h>

//////////////////////////////////////////////////////////////////////////
#include "I2C_Command.h"

///////////////////////////////////////////////////////////////////////////////
 void I2C_Command::I2C_SendData(int I2Cadr, int I2Clenght, int I2Cdata[]) { 
  Wire.beginTransmission(I2Cadr); // transmit to device #2
  for (int i = 0; i <= I2Clenght; ++i) {
    Wire.write(I2Cdata[i]);   // sends one byte
  }
  Wire.endTransmission();    // stop transmitting
  Serial.println(I2Clenght + "I2C sent");
  for (int i = 0; i <= I2Clenght; ++i) {
    Serial.print(I2Cdata[i]);   // sends one byte
  }
  Serial.println();
}

///////////////////////////////////////////////////////////////////////////////
void I2C_Command::I2C_Request(int I2Cadr, int I2Clenght, int I2Cdata[]) {
  Wire.requestFrom(I2Cadr, I2Clenght);    // request int from slave device #8
  for (int i = 0; i < I2Clenght; ++i) {
    while (Wire.available()) { // slave may send less than requested
      I2Cdata[i] = Wire.read();
    }
  }
}

3rd part - In Packet.cpp file :

#include "Packet.h"
#include "I2C_Command.h"

///////////////////////////////////////////////////////////////////////////////
void PacketList::set(char *s) volatile {
  byte b[5];                      // save space for checksum byte
  int nR;
  int tC;
  int tS;
  int tD;
  int dataToI2C[20];

  if (sscanf(s, "%d %d %d %d", &nR, &tC, &tS, &tD) != 4)
    return;

  dataToI2C[0] = '1';
  I2C_Command::I2C_SendData(2, 1, dataToI2C) ;

} // PacketList::set()

You've already identified the problem in the title of this thread. You can't call a struct/class member function without specifying an object of that struct/class. What is your question?

Thank's for your remark.

my question is : how can I modified my code to have a success during compilation ?

Since Packet.h was not provided, I guessed what it might contain. Then I made the following changes (see comments).

#pragma once

#include "I2C_Command.h"   // Moved from "Packet.cpp".

struct PacketList {
  I2C_Command i2cCommand;  // Added `I2C_Command` object.
  void set(char*) volatile;
};

Now, if we change the following line,

to:

i2cCommand.I2C_SendData(2, 1, dataToI2C);

The sketch compiles (but it may still be non functional).

[edit]

Alternatively, you could have declared I2C_SendData static. Then your original syntax should work (but this only works if you have no plans to add state to your structs).

I have in Packet.h only :

#ifndef Packet_h
#define Packet_h

#include "Arduino.h"

struct PacketList{  
  void set(char *) volatile;
}

Thanks a lot for your contribution.

but like we say "sleep on it" ...
so to resolve this problem, I changed the program structure ...
The old program structure was

Main 
…
Call PacketList::set(char *s);

Packet.h
…
Struct PacketList {
…
Void set(char *) volatile;
…
}

Packet.cpp
#include "Packet.h"
#include "I2C_Command.h"

///////////////////////////////////////////////////////////////////////////////
void PacketList::set(char *s) volatile {
…
  I2C_Command::I2C_SendData(2, 1, dataToI2C) ;
…
} 

I2C_Command.h
…
struct I2C_Command {
…
  void I2C_SendData(int, int, int[]) ;
…
}

I2C_Command.cpp
//////////////////////////////////////////////////////////////////////////
#include "I2C_Command.h"
…

///////////////////////////////////////////////////////////////////////////////
 void I2C_Command::I2C_SendData(int I2Cadr, int I2Clenght, int I2Cdata[]) { 
…
}

The new program structure :

Main 
…
Call PacketList::set(char *s);
Call I2C_Command::set(char *s);

Packet.h
…
Struct PacketList {
…
Void set(char *) volatile;
…
}

Packet.cpp
#include "Packet.h"


///////////////////////////////////////////////////////////////////////////////
void PacketList::set(char *s) volatile {
…

…
} 

I2C_Command.h
…
struct I2C_Command {
…
  void I2C_Set(char *) volatile ;
  void I2C_SendData(int, int, int[]) volatile ;
…
}

I2C_Command.cpp
//////////////////////////////////////////////////////////////////////////
#include "I2C_Command.h"
…

///////////////////////////////////////////////////////////////////////////////
 void I2C_Command::I2C_Set(char *s) volatile { 
…
  dataToI2C[0] = '1';
  I2C_Command::I2C_SendData(2, 1, dataToI2C) volatile ;
…
}
///////////////////////////////////////////////////////////////////////////////
 void I2C_Command::I2C_SendData(int I2Cadr, int I2Clenght, int I2Cdata[]) volatile { 
…
}

and now it's OK :smile: :smile: :smile:

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