In C++, I have two shared_ptr. My expectation is that the content is the same for both…
- User
In C++, I have two shared_ptr. My expectation is that the content is the same for both of them, and I am tryin to Unit Test it using Catch2. The content is a struct with two string. In the REQUIRE of Catch2 I am comparing the content using * to retrieve the content of the shared_ptr, however I am getting error from compiler. Basically I have: error: ~no match for ‘operator==’ (operand types are ‘std::__shared_ptr_access<DAGRepresentation::Node, __gnu_cxx::_S_atomic, false, false>::element_type’ {aka ‘DAGRepresentation::Node’} and ‘std::__shared_ptr_access<DAGRepresentation::Node, __gnu_cxx::_S_atomic, false, false>::element_type’ {aka ‘DAGRepresentation::Node’}) 135 | REQUIRE(*symbolTable.getNode("osc1") == *node1); ~ DAGRepresentation::Node is the name of the struct. The log error is quite long, and I get even this part that seems relevant: ~ error: no type named ‘type’ in ‘struct std::enable_if<false, Catch::BinaryExpr<DAGRepresentation::Node&, DAGRepresentation::Node> >’ 2610 | using enable_if_t = typename enable_if<_Cond, _Tp>::type; ~ Please provide any relevant idea to fix this error, or at least to understand the reason of it.
- Assistant
The error you're encountering is due to the fact that your
DAGRepresentation::Nodestruct does not have an equality operator (operator==) defined. In C++, theREQUIREmacro from Catch2 will use theoperator==to compare the values, but if such an operator is not available for the types being compared, you'll get a compiler error.To fix this, you need to define the
operator==for yourDAGRepresentation::Nodestruct. Here's how you can do that:#include <string> namespace DAGRepresentation { struct Node { std::string first; std::string second; bool operator==(const Node& other) const { return first == other.first && second == other.second; } }; } // In your unit test file #include <memory> #include <catch2/catch.hpp> TEST_CASE("Node comparison") { using namespace DAGRepresentation; auto node1 = std::make_shared<Node>(); node1->first = "value1"; node1->second = "value2"; auto node2 = std::make_shared<Node>(); node2->first = "value1"; node2->second = "value2"; REQUIRE(*node1 == *node2); // This should now work as expected } - User
Only the Big Six are provided from the compiler in an automatic way? There is anything else that the compiler provides?
- Assistant
In C++, the term "Big Six" typically refers to the six special member functions that are automatically generated by the compiler if you don't explicitly declare them. These member functions are crucial for resource management and include:
- Default Constructor:
ClassName() - Destructor:
~ClassName() - Copy Constructor:
ClassName(const ClassName&)
- Default Constructor:
- User
I have a class with a unique_ptr. I am getting familiar to RAII. In this case, there is the need to explicitly call the destructor or it will be the destructor of the unique_ptr to release the memory? Moreover, if I define a class with a unique_ptr, the move constructor and move assignment are deleted by the inclusion of the unique_ptr?
- Assistant
- User
Got it. Instead, shared_ptr is both copyable and moveable?
- Assistant