使用方法作为左值和“赋值中的无效左值”编译错误 [英] using a method as an lvalue and "invalid lvalue in assignment"compilation error

查看:128
本文介绍了使用方法作为左值和“赋值中的无效左值”编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

这里是一个简化的代码示例,其中包含了我想要实现的内容。本质上,我想为一个返回值赋值。

方法的值是C.我知道,当然,在这个例子中我可以 >
通过newskb-> iph = iphdr得到这个(这也出现在下面示例中的注释

行中);但我想实现同样的地方

左边是:ip_hdr(newskb)。唉,如果我试试这个,我得到关于第25行的

a编译错误。

第25行是:

ip_hdr(newskb)= iphdr;

我得到的错误是:

lval.c:25:错误:赋值时左值无效

我使用gcc-4.1.2- 33c,我编译没有任何标志。


我试过施法,

喜欢:(struct iphdr *)ip_hdr(newskb)= iphdr;

或类似:

ip_hdr(newskb)=(struct iphdr *)iphdr;

并得到同样的错误。

任何想法?

问候,

马克

Hello,
Followed here is a simplified code example of something which I
try to implement; in essence , I want to assign a value to a return
value of a method is C. I know, of course, that in this example I
can
get this by newskb->iph = iphdr (this also appears in a commented
line in the example below) ; but I want to achieve the same where
the left side is : ip_hdr(newskb). Alas, if I try this , I get
a compilation error about line 25.
line 25 is:
ip_hdr(newskb)=iphdr;
the error I get is:
lval.c:25: error: invalid lvalue in assignment
I use gcc-4.1.2-33c, and I compile without any flag.

I tried casting,
like : (struct iphdr*)ip_hdr(newskb)=iphdr;
or like:
ip_hdr(newskb)=(struct iphdr*)iphdr;
and got the same error.
Any ideas?
Regards,
Mark

推荐答案

9月8日,19:25 ,markr ... @ gmail.com < markr ... @ gmail.comwrote:
On 8 Sep, 19:25, "markr...@gmail.com" <markr...@gmail.comwrote:

你好,

*这里是一个简化的代码示例,我

* * * *尝试实施;本质上,我想为一个返回值赋值

* * * *一个方法的值是C.我知道,当然,在这个例子中我是

可以

* * * *通过newskb-> iph = iphdr得到这个(这也在下面的例子中出现在

* * * *行中);但我想实现同样的目标

* * * *左侧是:ip_hdr(newskb)。唉,如果我试试这个,我会得到

* * * *关于第25行的编译错误。

* * * *第25行是:

* * * * * ip_hdr(newskb)= iphdr;

* * * *我得到的错误是:

* * * * lval.c:25:错误:赋值中的左值无效

* * * *我使用gcc-4.1.2-33c,我编译时没有任何标记。


* * * *我试过施法,

* * * *喜欢:(struct iphdr *)ip_hdr(newskb)= iphdr;

* * * *或者喜欢:

* * * * ip_hdr(newskb)=(struct iphdr *)iphdr;

* * * *并得到同样的错误。

* * * *有什么想法吗?

* * * *问候,

* * * *标记
Hello,
* Followed here is a simplified code example of something which I
* * * * try to implement; in essence , I want to assign a value to a return
* * * * value of a method is C. I know, of course, that in this example I
can
* * * * get this by newskb->iph = iphdr (this also appears in acommented
* * * * line in the example below) ; but I want to achieve the same where
* * * * the left side is : ip_hdr(newskb). Alas, if I try this , I get
* * * * a compilation error about line 25.
* * * * line 25 is:
* * * * * ip_hdr(newskb)=iphdr;
* * * * the error I get is:
* * * * lval.c:25: error: invalid lvalue in assignment
* * * * I use gcc-4.1.2-33c, and I compile without any flag.

* * * * I tried casting,
* * * * like : (struct iphdr*)ip_hdr(newskb)=iphdr;
* * * * or like:
* * * * ip_hdr(newskb)=(struct iphdr*)iphdr;
* * * * and got the same error.
* * * * Any ideas?
* * * * Regards,
* * * * Mark



你能解释一下你真的想在这里做什么?


如果ip_hdr是一个函数,它接受一个值并返回一个值

out,那么ip_hdr(newskb )将运行该函数并给出结果。对于

某些原因,你似乎想要一个不同的结果。你想要运行

ip_hdr函数吗?你打算怎么处理(a)它实际给出的

结果,以及(b)你想要给它的结果是什么?b $ b它给出了什么?


