Trying to declare a pointer and assign object to it

Hi there -

Must be doing something obviously wrong but have tried and tried to fix it, no luck. Made a sandbox sketch to just get the problem spot fixed, and am still stuck. Have tried numerous iterations starting from the more clean and obvious, to the most cumbersome and explicit, still with no luck.

Am getting this error:
\Greenhouse\Sandbox_1\Sandbox_1.ino:13:1: error: 'p_Sensor_G' does not name a type
p_Sensor_G = &mySensor;
^~~~~~~~~~
exit status 1

Compilation error: 'p_Sensor_G' does not name a type

with this code [snippet]:

#include "GoB_Sensor.h"

GoB_Sensor* p_Sensor_G;

GoB_Sensor mySensor;

p_Sensor_G = &mySensor;

Welcome to the forum

Please post a full sketch and associated library files

1 Like

try

#include "GoB_Sensor.h"

GoB_Sensor mySensor;
GoB_Sensor* p_Sensor_G = &mySensor;

Thanks for the welcome. Ok, here goes:

Sandbox_1.ino

#include "DHT.h"
#include "Globals.h"
#include "GoB_Sensor.h"
//#include "GoB_Sensor_TempHumid.h"


//GoB_Sensor_TempHumid TempHumid_G("TempHumid Sensor", SENSOR_1, LOGGING_VERBOSITY, REPORTING_VERBOSITY, TESTING_INTENSITY);
GoB_Sensor* p_Sensor_G;
GoB_Sensor mySensor;
p_Sensor_G = &mySensor;
//GoB_Sensor sensorArray[0] = &Sensor_G;

// For the Adafruit shield, these are the default.
#define TFT_DC 9
#define TFT_CS 10
// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("Karaerin v1.1 Boot Sequence"); 
 
  tft.begin();
}

void loop() {
  // put your main code here, to run repeatedly:
  String sensorName = "Default";
  
  
  delay(1000);
  //sensorName = TempHumid_G.getName();
  sensorName = p_Sensor_G->getName();
  tft.print(F("Sensor Name: ")); tft.println(sensorName);
  delay(5000);
  
}

GoB_Sensor.h:

#ifndef _GOBSENSOR_H_
#define _GOBSENSOR_H_

#include "Globals.h"
#include "Arduino.h"


class GoB_Sensor {
  private:
  String myName = "Generic Sensor";
  uint8_t myID = 0;
  uint8_t myLoggingVerbosity = 0;
  uint8_t myReportingVerbosity = 0;
  uint8_t myTestingIntensity = 0;

  public:
  String getName();
  uint8_t setName(String nameString); //Returns SUCCESS on success
  uint8_t setID(uint8_t sensorID); //Returns SUCCESS on success
  uint8_t getID ();
  uint8_t setLoggingVerbosity(uint8_t loggingVerbosity); //Returns SUCCESS on success
  uint8_t getLoggingVerbosity();
  uint8_t setReportingVerbosity(uint8_t reportingVerbosity); //Returns SUCCESS on success
  uint8_t getReportingVerbosity(); 
  uint8_t setTestingIntensity(uint8_t TestingLevel); //Returns SUCCESS on success
  uint8_t getTestingIntensity(); 

  virtual uint8_t m_SetupSensor(DHT &r_MySensor);
  virtual float m_ReadSensor(int mySensor, int tempMode);
  
  GoB_Sensor();
  GoB_Sensor(uint8_t sensorID, uint8_t verbosityLevel, uint8_t reportingLevel, uint8_t testingLevel);
  //~GoB_Sensor(); //TO-DO: Figure out how to make =0 w/o being Abstract Class
};

#endif

GoB_Sensor.cpp:

#include "GoB_Sensor.h"
//#include "GoB_Sensor_TempHumid.h"

  uint8_t m_SetupSensor(DHT &r_MySensor)
  {
    //Empty
  }

  float m_ReadSensor(int mySensor, int tempMode)
  {
    //Empty
  }

  String GoB_Sensor::getName()  {
    return myName;
  }

  uint8_t GoB_Sensor::setName(String nameString)  {
    myName = nameString;
    return SUCCESS;
  }

  uint8_t GoB_Sensor::setID(uint8_t sensorID) {
    myID = sensorID;
    return SUCCESS;
  }

  uint8_t GoB_Sensor::getID () {
    return myID;
  }

  uint8_t GoB_Sensor::setLoggingVerbosity(uint8_t loggingVerbosity) {
    myLoggingVerbosity = loggingVerbosity;
    return SUCCESS;
  }

  uint8_t GoB_Sensor::getLoggingVerbosity() {
    return myLoggingVerbosity;
  }

  uint8_t GoB_Sensor::setReportingVerbosity(uint8_t reportingVerbosity) {
    myReportingVerbosity = reportingVerbosity;
    return SUCCESS;
  }

  uint8_t GoB_Sensor::getReportingVerbosity() {
    return myReportingVerbosity;
  }

