如何在函数中返回多个变量? [英] How to return many variable in function?

查看:106
本文介绍了如何在函数中返回多个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void CallBackFunc(int event,int x,int y,int flags,void * userdata)

{

if(event == EVENT_LBUTTONDOWN)

{

cout<< 点击鼠标的左键 - 位置(<<<<<<,<<<<<)<< endl;

}

else if(event == EVENT_RBUTTONDOWN)

{

cout<< 点击鼠标右键 - 位置(<<<<<<,<<<<<)<< endl;

}

else if(event == EVENT_MBUTTONDOWN)

{

cout<< 点击鼠标的中间按钮 - 位置(<<<<<,<<<<<)<< endl;

}

else if(event == EVENT_MOUSEMOVE)

{

cout<< 鼠标在窗口上移动 - 位置(<< x<<,<< y<<)<<结束;



}

//返回问题

return(x,y,event,flags)< br $>
}



int main(int argc,char ** argv)

{

//从文件中读取图像

Mat img = imread(。\\resources\\Data1.JPG);



//如果无法阅读图片

if(img.empty())

{

cout<< 加载图像时出错<< endl;

返回-1;

}



//创建一个窗口

namedWindow(我的窗口,1);



//为任何鼠标事件设置回调函数

setMouseCallback(我的窗口,CallBackFunc,NULL);



//显示图片

imshow(我的窗口,img);

Mat cropedImage = img(Rect(x1, y1,90,100));

imshow(Window1,cropedImage);



//等到用户按某个键

waitKey(0);



返回0;

}

void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{
if ( event == EVENT_LBUTTONDOWN )
{
cout << "Left button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
}
else if ( event == EVENT_RBUTTONDOWN )
{
cout << "Right button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
}
else if ( event == EVENT_MBUTTONDOWN )
{
cout << "Middle button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
}
else if ( event == EVENT_MOUSEMOVE )
{
cout << "Mouse move over the window - position (" << x << ", " << y << ")" << endl;

}
//Return problem
return(x,y,event,flags)
}

int main(int argc, char** argv)
{
// Read image from file
Mat img = imread(".\\resources\\Data1.JPG");

//if fail to read the image
if ( img.empty() )
{
cout << "Error loading the image" << endl;
return -1;
}

//Create a window
namedWindow("My Window", 1);

//set the callback function for any mouse event
setMouseCallback("My Window", CallBackFunc, NULL);

//show the image
imshow("My Window", img);
Mat cropedImage = img(Rect(x1,y1,90,100));
imshow ("Window1", cropedImage);

// Wait until user press some key
waitKey(0);

return 0;
}

推荐答案

通常,有两种方法:

Generally, there are two ways:
  1. 创建用户定义的类型,类或结构,并将其用作类型功能回归。这种类型可以由几种原始类型的值组成,也可以更复杂。
  2. 严格来说,第二种方法不是返回,但可以用于完全相同的目的。通过(非常数)引用传递参​​数,这允许您修改功能内的值:

    http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference [ ^ ]
  1. Create user-defined type, class or structure, and use it as a type of function return. Such type can be composed of several values of primitive types or be more complex.
  2. Second method is, strictly speaking, not return, but can used for exact same purpose. Pass parameters by (non-constant) reference, which allows you to modify the value inside the funtion:
    http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference[^]





请注意,从低级别的角度来看,通过引用传递某种类型的参数与传递值相同,该参数的类型是指向相同的类型。从语言和编程学科的角度来看,这是一个非常不同的事情。特别是,使用指针,您可以(例如,意外地)传递空指针或一些无效指针。通过引用,C ++编译器可以保护您免受此类事件的侵害。



-SA


而SAs解决方案是对主题标题中提出的问题的完美答案,您发布的代码暗示了一个完全不同的问题:如何将结果值从回调函数返回到......?



这个问题的答案是:你没有!事件处理函数的要点是处理输入数据,即。即做你想做的事。您不会从事件处理程序返回结果。这就是为什么他们的返回类型是 void 。无论如何:想想谁叫那个事件处理程序!你不这样做 - 所以你怎么看这些结果值呢?



所以要么你把实际工作的代码放到事件处理程序中,要么你将它放入一个单独的函数中,并从事件处理程序中调用该函数 - 在后一种情况下,您只需将多个值(x,y,event,flags)作为函数参数传递给该函数。
While SAs solution is a perfect answer to the question posed in your topic heading, the code you posted hints at an entirely different problem: "how do I return result value(s) from a callback function to ... ?"

The answer to that question is: "you don't"! The point of an event handler function is to 'handle' the input data, i. e. do whatever you meant to do. You don't return results from an event handler. That is why their return type is void. Anyway: think about who calls that event handler! You don't - so how would you read those result values anyway?

So either you put your code that does the actual work right into the event handler, or you put it into a separate function, and call that function from the event handler - in that latter case, you simply pass the multiple values (x, y, event, flags) as function arguments to that function.


这篇关于如何在函数中返回多个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