0

I'm working on a UE4 demo with C++ now. To do something with procedural mesh, I defined a custom class named Point, which was pretty simple. And to avoid creating duplicate points during some processes, I defined a static unordered_set to store cached points, with the key type of Point*.

#pragma once
#include <unordered_set>
#include <math.h>

namespace Grid
{
    class Point
    {
    public:
        float X;
        float Y;
        float Z;

    public:
        Point(float, float, float);
        static Point* CreatePoint(float, float, float);
        static void ClearCache();

    private:
        static size_t CachedNum;
        struct PointHash
        {
            size_t operator()(const Point* P) const
            {
                std::size_t HsX = std::hash<float>()(P->X);
                std::size_t HsY = std::hash<float>()(P->Y);
                std::size_t HsZ = std::hash<float>()(P->Z);
                return (HsX ^ (HsY << 1)) ^ HsZ;
            }
        };

        struct PointEqual
        {
            bool operator()(const Point* P1, const Point* P2) const
            {
                return (fabs(P1->X - P2->X) < 1e-6) && (fabs(P1->Y - P2->Y) < 1e-6) && (fabs(P1->Z - P2->Z) < 1e-6);
            }
        };

        static std::unordered_set<Point*, PointHash, PointEqual> CachedPoint;
    };
}

The simplified codes are above, I defined a custom hash func and equal func, but could not compile with the LNK 2001 error: unresolved external symbol "private: static class std::unordered_set<class Grid::Point *,struct Grid::Point::PointHash,struct Grid::Point::PointEqual,class std::allocator<class Grid::Point *> > Grid::Point::CachedPoint". Can anybody show me where the problem is?

With the empty .cpp file, the project can build successfully.

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
  • Yes you haven't **defined** `CachedPoint`, you have only **declared** it. You need to add the definition. This has nothing to do with maps, or custom hash functions, it's because `CachedPoint` is static. If `CachedPoint` was an integer you would have the same problem. – john Mar 12 '23 at 07:07
  • To the question in title, simply yes, and stdlib already define default `template< class T > struct hash;`, referring to [unordered_set](https://en.cppreference.com/w/cpp/container/unordered_set) and [hash](https://en.cppreference.com/w/cpp/utility/hash). – rustyhu Mar 12 '23 at 07:10
  • Thank you so much john. I forgot the most important step. – Krieg0726 Mar 12 '23 at 11:20
  • If this is meant to be used in unreal why don't you use unreals types like `TSet` and `FVector`? Otherwise, I don't see a point in tagging unreal at all. – Max Play Mar 12 '23 at 19:01

0 Answers0