uint8_t GoB_Sensor::setTestingIntensity(uint8_t testingLevel) {
    myTestingIntensity = testingLevel;
    return SUCCESS;
  }

  uint8_t GoB_Sensor::getTestingIntensity() {
    return myTestingIntensity;
  }

 GoB_Sensor::GoB_Sensor() { 
  myName = "Generic Sensor";
  myID = 0;
  myLoggingVerbosity = 0;
  myReportingVerbosity = 0;
  myTestingIntensity = 0;
 }

 GoB_Sensor::GoB_Sensor(uint8_t sensorID, uint8_t loggingLevel, uint8_t reportingLevel, uint8_t testingLevel) {
  myName = "Generic Parameterized Sensor 1";
  myID = sensorID;
  myLoggingVerbosity = loggingLevel;
  myReportingVerbosity = reportingLevel;
  myTestingIntensity = testingLevel;
 }

/* GoB_Sensor::~GoB_Sensor()
 {
  //empty
 }
 */

Globals.h:

#ifndef _GLOBALS_H_

#ifndef _GLOBALS_H_
#define _GLOBALS_H_

#include "DHT.h" //All sensor definitions must be global.
#define LOGGED_SAMPLE_SIZE 5

// GoB defines
#define SUCCESS 1
#define FAILURE 0
#define FAHRENHEIT 1
#define CELSIUS 0


//[SNIP--misc #defines]
#endif

Have done that before. Got different error. Tried again to demonstrate, got the vtable error.

/GoB_Sensor_TempHumid TempHumid_G("TempHumid Sensor", SENSOR_1, LOGGING_VERBOSITY, REPORTING_VERBOSITY, TESTING_INTENSITY);

//GoB_Sensor* p_Sensor_G;

//GoB_Sensor mySensor;

//p_Sensor_G = &mySensor;

GoB_Sensor mySensor;

GoB_Sensor* p_Sensor_G = &mySensor;

Got this error:
C:\Users\mydig\AppData\Local\Temp\cceBU1fV.ltrans0.ltrans.o: In function global constructors keyed to 65535_0_GoB_Sensor.cpp.o.4107': <artificial>:(.text.startup+0x60): undefined reference to vtable for GoB_Sensor'
:(.text.startup+0x62): undefined reference to vtable for GoB_Sensor' C:\Users\mydig\AppData\Local\Temp\cceBU1fV.ltrans0.ltrans.o: In function __base_dtor ':
C:\Users\mydig\Documents\Commercial Projects\Greenhouse\Sandbox_1/GoB_Sensor.h:8: undefined reference to vtable for GoB_Sensor' C:\Users\mydig\Documents\Commercial Projects\Greenhouse\Sandbox_1/GoB_Sensor.h:8: undefined reference to vtable for GoB_Sensor'
collect2.exe: error: ld returned 1 exit status
exit status 1

Compilation error: exit status 1

Have also rebooted computer, no luck. Maybe reinstall the IDE?

I've fixed your code tags for you; it was a bit of a mess.

1 Like

Thank you, yeah couldn't get it to handle discontiguous code very well.

Your code as shown did not compile.

Add #include <Adafruit_ILI9341.h> to your .ino file.

Add #define SUCCESS true to Gob_Sensor.h.

Change

GoB_Sensor* p_Sensor_G;
GoB_Sensor mySensor;
p_Sensor_G = &mySensor;

to

GoB_Sensor mySensor;
GoB_Sensor* p_Sensor_G = &mySensor;

Change

  virtual uint8_t m_SetupSensor(DHT &r_MySensor);
  virtual float m_ReadSensor(int mySensor, int tempMode);

to

  uint8_t m_SetupSensor(DHT &r_MySensor);
  float m_ReadSensor(int mySensor, int tempMode);