如果你想要一个不同的结果出来的功能,你可能需要改变功能并让它返回所需的结果

结果。例如,通过输入return iphdr;进去。或者,做一下这样的事情:


if(someflag)

{result = iphdr; }

else

{result = ip_hdr(newskb); }


希望有所帮助。

保罗。

Could you explain what you''re actually trying to do here?

If ip_hdr is a function that takes one value in and returns one value
out, then ip_hdr(newskb) will run the function and give a result. For
some reason, you seem to want a different result. Do you want the
ip_hdr function to run, or not? What are you going to do with (a) the
result it actually gives, and (b) the result that you appear to want
it to give?

If you want a different result to come out of the function, you
probably need to alter the function and get it to return the desired
result. Eg by putting "return iphdr;" into it. Alternatively, do
something like:

if (someflag)
{ result = iphdr; }
else
{ result = ip_hdr(newskb); }

Hope that helps.
Paul.


" ma ***** *@gmail.com" < ma ****** @ gmail.comwrites:
"ma******@gmail.com" <ma******@gmail.comwrites:

这里是一个简化的代码示例,其中包含了我b / b
的尝试实施;本质上,我想为一个返回值赋值。

方法的值是C.我知道,当然,在这个例子中我可以 >
通过newskb-> iph = iphdr得到这个(这也出现在下面示例中的注释

行中);但我想实现同样的地方

左边是:ip_hdr(newskb)。唉,如果我试试这个,我得到关于第25行的

a编译错误。

第25行是:

ip_hdr(newskb)= iphdr;

我得到的错误是:

lval.c:25:错误:赋值时左值无效

我使用gcc-4.1.2- 33c,我编译没有任何标志。


我试过施法,

喜欢:(struct iphdr *)ip_hdr(newskb)= iphdr;

或类似:

ip_hdr(newskb)=(struct iphdr *)iphdr;

并得到同样的错误。

有任何想法吗?
Followed here is a simplified code example of something which I
try to implement; in essence , I want to assign a value to a return
value of a method is C. I know, of course, that in this example I
can
get this by newskb->iph = iphdr (this also appears in a commented
line in the example below) ; but I want to achieve the same where
the left side is : ip_hdr(newskb). Alas, if I try this , I get
a compilation error about line 25.
line 25 is:
ip_hdr(newskb)=iphdr;
the error I get is:
lval.c:25: error: invalid lvalue in assignment
I use gcc-4.1.2-33c, and I compile without any flag.

I tried casting,
like : (struct iphdr*)ip_hdr(newskb)=iphdr;
or like:
ip_hdr(newskb)=(struct iphdr*)iphdr;
and got the same error.
Any ideas?



C没有方法。如果你真的想问一下方法,

你可能想要comp.lang.c ++(< OT> C ++标准引用它们

作为成员函数,但它通常将它们称为方法

无论如何< / OT>)。


函数调用的结果不是左值。例如,

尝试将2.0的平方根设置为1.5:

sqrt(2.0)= 1.5; / *非法* /


不能成功。


如果函数返回一个指针值,则取消引用该值

产生一个左值。例如:


#include< stdio.h>


int obj = 0;


int * func(void)

{

return& obj;

}


int main(void)

{

*(func())= 42;

printf(" obj =%d \ n",obj);

返回0;

}


(额外的括号是为了清晰;它们是'不严格

必要。)


你必须对

对象的生命周期进行常规处理结果指出;例如,返回指向

a本地对象的指针是坏的。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

诺基亚

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

C doesn''t have "methods". If you really want to ask about methods,
you probably want comp.lang.c++ (<OT>the C++ standard refers to them
as member functions, but it''s common to refer to them as methods
anyway</OT>).

The result of a function call is not an lvalue. For example, an
attempt to set the square root of 2.0 to 1.5:

sqrt(2.0) = 1.5; /* illegal */

cannot succeed.

If a function returns a pointer value, dereferencing that value does
yield an lvalue. For example:

#include <stdio.h>

int obj = 0;

int *func(void)
{
return &obj;
}

int main(void)
{
*(func()) = 42;
printf("obj = %d\n", obj);
return 0;
}

(The extra parentheses are for clarity; they''re not strictly
necessary.)

You''ll have to exercise the usual care about the lifetime of the
object to which the result points; for example, returning a pointer to
a local object is bad.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


ma ****** @ gmail.com 写道:

您好,

这里是一个简化的代码示例,其中包含了我试图实现的内容。本质上,我想为一个返回值赋值

