Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

‘true’ undeclared (first use in this function) in opencv [duplicate]

Tags:

c

opencv

Possible Duplicate:
Using boolean values in C

I am newbie to C and want to write a program that will detect face from web cam, I got one on line,I am using opencv-2.4.3 on eclipse CDT, I searched on line for the solution but did not get the appropriate solution for my problem so posting it as new question.Here is the code:

 // Include header files
 #include "/home/OpenCV-2.4.3/include/opencv/cv.h"
 #include "/home/OpenCV-2.4.3/include/opencv/highgui.h"
 #include "stdafx.h"

 int main(){

//initialize to load the video stream, find the device
 CvCapture *capture = cvCaptureFromCAM( 0 );
if (!capture) return 1;

//create a window
cvNamedWindow("BLINK",1);

 while (true){
    //grab each frame sequentially
    IplImage* frame = cvQueryFrame( capture );
    if (!frame) break;

    //show the retrived frame in the window
    cvShowImage("BLINK", frame);

    //wait for 20 ms
    int c = cvWaitKey(20);

    //exit the loop if user press "Esc" key
    if((char)c == 27 )break;
}
 //destroy the opened window
cvDestroyWindow("BLINK");

//release memory
cvReleaseCapture(&capture);
return 0;
 }

And I am getting error as true’ undeclared (first use in this function), It is causing problem in while loop, I read it is not good practise to use while(true) but how should I go about. Can anybody hellp me out.

like image 402
shreya Avatar asked Oct 23 '25 11:10

shreya


2 Answers

Replace it with e.g.

while(1)

or

for(;;)

or you can do (defining c before the loop):

while (c != 27)
{
    //grab each frame sequentially
    IplImage* frame = cvQueryFrame( capture );
    if (!frame)
        break;
    //show the retrieved frame in the window
    cvShowImage("BLINK", frame);
    //wait for 20 ms
    c = cvWaitKey(20);
    //exit the loop if user press "Esc" key
}

or without c at all, but this will start the loop with a 20ms wait:

while (cvWaitKey(20) != 27)
{
    //grab each frame sequentially
    IplImage* frame = cvQueryFrame( capture );
    if (!frame)
        break;
    //show the retrieved frame in the window
    cvShowImage("BLINK", frame);
}

And a third possibility:

for(;;)
{
    //grab each frame sequentially
    IplImage* frame = cvQueryFrame( capture );
    if (!frame)
        break;
    //show the retrieved frame in the window
    cvShowImage("BLINK", frame);
    if (cvWaitKey(20) == 27)
        break;
}

UPDATE: while wondering whether it would be more correct to define

#define true  1
#define false 0

or

#define true 1
#define false (!true)

or again

#define false 0
#define true  (!false)

because if I, say, did:

int a = 5;
if (a == true) { // This is false. a is 5 and not 1. So a is not true }
if (a == false){ // This too is false. So a is not false              }

I would come up with a really weird result, I found this link to a slightly weirder result.

I suspect that to solve this in a safe way would require some macro such as

#define IS_FALSE(a)  (0 == (a))
#define IS_TRUE(a)   (!IS_FALSE(a))
like image 85
LSerni Avatar answered Oct 26 '25 06:10

LSerni


true is not defined in many versions of c. If you want to use "boolean" see Using boolean values in C

like image 20
UmNyobe Avatar answered Oct 26 '25 04:10

UmNyobe