dimanche 12 janvier 2020

Can't seem to compile C++ code that uses std::sort using G++ -std=c++11

I'm revisiting some old code that I wrote using visual studio. It was a sort of simulation thing. Well, I have a bunch of lines of code that use the std::sort function, which compiled and executed back when I was using visual studio. However now I'm using linux with g++ and after using the command "g++ -std=c++11 main.cpp" it won't compile giving me the same error that there is no function std::sort.

Here is some of my code. Note that VEC is actually just std::vector, I like to do #define VEC std::vector I don't know why I like that, it just feels better for me.

void concBotInput(VEC<Player>& players, Map& map, size_t begin, size_t end, size_t inputSize)
{
    // Nodes need to be closer than 100.0 to be added to the input list if there are no nodes closer than than the 100 they will be filled with -1.0's

    VEC<long double> distancesC;
    VEC<long double> distancesXU;
    VEC<long double> distancesXD;
    VEC<long double> distancesYU;
    VEC<long double> distancesYD;
    VEC<long double> distancesZU;
    VEC<long double> distancesZD;
    VEC<long double> botInputs;

    for (size_t index = begin; index < end; index++)
    {
        /*VEC<std::thread> threads;
        threads.push_back(std::thread(concDistanceCalc, players[index].x, players[index].y, players[index].z, std::ref(map), std::ref(distancesC)));
        threads.push_back(std::thread(concDistanceCalc, players[index].x + 10.0, players[index].y, players[index].z, std::ref(map), std::ref(distancesXU)));
        threads.push_back(std::thread(concDistanceCalc, players[index].x - 10.0, players[index].y, players[index].z, std::ref(map), std::ref(distancesXD)));
        threads.push_back(std::thread(concDistanceCalc, players[index].x, players[index].y + 10.0, players[index].z, std::ref(map), std::ref(distancesYU)));
        threads.push_back(std::thread(concDistanceCalc, players[index].x, players[index].y - 10.0, players[index].z, std::ref(map), std::ref(distancesYD)));
        threads.push_back(std::thread(concDistanceCalc, players[index].x, players[index].y, players[index].z + 10.0, std::ref(map), std::ref(distancesZU)));
        threads.push_back(std::thread(concDistanceCalc, players[index].x, players[index].y, players[index].z - 10.0, std::ref(map), std::ref(distancesZD)));

        for (size_t subIndex = 0; subIndex < threads.size(); subIndex++)
            threads[subIndex].join();*/

        for (size_t subIndex = 0; subIndex < map.nodes.size(); subIndex++)
            distancesC.push_back(distance3D(players[index].x, players[index].y, players[index].z, map.nodes[subIndex].x, map.nodes[subIndex].y, map.nodes[subIndex].z));
        for (size_t subIndex = 0; subIndex < map.nodes.size(); subIndex++)
            distancesXU.push_back(distance3D(players[index].x + 10.0, players[index].y, players[index].z, map.nodes[subIndex].x, map.nodes[subIndex].y, map.nodes[subIndex].z));
        for (size_t subIndex = 0; subIndex < map.nodes.size(); subIndex++)
            distancesXD.push_back(distance3D(players[index].x - 10.0, players[index].y, players[index].z, map.nodes[subIndex].x, map.nodes[subIndex].y, map.nodes[subIndex].z));
        for (size_t subIndex = 0; subIndex < map.nodes.size(); subIndex++)
            distancesYU.push_back(distance3D(players[index].x, players[index].y + 10.0, players[index].z, map.nodes[subIndex].x, map.nodes[subIndex].y, map.nodes[subIndex].z));
        for (size_t subIndex = 0; subIndex < map.nodes.size(); subIndex++)
            distancesYD.push_back(distance3D(players[index].x, players[index].y - 10.0, players[index].z, map.nodes[subIndex].x, map.nodes[subIndex].y, map.nodes[subIndex].z));
        for (size_t subIndex = 0; subIndex < map.nodes.size(); subIndex++)
            distancesZU.push_back(distance3D(players[index].x, players[index].y, players[index].z + 10.0, map.nodes[subIndex].x, map.nodes[subIndex].y, map.nodes[subIndex].z));
        for (size_t subIndex = 0; subIndex < map.nodes.size(); subIndex++)
            distancesZD.push_back(distance3D(players[index].x, players[index].y, players[index].z - 10.0, map.nodes[subIndex].x, map.nodes[subIndex].y, map.nodes[subIndex].z));

        std::sort(distancesC.begin(), distancesC.end(), DistanceCompare());
        std::sort(distancesXU.begin(), distancesXU.end(), DistanceCompare());
        std::sort(distancesXD.begin(), distancesXD.end(), DistanceCompare());
        std::sort(distancesYU.begin(), distancesYU.end(), DistanceCompare());
        std::sort(distancesYD.begin(), distancesYD.end(), DistanceCompare());
        std::sort(distancesZU.begin(), distancesZU.end(), DistanceCompare());
        std::sort(distancesZD.begin(), distancesZD.end(), DistanceCompare());

        for (size_t subIndex = 0; subIndex < inputSize / 7; subIndex++)
        {
            if (distancesC[subIndex] <= 25.0)
            {
                botInputs.push_back(sigmoid(distancesC[subIndex]));
                botInputs.push_back(sigmoid(distancesXU[subIndex]));
                botInputs.push_back(sigmoid(distancesXD[subIndex]));
                botInputs.push_back(sigmoid(distancesYU[subIndex]));
                botInputs.push_back(sigmoid(distancesYD[subIndex]));
                botInputs.push_back(sigmoid(distancesZU[subIndex]));
                botInputs.push_back(sigmoid(distancesZD[subIndex]));
            }
            else
            {
                botInputs.push_back(-1.0);
                botInputs.push_back(-1.0);
                botInputs.push_back(-1.0);
                botInputs.push_back(-1.0);
                botInputs.push_back(-1.0);
                botInputs.push_back(-1.0);
                botInputs.push_back(-1.0);
            }
        }

        players[index].net.setInput(botInputs);

        distancesC.clear();
        distancesXU.clear();
        distancesXD.clear();
        distancesYU.clear();
        distancesYD.clear();
        distancesZU.clear();
        distancesZD.clear();
        botInputs.clear();
    }
}

and the error I'm getting is

Main.cpp: In function ‘void concBotInput(std::vector<Player>&, Map&, size_t, size_t, size_t)’:
Main.cpp:917:8: error: ‘sort’ is not a member of ‘std’
   std::sort(distancesC.begin(), distancesC.end(), DistanceCompare());
        ^~~~
Main.cpp:917:8: note: suggested alternative: ‘sqrt’
   std::sort(distancesC.begin(), distancesC.end(), DistanceCompare());
        ^~~~
        sqrt

I'm not sure what to do. Please forgive me if my question is ridiculously stupid or something.

Aucun commentaire:

Enregistrer un commentaire