And set your compile warning level to ALL to see the other things that, while aren't fatal for compilation, will be for proper execution.

Hmmm maybe didn't include that in my code, but thought I gave all the includes. Anyway, that is already in there, here. UPDATE: See that indeed I did clip the top 3 lines when pasting. But yeah, that library is in the compile [here].

Not sure what you mean about SUCCESS, but it is defined as 1 in the Globals.h file. What are you seeing with it? Can you elaborate?

Also, those virtual functions need to stay virtual as will be overridden in derived classes.

Lastly, how do I set compile warning to ALL? UPDATE: Found it, and set to ALL plus verbose for compile. :-)

#include "SPI.h"

#include "Adafruit_GFX.h"

#include "Adafruit_ILI9341.h"

#include "DHT.h"

#include "Globals.h"

#include "GoB_Sensor.h"

//#include "GoB_Sensor_TempHumid.h"

No, it's not.

#ifndef _GLOBALS_H_

#define _GLOBALS_H_

#include "DHT.h" //All sensor definitions must be global.

#define LOGGED_SAMPLE_SIZE 5

// GoB defines

//[SNIP--misc #defines]
#endif

I invite you to take the code in post #4, copy and paste each file, name them appropriately, and see for yourself.

Ok, here is the verbose ALL output with the one more typical declaration & define.

GoB_Sensor mySensor;

GoB_Sensor* p_Sensor_G = &mySensor;

reference to vtable for GoB_Sensor' C:\Users\mydig\Documents\Commercial Projects\Greenhouse\Sandbox_1/GoB_Sensor.h:8: undefined reference to vtable for GoB_Sensor'
collect2.exe: error: ld returned 1 exit status
Using library SPI at version 1.0 in folder: C:\Users\mydig\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\SPI
Using library Adafruit GFX Library at version 1.12.4 in folder: C:\Users\mydig\Documents\Arduino\libraries\Adafruit_GFX_Library
Using library Adafruit BusIO at version 1.17.4 in folder: C:\Users\mydig\Documents\Arduino\libraries\Adafruit_BusIO
Using library Wire at version 1.0 in folder: C:\Users\mydig\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire
Using library Adafruit ILI9341 at version 1.6.2 in folder: C:\Users\mydig\Documents\Arduino\libraries\Adafruit_ILI9341
Using library DHT sensor library at version 1.4.6 in folder: C:\Users\mydig\Documents\Arduino\libraries\DHT_sensor_library
Using library Adafruit Unified Sensor at version 1.1.15 in folder: C:\Users\mydig\Documents\Arduino\libraries\Adafruit_Unified_Sensor
exit status 1

Compilation error: exit status 1

You did not follow all the instructions in post #9. If you had, your code that you showed would have compiled and linked, as it did for me.

arduino-cli compile -b arduino:avr:uno --warnings all --output-dir ~/tmp --no-color (in directory: /home/me/Documents/sketchbook/Uno_R3/test)
Sketch uses 15680 bytes (48%) of program storage space. Maximum is 32256 bytes.
Global variables use 498 bytes (24%) of dynamic memory, leaving 1550 bytes for local variables. Maximum is 2048 bytes.
Compilation finished successfully.

Yeah, I need the virtual and was requesting more info on the SUCCESS, what you're seeing as an issue (since it is defined in Globals.h) and not used in the sandbox code.

Your code, as shown, does not need virtual on those methods.

Your code, as shown, does not include a definition of SUCCESS in Globals.h, or anywhere else.

Since your basis of your issues seem to exist in code that is not shown, I will withdraw and wish you good luck.

I pulled the code out for the sandbox. It is needed for the project. SUCCESS is not used in this sandbox, but is defined in Globals.h.

The SUCCESS is defined as part of the [SNIP--misc #defines] sorry. But it is in the code on my end.

#ifndef _GLOBALS_H_

#define _GLOBALS_H_

#include "DHT.h" //All sensor definitions must be global.

#define LOGGED_SAMPLE_SIZE 5

// GoB defines

#define SUCCESS 1

#define FAILURE 0

#define FAHRENHEIT 1

#define CELSIUS 0

Ok, edited the original Sketch post at the top, so that the SUCCESS constant is visible.

@happyrick

Which Arduino board are you using?

MEGA2560. But I can't compile the code to upload it, so is not a factor.