Home > OS >  Solving a C2039 error and a C3861 error using std::minmax_element
Solving a C2039 error and a C3861 error using std::minmax_element

Time:11-07

I'm newer to C .

I've written the following line in a test function inside a standard VS2019 test project:

auto minAndMaxYards = std::minmax_element(simResults.begin(), simResults.end());

It yields both C2039 and C3861 errors for the minmax_element function even though intellisense recognizes it as a member of std, and I can peek its definition. I can't figure out what I'm missing. I've included the algorithm file as well at the top of the test project.

Is there a project setting that I don't have right?

Full error text:

C2039 'minmax_element': is not a member of 'std'
C3861 'minmax_element': identifier not found

Edit, including code in case it helps

#include <algorithm>

#include "pch.h"
#include "CppUnitTest.h"
#include "Playbook.h"
#include "PlaySim.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

std::string output;
using std::vector;

namespace FootballDynastyV20UnitTest
{
    TEST_CLASS(PlaybookIO)
    {
    public:

        TEST_METHOD(setAndGetPlayblookName)
        {
            Playbook testPlays;

            string testName = "testPlays";

            testPlays.setName(testName);
            string name = testPlays.getName();

            Assert::IsTrue(name == testName);

        }

        TEST_METHOD(addPlayIncrementsPlayNum)
        {
            Playbook testPlays;
            
            string playName = "Play1";
            int numDLine = 4;
            int numLB = 3;
            vector<int> playerPos = { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 19 };
            vector<int> playerStance = { 2, 1, 0, 0, 1, 2, 2, 3, 2, 3, 3 };
            vector<int> playerBlitzGaps = { 0, 3, 0, 0, 3, 0, 0, 0, 0, 0, 0 };


            testPlays.setName("testPlays");
            testPlays.addPlay(playName, numDLine, numLB, playerPos, playerStance, playerBlitzGaps);

            Assert::IsTrue(testPlays.getNumPlays() == 1);
        }

        TEST_METHOD(saveAndLoadPlayblook)
        {
            Playbook testPlays;
            Playbook testPlaysLoad;

            string playName = "Play1";
            int numDLine = 4;
            int numLB = 3;
            vector<int> playerPos = { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 19 };
            vector<int> playerStance = { 2, 1, 0, 0, 1, 2, 2, 3, 2, 3, 3 };
            vector<int> playerBlitzGaps = { 0, 3, 0, 0, 3, 0, 0, 0, 0, 0, 0 };


            testPlays.setName("testPlays");
            testPlays.addPlay(playName, numDLine, numLB, playerPos, playerStance, playerBlitzGaps);
            testPlays.save();

            testPlaysLoad.load(testPlays.getName());

            
            Assert::IsTrue(testPlays == testPlaysLoad);
        }
    };

    TEST_CLASS(PlaySimTesting)
    {
    public:

        TEST_METHOD(playSimReturnsYdsGainedBetweenNegative10And40)
        {
            PlaySim newPlay;
            int numSims = 2000;
            int lwrBound = -10;
            int uprBound = 40;
            vector<int> simResults;

            for (int i = 0; i < numSims; i  )
            {
                newPlay.Run();
                simResults.push_back(newPlay.GetYds());
            }
            
            auto minAndMaxYards = std::minmax_element(simResults.begin(), simResults.end());

            int actualMin = *minAndMaxYards.first;
            int actualMax = *minAndMaxYards.second;

            int yds = newPlay.GetYds();

            Assert::IsTrue((actualMin >= lwrBound) && (actualMax <= uprBound));

        }
    };
}

CodePudding user response:

Move #include "pch.h" to the top of the file. When using precompiled headers, the compiler ignores everything above this line. In your example, that would be #include <algorithm>, that's why std::minmax_element is not found.

  • Related