xutility file?

16,313

Solution 1

I mentioned, you got function named "distance" in your code. So had I and I had been receiving the same error. Once I renamed this function to "Distance" my code was compiled successfully. My guess is that there is another function called "distance" defined somewere in xutility file. So the solution is just to rename the function.

Solution 2

You state that you're "trying to use c code with opencv", but your code contains #include <iostream>. That's a C++ header.

Now, there's no such thing as a C/C++ language. C and C++ are two distinct languages. You'll have to choose.

Solution 3

I guess that one of your C files includes a header file that is C++, for example:

#include <iostream>

A general approach for solving such issues is isolating the problem:

  1. Determine which source file is problematic
  2. Remove as much code as possible from that file while making sure the problem still appears
  3. Edit your question, showing that code

Solution 4

I happen to be reading the book The C++ Standard Template Library by Stepanov, Lee, Musser and Plauger. The implementation of the STL in that book uses several internal helper files, including xutility. xutility is not, itself, part of the C++ Standard, and I suspect that it serves the same purpose as the xutility in the book I'm reading: it's an implementation detail.

So what's the problem? The first five errors ("Missing type specifier") suggest some kind of syntax error. Unfortunately you haven't given us line numbers, so I can't say exactly what the syntax error is. I believe I've had this error when I either did not #include a type declaration or forgot to put the semicolon after a class definition.

