C++: The problem with constructor of class

  arduino-esp8266, c++, esp8266

I had declared the class with a static "vector"-object and cannot to find decision for constructor of class.

header:

#ifndef _SVD_PRGOBJ_H_
#define _SVD_PRGOBJ_H_


#include <stdint.h>
#include <Arduino.h>
#include <vector>

#define _SVD_PO_DEF_INDEX    0
#define _SVD_PO_NOTDEFINED   1
 
class CProgObj;

class CProgObj {
  protected:
     static std::vector<CProgObj*> global_progobjList;
     static CProgObj* getProgObjByIndex( uint16_t a_idx );
     static String& getJSONAll();

     static std::vector<uint8_t>  global_pins;
  public:
     static bool _init();
  protected:
    uint16_t index = _SVD_PO_DEF_INDEX; // The index of current object

  public:
    CProgObj();/*
      : index( global_progobjList.size() )
      {
        //index = global_progobjList.size();
        global_progobjList.push_back( this );
      }*/ 

    uint16_t getIndex(){ return index;};
    void setIndex( uint16_t a_idx){ index = a_idx; };

    
    virtual bool is_dev_in(){ return false;}; // must the object do some input of a data into process
    virtual bool is_dev_out(){ return false;}; // must the object do some input of a data into process
    virtual uint8_t switch_state(); // The function changing the current state to next one (if state exists)

    virtual String getJSON(){return "";}; 
    
};
#endif

Cpp

#include "svd_prjobj.h"
#include <vector>
#include <ESP8266WebServer.h>

std::vector<CProgObj*> CProgObj::global_progobjList;
    
std::vector<uint8_t>  CProgObj::global_pins;


CProgObj* CProgObj::getProgObjByIndex( uint16_t a_idx ){

   for( CProgObj* i : global_progobjList ){
     //CProgObj* obj = (CProgObj*)(*i);
     uint16_t idx = (*i).getIndex();
     if ( idx == a_idx )
        return i;
   }

   return nullptr;
}

String& CProgObj::getJSONAll(){

   bool need_comma = false;
   String ret = "[";
   for( CProgObj* i : global_progobjList ){
      //CProgObj* obj = (CProgObj*)(*i);
      String dev = (*i).getJSON();
      // If dev is emty, then don't need doing something
      if ( dev.length() != 0 ){
          if ( need_comma )
             ret += ',';
          need_comma = true;
      }
      //*
         
      ret += "{" + dev + "}";
   }
   ret += "]";
   return ret;
}


CProgObj::CProgObj(){
  
  //index = global_progobjList.size(); 
  //global_progobjList.push_back( this ); 
}

bool CProgObj::_init()
{
/*  if ( global_pins.size() > 0) return false;
  
  global_pins.push_back( 0 );
  global_pins.push_back( 2 );
  global_pins.push_back( 4 );
  global_pins.push_back( 5 );
  global_pins.push_back( 12 );
  global_pins.push_back( 13 );*/

  return true;
}

In the compilation-time shown follow error:

c:/users/svd/appdata/local/arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/3.0.4-gcc10.3-1757bed/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld.exe: C:UsersSvdAppDataLocalTemparduino_build_321565sketchsvd_prjobj.cpp.o:(.text._ZN8CProgObjC2Ev+0x0): undefined reference to `_ZTV8CProgObj'
collect2.exe: error: ld returned 1 exit status

If I have declare the constructor in the header -file like this:

....
  public:
    CProgObj();
      : index( global_progobjList.size() )
      {
        //index = global_progobjList.size();
        global_progobjList.push_back( this );
      } 
..

I receive follow error on the compilation:

c:/users/svd/appdata/local/arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/3.0.4-gcc10.3-1757bed/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld.exe: C:UsersSvdAppDataLocalTemparduino_build_321565sketchesp8266_smartdoze2.ino.cpp.o: in function `_ZN9__gnu_cxx13new_allocatorIP8CProgObjE10deallocateEPS2_j':
Z:Arduinoespesp8266_smartdoze2/esp8266_smartdoze2.ino:250: undefined reference to `_ZTV8CProgObj'
collect2.exe: error: ld returned 1 exit status

What is incorrect on my declaration?

Source: Windows Questions C++

LEAVE A COMMENT