我创建了一个名为 [Perception_Element] 的对象
H 文件
#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() {}
然后我创建了一个继承它的类,称为 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()
{
}
为了更好的衡量,我调用的函数在这里定义
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;
}
}
}
现在我正在编写单元测试并尝试比较矩阵(二维向量),但它失败了
测试
/// <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 和 Neural_Node 都是 Perception_Element 的子类
我可以成功地将一个预测与另一个预测进行比较
但是当它被埋在向量的向量中时它就会失败
错误:
错误C2672'operator __surrogate_func':没有匹配的重载 找到函数了
我错过了什么?
当我将代码复制到 gcc 中,然后进行数百行更改时,我收到错误消息:
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
问题是你的
operator==
方法不是 const
,因此不能被非变异算法调用,比如向量的 operator==
。
正确的签名是:
bool operator==(const Perception& element) const
^^^^^ ^ ^^^^^