arcy
arcy

Reputation: 13133

How am I supposed to declare this C++ function?

I have a C++ program with an undefined reference error on the call of a method in a class. As near as I can tell, the method is public and I'm calling it with a parameter declared the same as the method definition, but eclipse/gcc tells me it is undefined. I'm not used to C++, can someone please tell me what's wrong with the definition?

The main class:

#include <iostream>
using namespace std;

#include "AttenuationConfigurationTable.h"

int main()
{
  flash_entry_struct flash_array[] = { { 10, 20, 30, 40 }, { 50, 60, 70, 80 }, { -1, -1, -1, -1 } };

  // note: no undefined reference or any other error for the line with the class
  AttenuationConfigurationTable attConfigTable;
  // error appears for next line: undefined reference to ...load_attenuation_...
  attConfigTable.load_attenuation_calibration_table_from_flash(flash_array);
  return 0;
}

The class file:

#include "AttenuationConfigurationTable.h"
#include "flashEntryStruct.h"

AttenuationConfigurationTable::AttenuationConfigurationTable() {    }

AttenuationConfigurationTable::~AttenuationConfigurationTable() {    }

class Attenuation_configuration_table
{
  struct attenuation_voltages_struct
  {
    float att_value;
    float v1;
    float v2;
  } ;

  struct frequency_tables_struct
  {
    int frequency;
    attenuation_voltages_struct attenuation_voltages[100];
    int      voltages_count = 0;
  } ;

  frequency_tables_struct _frequency_tables[42];

  public:

  /************************************************************************/
  /* load the table in this object from the given flash memory address    */
  /************************************************************************/
  void load_attenuation_calibration_table_from_flash(flash_entry_struct memory_address[])
  {
    // bunch of logic here...
  }
};

The h file for the class:

#ifndef ATTENUATIONCONFIGURATIONTABLE_H_
#define ATTENUATIONCONFIGURATIONTABLE_H_

#include "flashEntryStruct.h"

class AttenuationConfigurationTable
{
public:
  AttenuationConfigurationTable();
  virtual ~AttenuationConfigurationTable();
  void load_attenuation_calibration_table_from_flash(flash_entry_struct flash_memory_address[]);
};

#endif /* ATTENUATIONCONFIGURATIONTABLE_H_ */

And, just for completeness, the h file defining the parameter structure:

#ifndef FLASHENTRYSTRUCT_H_
#define FLASHENTRYSTRUCT_H_

  struct flash_entry_struct
  {
    uint16_t  frequency;
    uint16_t  scaled_db;
    int8_t    v1_byte;
    int8_t    v2_byte;
  } ;

#endif /* FLASHENTRYSTRUCT_H_ */

EDIT: the error message itself:

Invoking: Cygwin C++ Linker
g++  -o "HelloCPP.exe" ./src/AttenuationConfigurationTable.o ./src/Hello2.o ./src/HelloCPP.o   
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: ./src/HelloCPP.o:/cygdrive/c/Users/ralph/files/programming/workspaces/HelloCPP/HelloCPP/Debug/../src/HelloCPP.cpp:15: undefined reference to `AttenuationConfigurationTable::load_attenuation_calibration_table_from_flash(flash_entry_struct*)'
collect2: error: ld returned 1 exit status
make: *** [makefile:58: HelloCPP.exe] Error 1
"make all" terminated with exit code 2. Build might be incomplete.

Upvotes: 0

Views: 98

Answers (1)

anthonyvd
anthonyvd

Reputation: 7590

You declare AttenuationConfigurationTable in the header file with the load_attenuation_calibration_table_from_flash function, but then the function with the same name in the implementation file is inside the definition for another class, Attenuation_configuration_table.

Take the implementation for load_attenuation_calibration_table_from_flash out of the class definition for Attenuation_configuration_table in your cpp file, and define it instead as

void AttenuationConfigurationTable::load_attenuation_calibration_table_from_flash(/* ... */) {
// ...
}

This is the same syntax already used for the constructor and destructor in that file.

In the end, your header should look something like this:

class AttenuationConfigurationTable
{
public:
  AttenuationConfigurationTable();
  virtual ~AttenuationConfigurationTable();
  void load_attenuation_calibration_table_from_flash(flash_entry_struct flash_memory_address[]);

private:
  struct attenuation_voltages_struct
  {
    float att_value;
    float v1;
    float v2;
  } ;

  struct frequency_tables_struct
  {
    int frequency;
    attenuation_voltages_struct attenuation_voltages[100];
    int voltages_count;
  } ;
  
  frequency_tables_struct _frequency_tables[42];
};

and your cpp file should look something like this:

AttenuationConfigurationTable::AttenuationConfigurationTable() {    }

AttenuationConfigurationTable::~AttenuationConfigurationTable() {    }

void AttenuationConfigurationTable::load_attenuation_calibration_table_from_flash(/* params */) {
  // body
}

Upvotes: 3

Related Questions