The other errors may be related, but not necessarily. They all point to iterator_traits<>, which is part of the mechanism used by the STL to decide how to use an iterator efficiently. I've taken a quick look at the code sample you posted and I don't see any case where you use a standard container (there's an #include <vector> but no vector) or STL algorithm. I suspect that somewhere else in your code you're using the wrong syntax in a call to an STL algorithm (for instance, using CvPoint when you want CvPoint*) which is causing those errors.

Solution 5

Here is the solution:

Remove the line using namespace std. Then you get lots of undeclared identifier or syntax error : missing ';' before '<' errors. Add std:: namespace to every class and object you use from standard library.

When you put the using statement for the entire std namespace, then you can easily have a name conflict with the standard library classes and objects. Try to never put a using namespace std in you source file.

Share:
16,313
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm trying to use C code with opencv in face detection and counting, but I cannot build the source. I am trying to compile my project and I am having a lot of problems with a line in the xutility file.

    The error message shows that it is giving errors in the xutility file.

    Please help me solve this problem?

    Code

    // Include header files
    #include "stdafx.h"
    #include "cv.h"
    #include "highgui.h"
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <assert.h>
    #include <math.h>
    #include <float.h>
    #include <limits.h>
    #include <time.h>
    #include <ctype.h>
    #include <iostream>
    #include <fstream>
    #include <vector>
    
    using namespace std;
    
    
    #ifdef _EiC
    #define WIN32
    #endif
    
    int countfaces=0;
    int numFaces = 0;
    int k=0 ;
    int list=0;
    char filelist[512][512];
    
    int timeCount = 0;
    
    static CvMemStorage* storage = 0;
    static CvHaarClassifierCascade* cascade = 0;
    
    void detect_and_draw( IplImage* image );
    
    void WriteInDB();
    int found_face(IplImage* img,CvPoint pt1,CvPoint pt2);
    int load_DB(char * filename);
    
    
    const char* cascade_name = "C:\\Program Files\\OpenCV\\OpenCV2.1\\data\\haarcascades\\haarcascade_frontalface_alt_tree.xml"; 
    
    
    // BEGIN NEW CODE
    #define WRITEVIDEO
    char* outputVideo = "c:\\face_counting1_tracked.avi";
    //int faceCount = 0;
    int posBuffer = 100;
    int persistDuration = 10; //faces can drop out for 10 frames
    int timestamp = 0;
    float sameFaceDistThreshold = 30; //pixel distance
    CvPoint facePositions[100];
    int     facePositionsTimestamp[100];
    
    float distance( CvPoint a, CvPoint b ) {
        float dist = sqrt(float ( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) ) );
        return dist;
    }
    
    void expirePositions()
    {
        for (int i = 0; i < posBuffer; i++)
        {
            if (facePositionsTimestamp[i] <= (timestamp - persistDuration))    //if a tracked pos is older than three frames
            {
                facePositions[i] = cvPoint(999,999);
            }
        }
    }
    
    void updateCounter(CvPoint center) 
    {
        bool newFace = true;
        for(int i = 0; i < posBuffer; i++) 
        {
            if (distance(center, facePositions[i]) < sameFaceDistThreshold)
            {
                facePositions[i] = center;
                facePositionsTimestamp[i] = timestamp;
                newFace = false;
                break;
            }
        }
        if(newFace)
        {
            //push out oldest tracker
            for(int i = 1; i < posBuffer; i++) 
            {
                facePositions[i] = facePositions[i - 1];
            }
            //put new tracked position on top of stack
            facePositions[0] = center;
            facePositionsTimestamp[0] = timestamp;
            countfaces++;
        }
    }
    
    void drawCounter(IplImage* image) {
        // Create Font
        char buffer[5];
        CvFont font;
        cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, .5, .5, 0, 1);
        cvPutText(image, "Faces:", cvPoint(20, 20), &font, CV_RGB(0,255,0));
        cvPutText(image, itoa(countfaces, buffer, 10), cvPoint(80, 20), &font, CV_RGB(0,255,0));
    }
    #ifdef WRITEVIDEO
    CvVideoWriter* videoWriter = cvCreateVideoWriter(outputVideo, -1, 30, cvSize(240, 180));
    #endif
    //END NEW CODE
    
    int main( int argc, char** argv )
    {
    
        CvCapture* capture = 0;
        IplImage *frame, *frame_copy = 0;
        int optlen = strlen("--cascade=");
        const char* input_name;
    
        if( argc > 1 && strncmp( argv[1], "--cascade=", optlen ) == 0 )
        {
            cascade_name = argv[1] + optlen;
            input_name = argc > 2 ? argv[2] : 0;
        }
        else
        {
            cascade_name =  "C:\\Program Files\\OpenCV\\OpenCV2.1\\data\\haarcascades\\haarcascade_frontalface_alt_tree.xml"; 
    
            input_name = argc > 1 ? argv[1] : 0;
        }
    
        cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
    
        if( !cascade )
        {
            fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
            fprintf( stderr,
            "Usage: facedetect --cascade=\"<cascade_path>\" [filename|camera_index]\n" );
            return -1;
        }
        storage = cvCreateMemStorage(0);
    
        //if( !input_name || (isdigit(input_name[0]) && input_name[1] == '\0') )
        //    capture = cvCaptureFromCAM( !input_name ? 0 : input_name[0] - '0' );
        //else
    
        capture = cvCaptureFromAVI( "c:\\face_counting1.avi" ); 
    
        cvNamedWindow( "result", 1 );
    
        if( capture )
        {
            for(;;)
            {
                if( !cvGrabFrame( capture ))
                    break;
                frame = cvRetrieveFrame( capture );
                if( !frame )
                    break;
                if( !frame_copy )
                    frame_copy = cvCreateImage( cvSize(frame->width,frame->height),
                                                IPL_DEPTH_8U, frame->nChannels );
                if( frame->origin == IPL_ORIGIN_TL )
                    cvCopy( frame, frame_copy, 0 );
                else
                    cvFlip( frame, frame_copy, 0 );
    
                detect_and_draw( frame_copy );
    
                if( cvWaitKey( 30 ) >= 0 )
                    break;
            }
    
            cvReleaseImage( &frame_copy );
            cvReleaseCapture( &capture );
        }
        else
        {
            if( !input_name || (isdigit(input_name[0]) && input_name[1] == '\0'))
            cvNamedWindow( "result", 1 );
            const char* filename = input_name ? input_name : (char*)"lena.jpg";
            IplImage* image = cvLoadImage( filename, 1 );
    
            if( image )
            {
                detect_and_draw( image );
                cvWaitKey(0);
                cvReleaseImage( &image );
            }
            else
            {
                /* assume it is a text file containing the
                   list of the image filenames to be processed - one per line */
                FILE* f = fopen( filename, "rt" );
                if( f )
                {
                    char buf[1000+1];
                    while( fgets( buf, 1000, f ) )
                    {
                        int len = (int)strlen(buf);
                        while( len > 0 && isspace(buf[len-1]) )
                            len--;
                        buf[len] = '\0';
                        image = cvLoadImage( buf, 1 );
                        if( image )
                        {
                            detect_and_draw( image );
                            cvWaitKey(0);
                            cvReleaseImage( &image );
                        }
                    }
                    fclose(f);
                }
            }
    
        }
    
        cvDestroyWindow("result");
        #ifdef WRITEVIDEO
        cvReleaseVideoWriter(&videoWriter); 
        #endif
        return 0;
    }
    
    void detect_and_draw( IplImage* img )
    {
        static CvScalar colors[] = 
        {
            {{0,0,255}},
            {{0,128,255}},
            {{0,255,255}},
            {{0,255,0}},
            {{255,128,0}},
            {{255,255,0}},
            {{255,0,0}},
            {{255,0,255}}
        };
    
             double scale = 1.3;
            IplImage* gray = cvCreateImage( cvSize(img->width,img->height), 8, 1 );
             IplImage* small_img = cvCreateImage( cvSize( cvRound (img->width/scale),
                             cvRound (img->height/scale)),
                         8, 1 );
            CvPoint pt1, pt2;
            int i;
    
            cvCvtColor( img, gray, CV_BGR2GRAY );
            cvResize( gray, small_img, CV_INTER_LINEAR );
             cvEqualizeHist( small_img, small_img );
            cvClearMemStorage( storage );
    
            if( cascade )
                {
                double t = (double)cvGetTickCount();
                CvSeq* faces = cvHaarDetectObjects( small_img, cascade, storage,
                                                1.1, 2, 0/*CV_HAAR_DO_CANNY_PRUNING*/,
                                                cvSize(30, 30) );
                t = (double)cvGetTickCount() - t;
                printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) );
    
                if (faces)
                {
                //To save the detected faces into separate images, here's a quick and dirty code:
                char filename[6];
    
                for( i = 0; i < (faces ? faces->total : 0); i++ )
                 {
                    /* CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
                    CvPoint center;
                    int radius;
                    center.x = cvRound((r->x + r->width*0.5)*scale);
                    center.y = cvRound((r->y + r->height*0.5)*scale); 
                    radius = cvRound((r->width + r->height)*0.25*scale);
                    cvCircle( img, center, radius, colors[i%8], 3, 8, 0 );*/
                    // Create a new rectangle for drawing the face
                    CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
    
                    // Find the dimensions of the face,and scale it if necessary
                    pt1.x = r->x*scale;
                    pt2.x = (r->x+r->width)*scale;
                    pt1.y = r->y*scale;
                    pt2.y = (r->y+r->height)*scale;
    
                    // Draw the rectangle in the input image
                    cvRectangle( img, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
                    CvPoint center;
                    int radius;
                    center.x = cvRound((r->x + r->width*0.5)*scale);
                    center.y = cvRound((r->y + r->height*0.5)*scale); 
                    radius = cvRound((r->width + r->height)*0.25*scale);
                    cvCircle( img, center, radius, CV_RGB(255,0,0), 3, 8, 0 );
    
                    //update counter
                    updateCounter(center);
    
                    int y=found_face(img,pt1,pt2);
                    if(y==0)
                    countfaces++;
                }//end for
                printf("Number of detected faces: %d\t",countfaces);
    
        }//end if 
        //delete old track positions from facePositions array
        expirePositions();
        timestamp++;
    
        //draw counter
        drawCounter(img);
        #ifdef WRITEVIDEO
        cvWriteFrame(videoWriter, img);
        #endif
        cvShowImage( "result", img );
         cvDestroyWindow("Result");
        cvReleaseImage( &gray );
        cvReleaseImage( &small_img );
    }//end if
    } //end void
    
    
    int found_face(IplImage* img,CvPoint pt1,CvPoint pt2)
    {
            /*if (faces)
                {*/
            CvSeq* faces = cvHaarDetectObjects( img, cascade, storage,
                                                1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
                                                cvSize(40, 40) );
            int i=0;
            char filename[512];
             for( i = 0; i < (faces ? faces->total : 0); i++ )
              {//int scale = 1, i=0;
                //i=iface;
                //char filename[512];
    
                /* extract the rectanlges only */
                 //  CvRect face_rect = *(CvRect*)cvGetSeqElem( faces, i);
                CvRect face_rect = *(CvRect*)cvGetSeqElem( faces, i);
    
    
                //IplImage* gray_img = cvCreateImage( cvGetSize(img), IPL_DEPTH_8U, 1 );
                IplImage* clone = cvCreateImage (cvSize(img->width, img->height),
                                        IPL_DEPTH_8U, img->nChannels );
                IplImage* gray = cvCreateImage (cvSize(img->width, img->height),
                                        IPL_DEPTH_8U, 1 );
    
                cvCopy (img, clone, 0);
                cvNamedWindow ("ROI", CV_WINDOW_AUTOSIZE);
                cvCvtColor( clone, gray, CV_RGB2GRAY );
                face_rect.x = pt1.x;
                face_rect.y = pt1.y;
                face_rect.width = abs(pt1.x - pt2.x);
                face_rect.height = abs(pt1.y - pt2.y);
    
    
                 cvSetImageROI ( gray, face_rect);
                ////    * rectangle  = cvGetImageROI ( clone );
                face_rect = cvGetImageROI ( gray );
                cvShowImage ("ROI", gray);
                k++;
                char *name=0;
                 name=(char*) calloc(512, 1);
                sprintf(name, "Image%d.pgm", k);
                cvSaveImage(name, gray);
    
                ////////////////
                for(int j=0;j<512;j++)
                filelist[list][j]=name[j];
                list++;
                WriteInDB();
                //int found=SIFT("result.txt",name);
                cvResetImageROI( gray );
                //return found;
                return 0;
            //  }//end if
    
            }//end for
    }//end void
    
    void WriteInDB()
    {
    ofstream myfile;
    myfile.open ("result.txt");
    for(int i=0;i<512;i++)
    {
    if(strcmp(filelist[i],"")!=0)
    myfile << filelist[i]<<"\n";
    }
    myfile.close();
    }
    

    Error messages

    Error   3   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   
    Error   8   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   
    Error   13  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   c:\program files\microsoft visual studio 9.0\vc\include\xutility    766
    Error   18  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   c:\program files\microsoft visual studio 9.0\vc\include\xutility    768
    Error   23  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   c:\program files\microsoft visual studio 9.0\vc\include\xutility    769
    Error   10  error C2868: 'std::iterator_traits<_Iter>::value_type' : illegal syntax for using-declaration; expected qualified-name  c:\program files\microsoft visual studio 9.0\vc\include\xutility    765
    Error   25  error C2868: 'std::iterator_traits<_Iter>::reference' : illegal syntax for using-declaration; expected qualified-name   c:\program files\microsoft visual studio 9.0\vc\include\xutility    769
    Error   20  error C2868: 'std::iterator_traits<_Iter>::pointer' : illegal syntax for using-declaration; expected qualified-name c:\program files\microsoft visual studio 9.0\vc\include\xutility    768
    Error   5   error C2868: 'std::iterator_traits<_Iter>::iterator_category' : illegal syntax for using-declaration; expected qualified-name   c:\program files\microsoft visual studio 9.0\vc\include\xutility    764
    Error   15  error C2868: 'std::iterator_traits<_Iter>::difference_type' : illegal syntax for using-declaration; expected qualified-name c:\program files\microsoft visual studio 9.0\vc\include\xutility    766
    Error   9   error C2602: 'std::iterator_traits<_Iter>::value_type' is not a member of a base class of 'std::iterator_traits<_Iter>' c:\program files\microsoft visual studio 9.0\vc\include\xutility    765
    Error   24  error C2602: 'std::iterator_traits<_Iter>::reference' is not a member of a base class of 'std::iterator_traits<_Iter>'  c:\program files\microsoft visual studio 9.0\vc\include\xutility    769
    Error   19  error C2602: 'std::iterator_traits<_Iter>::pointer' is not a member of a base class of 'std::iterator_traits<_Iter>'    c:\program files\microsoft visual studio 9.0\vc\include\xutility    768
    Error   4   error C2602: 'std::iterator_traits<_Iter>::iterator_category' is not a member of a base class of 'std::iterator_traits<_Iter>'  c:\program files\microsoft visual studio 9.0\vc\include\xutility    764
    Error   14  error C2602: 'std::iterator_traits<_Iter>::difference_type' is not a member of a base class of 'std::iterator_traits<_Iter>'    c:\program files\microsoft visual studio 9.0\vc\include\xutility    766
    Error   7   error C2146: syntax error : missing ';' before identifier 'value_type'  c:\program files\microsoft visual studio 9.0\vc\include\xutility    765
    Error   22  error C2146: syntax error : missing ';' before identifier 'reference'   c:\program files\microsoft visual studio 9.0\vc\include\xutility    769
    Error   17  error C2146: syntax error : missing ';' before identifier 'pointer' c:\program files\microsoft visual studio 9.0\vc\include\xutility    768
    Error   2   error C2146: syntax error : missing ';' before identifier 'iterator_category'   c:\program files\microsoft visual studio 9.0\vc\include\xutility    764
    Error   12  error C2146: syntax error : missing ';' before identifier 'difference_type' c:\program files\microsoft visual studio 9.0\vc\include\xutility    766
    Error   6   error C2039: 'value_type' : is not a member of 'CvPoint'    c:\program files\microsoft visual studio 9.0\vc\include\xutility    765
    Error   21  error C2039: 'reference' : is not a member of 'CvPoint' c:\program files\microsoft visual studio 9.0\vc\include\xutility    769
    Error   16  error C2039: 'pointer' : is not a member of 'CvPoint'   c:\program files\microsoft visual studio 9.0\vc\include\xutility    768
    Error   1   error C2039: 'iterator_category' : is not a member of 'CvPoint' c:\program files\microsoft visual studio 9.0\vc\include\xutility    764
    Error   11  error C2039: 'difference_type' : is not a member of 'CvPoint'   c:\program files\microsoft visual studio 9.0\vc\include\xutility    766
    
  • MSalters
    MSalters over 8 years
    C++ has std::distance, which Microsoft may very well define in xutility. It shouldn't clash with ::distance because C++ has namespaces. However, the code above contains using namespace std;. Self-inflicted ambiguity, therefore. The solution is not to rename ::distance, the solution is to use namespaces as they were intended.