Error with class destructor

Asked

Viewed 37 times

0

Good morning, I am trying to develop a matrix math engine, the error that is giving in my code is this:

R4DVector3n.cpp:(.text+0x226): undefined reference to `R4DEngine::R4DMatrix3n::~R4DMatrix3n()'

This is my code:

#include <iostream>

using namespace std;
namespace R4DEngine
{
  class R4DMatrix3n
  {

  private:
  public:
    //Matrix data elements
    float matrixData[9] = {0.0};

    //constructors
    R4DMatrix3n();

    R4DMatrix3n(float m0, float m3, float m6, float m1, float m4, float m7, float m2, float m5, float m8);

    //copy constructors
    R4DMatrix3n &operator=(const R4DMatrix3n &value);

    //destructors
    ~R4DMatrix3n();

    void show()
    {

      // 3x3 matrix - column major. X vector is 0, 1, 2, etc. (openGL prefer way)
      //    0    3    6
      //    1    4    7
      //    2    5    8

      std::cout << "[" << matrixData[0] << "," << matrixData[3] << "," << matrixData[6] << "," << std::endl;
      std::cout << matrixData[1] << "," << matrixData[4] << "," << matrixData[7] << "," << std::endl;
      std::cout << matrixData[2] << "," << matrixData[5] << "," << matrixData[8] << "]" << std::endl;
    }
  };
  R4DMatrix3n::R4DMatrix3n()
  {

    // 3x3 matrix - column major. X vector is 0, 1, 2, etc. (openGL prefer way)
    //    0    3    6
    //    1    4    7
    //    2    5    8

    for (int i = 0; i < 9; i++)
    {
      matrixData[i] = 0.0f;
    }

    matrixData[0] = matrixData[4] = matrixData[8] = 1.0f;
  };

  R4DMatrix3n::R4DMatrix3n(float m0, float m3, float m6, float m1, float m4, float m7, float m2, float m5, float m8)
  {

    // 3x3 matrix - column major. X vector is 0, 1, 2, etc. (openGL prefer way)
    //    0    3    6
    //    1    4    7
    //    2    5    8

    matrixData[0] = m0;
    matrixData[3] = m3;
    matrixData[6] = m6;

    matrixData[1] = m1;
    matrixData[4] = m4;
    matrixData[7] = m7;

    matrixData[2] = m2;
    matrixData[5] = m5;
    matrixData[8] = m8;
  };
}

And the main code is this:

#include <iostream>
#include "R4DMatrix3n.h"

int main(int argc, const char * argv[]) {

    //create an instance of R4DMatrix3n
    R4DEngine::R4DMatrix3n m(2,3,1,5,3,1,4,3,1);

    //Print the values of the matrix
    m.show();

    return 0;
}

How could I solve?

1 answer

0


Your code lacks destructor implementation. Substitute:

    ~R4DMatrix3n();

For:

    ~R4DMatrix3n() = default;

If you have a more complicated destructor, replace the default with the implementation.

  • Thank you, you answered right when I tried to do something similar, I put : ~R4dmatrix3n(){Cout << "Destroyed n";} ;?

  • So when you have an aggregate type (e.g. struct without private members with trivial copy) the compiler will give you a default constructor and destructor automatically. One of the ways to override this process is by defining a constructor, this signals to the compiler that for this structure the constructor/auto destructor does not serve. When vc uses the "default" specifier you make explicit pro compiler that will want the default constructor or destructor. If your matrix used dynamic allocation it would be interesting a custom destructor, but as its class is an aggregate type n is needed.

  • https://en.cppreference.com/w/cpp/language/destructor

Browser other questions tagged

You are not signed in. Login or sign up in order to post.