C语言 opencv 中未声明的“真”(首次在此函数中使用)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14376940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
‘true’ undeclared (first use in this function) in opencv
提问by shreya
Possible Duplicate:
Using boolean values in C
可能的重复:
在 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:
我是 C 的新手,想编写一个程序来从网络摄像头检测人脸,我在网上找到了一个,我在 eclipse CDT 上使用 opencv-2.4.3,我在网上搜索了解决方案,但没有得到合适的我的问题的解决方案,因此将其发布为新问题。这是代码:
// 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.
并且我收到错误为 true 未声明(第一次在此函数中使用),它在 while 循环中导致问题,我读到使用 while(true) 不是一个好习惯,但我应该怎么做。谁能帮帮我。
回答by LSerni
Replace it with e.g.
用例如替换它
while(1)
or
或者
for(;;)
or you can do (defining cbefore the loop):
或者你可以做(c在循环之前定义):
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 cat all, but this will startthe loop with a 20ms wait:
或者c根本没有,但这将启动循环并等待 20 毫秒:
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 linkto 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))
回答by UmNyobe
trueis not defined in many versions of c. If you want to use "boolean" see Using boolean values in C
true在许多版本的 c 中都没有定义。如果您想使用“boolean”,请参阅在 C 中使用布尔值
回答by Sue Spence
The C compiler is pointing out that the variable 'true'is not declared anywhere in your code nor in the header files that it includes. It's not part of the original C language specification. You may define it as a macro like so:
C 编译器指出变量“true”未在代码中的任何位置声明,也未在其包含的头文件中声明。它不是原始 C 语言规范的一部分。您可以将其定义为一个宏,如下所示:
#define true 1
#定义真1
however it's simpler and clearer to use while(1). If you need an event loop, this is way it's usually done. If it's 'not good practice' that's news to me.
然而,使用 while(1) 更简单、更清晰。如果你需要一个事件循环,这通常是这样做的。如果这不是“好的做法”,那对我来说就是新闻。
I keep forgetting about C99. You could also try adding
我一直忘记C99。您也可以尝试添加
#include <stdbool.h>
if your version of C supports it.
如果您的 C 版本支持它。