方法的值是C.
Hello,
Followed here is a simplified code example of something which I
try to implement; in essence , I want to assign a value to a return
value of a method is C.



在C ++中可以使用函数调用返回的值为

赋值运算符的左操作数;这是不可能的

C.


那么,你用C语言还是用C ++编程?您使用术语方法

会让我觉得您使用的是C ++ - 这两种语言实际上都没有使用

术语方法,但它在常用来描述什么是C ++调用

成员函数; C中不存在的东西。


C和C ++是两种截然不同的语言。如果您的问题实际上是关于C ++的,那么这是错误的新闻组。你应该去comp.lang.c ++
。那里有更多的

人有能力回答你的问题。

It is possible in C++ to use the value returned by a function call as
the left operand of an assignment operator; this is not possible in
C.

So, are you programming in C or in C++? Your use of the term "method"
makes me think you''re using C++ - neither language actually uses the
term "method", but it is in common use to describe what C++ call
member functions; something that doesn''t exist in C.

C and and C++ are two significantly different languages. If your
question is actually about C++, then this is the wrong newsgroup. You
should go to comp.lang.c++. There will be a much larger number of
people there who are competent to answer your question.


...我当然知道,在这个例子中我

可以通过newskb-> iph = iphdr得到这个(这也出现在评论

行中以下示例);但我想实现同样的地方

左边是:ip_hdr(newskb)。唉,如果我试试这个,我得到关于第25行的

a编译错误。

第25行是:

ip_hdr(newskb)= iphdr;

我得到的错误是:

lval.c:25:错误:赋值时左值无效

我使用gcc-4.1.2- 33c,我编译没有任何标志。


我试过施法,

喜欢:(struct iphdr *)ip_hdr(newskb)= iphdr;

或类似:

ip_hdr(newskb)=(struct iphdr *)iphdr;

并得到同样的错误。

有任何想法吗?
... I know, of course, that in this example I
can
get this by newskb->iph = iphdr (this also appears in a commented
line in the example below) ; but I want to achieve the same where
the left side is : ip_hdr(newskb). Alas, if I try this , I get
a compilation error about line 25.
line 25 is:
ip_hdr(newskb)=iphdr;
the error I get is:
lval.c:25: error: invalid lvalue in assignment
I use gcc-4.1.2-33c, and I compile without any flag.

I tried casting,
like : (struct iphdr*)ip_hdr(newskb)=iphdr;
or like:
ip_hdr(newskb)=(struct iphdr*)iphdr;
and got the same error.
Any ideas?



是的 - 我知道你没有回答你的问题所需的那种

信息的概念。你应该提供以下信息




ip_hdr()的定义是什么?它与iphdr有什么关系?什么

是newskb?你究竟想做什么?


如果你是用C ++编写的,那么某个地方存在一个struct iphdr

声明在你的代码中使iphdr类型名称。在你提供的所有

三个陈述中,''iphdr'的最终用法是在

上下文中,其中不允许使用类型名称。这让人很不清楚

你究竟想要做什么。如果你用C语言写作,

iphdr不一定是类型名称 - 你可以用相同的名字定义一个

变量,但它'实际上并不是一个好主意

所以。


我强烈建议你创建一个完整的程序,小到
$ b $可能,这实际上证明了你想要b
。编译它,然后发布程序的COMPLETE TEXT,并且

编译器错误消息的COMPLETE TEXT。请确保将

发布到相应的新闻组。

Yes - I''ve got the idea that you have no concept of the kind of
information that is needed to answer your question. You should provide
the following information:

What is the definition of ip_hdr()? How is it related to iphdr? What
is newskb? And what in the world are you actually trying to do?

If you are writing in C++, the existence of a "struct iphdr"
declaration somewhere in your code makes "iphdr" a type name. In all
three statements you''ve provided, the final use of ''iphdr'' is in a
context where a type name is not allowed. This makes it very unclear
what it is that you''re actually trying to do. If you''re writing in C,
iphdr is not necessarily a type name - you''re allowed to define a
variable with the same name, but it''s not a good idea to actually do
so.

I would strongly recommend creating a complete program, as small as
possible, that actually demonstrates what it is that you''re trying to
do. Compile it, and then post the COMPLETE TEXT of the program, and
the COMPLETE TEXT of the compiler''s error messages. Please make sure
that you post it to the appropriate newsgroup.


这篇关于使用方法作为左值和“赋值中的无效左值”编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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