Home > Mobile >  How do you compare a 2d vector of custom objects in C
How do you compare a 2d vector of custom objects in C

Time:12-30

I created an object called [Perception_Element]

H FILE

#define PERCEPTION_ELEMENT_H

#include <Root.h>
using namespace Utilities;

namespace Perception {
    namespace Layer {
        namespace Element {
            class Perception_Element {
                public:
                    Perception_Element();

                    Perception_Element(float param_value);

                    void Set_Value(float param_value);

                    float Get_Value();

                    bool operator==(const Perception_Element element);

                    ~Perception_Element();

                private:
                    float value;

            };
        }
    }
}
#endif

CPP

#include <Perception_Element.h>

using namespace Perception::Layer::Element;

Perception_Element::Perception_Element() {}

Perception_Element::Perception_Element(float param_value) {
    this->value = param_value;
}

void Perception_Element::Set_Value(float param_value) {
    this->value = param_value;
}

float Perception_Element::Get_Value() {
    return this->value;
}

bool Perception_Element::operator==(const Perception_Element element) {
    return (this->Get_Value() == element.value);
}


Perception_Element::~Perception_Element() {}


And then I created a class that inherits from it called Prediction

H

#ifndef Prediction_H
#define Prediction_H

#include <Perception_Element.h>

using namespace Utilities;

namespace Perception {

    namespace Layer {
        namespace Element {
            class Prediction : public Perception_Element
            {
            public:
                Prediction();
                Prediction(float param_value);
                ~Prediction();
            };
        }
    }
}
#endif // !Prediction_H

CPP

#include <Prediction.h>

using namespace Perception::Layer::Element;
/// <summary>
/// Instantiate an empty Input for a Neural Node 
/// </summary>
Prediction::Prediction()
{
}

/// <summary>
/// Instantiate an Input for a Neural Node and assigns it a value for input
/// </summary>
/// <param name="param_value">float</param>
Prediction::Prediction(float param_value) : Perception_Element(param_value)
{
}



/// <summary>
/// destructor for input object
/// </summary>
Prediction::~Prediction()
{
}


Just for Good measure the function I call is defined here

void Layer::Dot_Product(vector<vector<Neural_Node>> param_inputs, vector<vector<Weight>> param_weights) {
    float results = 0;

    // iterate through the inputs rows to get the row index for the inputs and the outputs matrix
    for (int neural_node_row_index = 0; neural_node_row_index < param_inputs.size(); neural_node_row_index  ) {
            // iterate through the inputs columns to get the column index for the input and the output matrix
            for (int result_matrix_index = 0; result_matrix_index < param_inputs[neural_node_row_index].size(); result_matrix_index  ) {
                // iterate through the inputs columns to get the column index for the output matrix
                for (int neural_node_column_index = 0; neural_node_column_index < param_inputs[neural_node_row_index].size(); neural_node_column_index  ) {


                    /*
                      {    INPUT             {     OUTPUT            {        RESULT
                          {1, 2, 3, 4 }          {1, 2, 3, 4 }               {(INROW0, OUTROW0), (INROW0, OUTROW1), (INROW0, OUTROW2)}
                          {1, 2, 3, 4 }          {1, 2, 3, 4 }               {(INROW1, OUTROW0), (INROW1, OUTROW1), (INROW1, OUTROW2)}
                          {1, 2, 3, 4 }          {1, 2, 3, 4 }               {(INROW2, OUTROW0), (INROW2, OUTROW1), (INROW2, OUTROW2)}
                      }                      }                       }
                    */


                results  =
                    param_inputs[neural_node_row_index][neural_node_column_index].Get_Value()
                    *
                    param_weights[neural_node_row_index][neural_node_column_index].Get_Value();
            }
            this->predictions[neural_node_row_index][result_matrix_index].Set_Value(results);
            results = 0;

        }
     }
}

And now im writing unit testing and in trying to compare the matrix (2d vector) it fails

TEST


/// <summary>
/// Tests the dot product from the layer
/// </summary>
/// <param name=""></param>
/// <param name=""></param>
TEST(Perception_Test, Test_Layer_Prediction_without_bias) {
    Neural_Node node1(1);
    Neural_Node node2(2);
    Neural_Node node3(3);
    Neural_Node node4(4);

    Weight weight1(1);
    Weight weight2(2);
    Weight weight3(3);
    Weight weight4(4);

    Prediction prediction30(30);

    Layer test_layer(4);


    vector<vector<Neural_Node>> test_inputs = { 
        { node1, node2, node3, node4 },
        { node1, node2, node3, node4 },
        { node1, node2, node3, node4 },
        { node1, node2, node3, node4 }
    };

    vector<vector<Weight>> test_weights = {
        {weight1,weight2,weight3,weight4},
        {weight1,weight2,weight3,weight4},
        {weight1,weight2,weight3,weight4},
        {weight1,weight2,weight3,weight4}
    };

    vector<vector<Prediction>> comparison = { 
        {prediction30, prediction30, prediction30, prediction30},
        {prediction30, prediction30, prediction30, prediction30},
        {prediction30, prediction30, prediction30, prediction30},
        {prediction30, prediction30, prediction30, prediction30}
    };

    test_layer.Dot_Product(test_inputs, test_weights);
    
    vector<vector<Prediction>> prediction_without_bias =
                         test_layer.Get_Prediction_Without_Bias();

    bool b = (prediction_without_bias == comparison);
     
    //EXPECT_TRUE();
}

Weight, Prediction, and Neural_Node are all children classes of Perception_Element

I can successfully compare 1 prediction to another

But when it's buried in a vector of vectors it fails

ERROR:

Error C2672 'operator __surrogate_func': no matching overloaded function found

What am I missing?

CodePudding user response:

I think @Retired Ninja was right --

I changed my == operator to value == element.value and it seems to work now

If someone can tell me how to flag a answer as correct i think i have it now

CodePudding user response:

When I copy your code into gcc, and then make hundreds of lines of changes, I get the error message:

main.cpp:103:39:   required from here
/usr/local/include/c  /12.1.0/bits/stl_algobase.h:1161:29: error: no match for 'operator==' (operand types are 'const Perception::Layer::Element::Prediction' and 'const Perception::Layer::Element::Prediction')
 1161 |             if (!(*__first1 == *__first2))
      |                  ~~~~~~~~~~~^~~~~~~~~~~~~
main.cpp:45:6: note: candidate: 'bool Perception::Layer::Element::Perception_Element::operator==(Perception::Layer::Element::Perception_Element)' (near match)
   45 | bool Perception_Element::operator==(const Perception_Element element) {
      |      ^~~~~~~~~~~~~~~~~~
main.cpp:45:6: note:   passing 'const Perception::Layer::Element::Prediction*' as 'this' argument discards qualifiers

The problem is that your operator== method isn't const, and so cannot be called by non-mutating algorithms, like vector's operator==.

The correct signature is:

bool operator==(const Perception& element) const
                ^^^^^           ^          ^^^^^
  • Related