我正在使用加速度计UIImageViews在屏幕周围移动一些,此时加速计仅在a NSTimer为0时工作,这很好.当发生不同的功能时,我还想让加速度计再次停止.
这是我目前的代码:
-(BOOL) accelerometerWorks {
return time == 0;
}
-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
if(![self accelerometerWorks]) return;
valueX = acceleration.x*25.5;
int newX = (int)(ball.center.x +valueX);
if (newX > 320-BALL_RADIUS)
newX = 320-BALL_RADIUS;
if (newX < 0+BALL_RADIUS)
newX = 0+BALL_RADIUS;
int XA = (int)(balloonbit1.center.x +valueX);
if (XA > 320-BALL_RADIUS)
XA = 320-BALL_RADIUS;
if (XA < 0+BALL_RADIUS)
XA = 0+BALL_RADIUS;
int XB = (int)(balloonbit2.center.x +valueX);
if (XB > 320-BALL_RADIUS)
XB = 320-BALL_RADIUS;
if (XB < 0+BALL_RADIUS)
XB …Run Code Online (Sandbox Code Playgroud) 我是C的新手,我正在研究其他人的代码,我正试图摆脱一个看起来像这样的警告:
warning: function expects to return value: myfunc
Run Code Online (Sandbox Code Playgroud)
myfunc是这样声明的,(我相信它默认为int)
myfunc(int id, int age) {
...
return;
}
Run Code Online (Sandbox Code Playgroud)
所以我尝试在myfunc后面放置void,使它看起来像这样
void myfunc(int num, int age) {
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
标识符已重新声明:myfunc
Run Code Online (Sandbox Code Playgroud)current : function() returning void previous: function() returning int : "students.c", line 233
但是当我去student.c的第233行时,这只是我实际上称之为函数的第一个地方.为什么会这样?
我知道我可以更改return为return 0;然后将myfunc定义为int.但是当调用这个函数时,它没有分配给任何东西,它只是执行myfunc(current_id, age);(int i = myfunc(...例如).
在这种情况下,最好不要使用void吗?可以return;在void函数中使用吗?
谢谢!
在我们的代码中,我们有一个混合类型的大结构,并希望为这些存储默认值的重复(const)结构.
当用户想要默认设置时,能够通过在结构中获取项的地址偏移量来实现这一点,然后在"defaults"结构中分配具有相同偏移量的值,这样会很好像这样:
void *setting = &settings->thing; // Points to a setting
int offset = setting - &settings;
void *default = &defaults_struct + offset; // Points to the default value
*setting = *default; // Set setting to default value
Run Code Online (Sandbox Code Playgroud)
想法是如果settings-> thing指向int8,则从默认值复制int8值,但如果settings-> other_thing是int32,则复制完整的32位.
问题是,如上所述,这是否适用于无效指针?如果没有,有没有办法做到这一点?我错过了更好的实现方法吗?
编辑澄清:我们希望在结构中将单个值设置为"defaults"结构中的相应值.
我试图通过引用将变量传递给void指针,以更新原始值.当我尝试它时,旧值永远不会更新.任何帮助将不胜感激.
gst_filter_chain就像主要功能(Gstreamer)
void update_value(void *oldValue, void *newValue)
{
oldValue = newValue;
}
void update_struct(myStruct *oldStruct, myStruct newStruct)
{
update_value((void *)&oldStruct->a, (void *)&newStruct.a)
}
static GstFlowReturn
gst_filter_chain (GstPad * pad, GstBuffer * buf)
{
GstFilter *filter= GST_FILTER (gst_pad_get_parent (pad));
myStruct temp_data;
int buf_size = GST_BUFFER_SIZE(buf);
if(buf_size > 1) //if buffer is not empty
{
if(!filter->is_init)
{
memcpy(&filter->data, GST_BUFFER_DATA(buf), sizeof(myStruct));
filter->is_init = TRUE;
}
else
{
memcpy(&temp_data, GST_BUFFER_DATA(buf), sizeof(myStruct));
update_struct(&filter->data, temp_data);
}
}
gst_buffer_unref(buf);
return GST_FLOW_OK;
}
Run Code Online (Sandbox Code Playgroud) 我正在使用ConcurrentDictionary,我需要使用删除元素TryRemove.
实现TryRemove需要一个out参数来返回将被删除的对象.
Body dummy;
if (!bulletBodies.TryRemove(bullet, out dummy))
{
}
...
Run Code Online (Sandbox Code Playgroud)
每当我删除字典中的条目时,我都不想在虚拟变量上使用额外的行:这就是为什么我试图绕过out返回参数不成功地键入下面的恐怖.我也google了一下,但没有成功.
bulletBodies.TryRemove(bullet, null);
bulletBodies.TryRemove(bullet, out null);
bulletBodies.TryRemove(bullet, void);
...
Run Code Online (Sandbox Code Playgroud)
有没有办法或聪明的提示来管理未使用的输出参数,所以没有必要在任何地方声明虚拟变量?
我正在研究voidC#中的类型定义.它是一个空结构,代表无价值.
但是,这种结构与其他结构之间究竟有什么区别.我的意思是如果你定义一个返回类型为void的方法,那么return typeOf(void)你将得到一个错误.
看来C#对这种类型有不同的行为.
有一些关于void真实情况的文件.我的意思是幕后发生了什么.值类型结构怎么能指的是什么?
更新:
我认为你们中的一些人没有明白这个问题,这就是为什么你把它标记为重复的原因.我的问题是.虚空是一种结构.好 ?..结构是价值类型.在可能的最低阶段会发生什么?什么空虚代表在记忆中?这个结构和其他结构之间有什么区别,如果你按空格键F12,你只看到一个空结构?哪个c#有这个区别?
这是我很久以来一直想知道的事情.
是否可以传递一个对象,但首先在同一行中调用该对象上的void方法?这很难解释,但我会举一个例子:
我正在使用Vector来自第三方API 的对象,它只保存3个坐标,我将它传递给一个组合setLocation(Vector)方法; 但首先我要将3添加到该Vector的Y值,这是由Vector#addY(3f);可以在同一行上执行此操作吗?
setLocation(new Vector(0f,4f,0f).addY(3));
Run Code Online (Sandbox Code Playgroud)
我认为这应该解释我的意思.
我知道以下,如果可能的话,将是一个绝对不好的做法,但我想知道这是否可行.
问题如下:是否有可能在C++中(并且在某种程度上编译器不会抛出任何警告),使用返回void的函数执行无用的算术运算.
std::vector<int> v;
int i = 42 + v.resize(42);
/* How to transform the last line to execute resize and to have i = 42 */
Run Code Online (Sandbox Code Playgroud)
我知道这是愚蠢的,但这不是问题......
我在书中看到了下面的代码.我知道void方法不能返回值.当我运行代码时,编译器无法打印修改后的数组,而在本书中,会显示值.如何修复代码以打印修改后的数组?
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
System.out.println("The values of original array are:");
for(int value: array)
{
System.out.print(value + "\t");
}
modifyArray(array);
System.out.println("The values after modified array are:");
for (int value:array)
{
System.out.print(value + "\t");
}
}
public static void modifyArray(int[] b)
{
for (int counter=0; counter<=b.length; counter++)
{
b[counter] *=2;
}
}
Run Code Online (Sandbox Code Playgroud) 我想知道是否有一个解决方案来指定一个模板,以便void可以传递任何一种或任何其他类型.下面是一个很简单的例子,它应该做什么,但回报void,如果TReturn是void一样好,甚至返回函数的返回值(返回TReturn过,但不同的,它实际上是一个类型,然后).
template <typename TReturn>
TReturn Foo(std::function<TReturn()> function)
{
if(std::is_same<TReturn, void>::value)
{
// I am not sure what to put here
return function();
}
TReturn returnValue = function();
// Do some stuff with the returnValue here
return returnValue;
}
Run Code Online (Sandbox Code Playgroud)
现在显然我所做的是错误的,因为函数不能返回两种不同的类型.这就是我要问的原因,有没有人知道这样做的方法?
任何帮助都很高兴