I have the line

#include <algorithm>

at the top of a file, and the line

nth_element(minRollArray,minRollArray+n,minRollArray+numSets);

in one of the fuctions in that file. However, when I try to compile it I get the error
error C3861: 'nth_element': identifier not found
at that line. I would expect this if I hadn't included <algorithm> but I did include it. So why am I getting it?

Probably because you didn't specify the namespace. nth_element is an std function so is only defined in the std namespace.

U have some options:
- Use std::nth_element
- Put using std::nth_element; after including the header file
- Put using namespace std; after including the header file (not a very sexy solution).

// vc3.cpp : main project file.

#include "stdafx.h"
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
    int a=10,b=20;
   int swapr(&a,&b);
    printf("a=%d and b=%d",a,b);
    scanf("%d",&a);
}

   int swapr(int *x,int *y)
{
    int t;
    t=*x;
    *x=*y;
    *y=t;

}

i'm getting error here how to solve it...i'm new in this field,i m using vc++..thanku

Edited 3 Years Ago by Reverend Jim: Fixed formatting

i m getting error here how to solve it...i m new in this field,i m using vc++..thanku

Not sure why you posted it in this thread instead of creating a new one, but I'm happy to answer anyway

a few things:

  1. You need to declare the function swapr before it is used. You can do this by adding a function prototype above the main function (or, alternatively, by just putting the entire function definition up there, instead of below it).
  2. In main, when you call swapr, you simply use the name of the function, you don't need the return type as well.
  3. You don't have any return statement in the swapr function.
  4. The return type of swapr should be void, not int.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

This question has already been answered. Start a new discussion instead.