无效的操作数错误 [英] invalid operand error

查看:209
本文介绍了无效的操作数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很确定这是算术中的一个简单错误,但我无法绕过它......我不断得到这个错误:无效操作数到二进制+(有int *和int *)。



  void  sort_ver2( int  *指针, int  size)
{
int * i,* j;
int temp;
for (i =指针; i<(指针+大小); ++ i){
for (j =指针+ 1 ; j<(指针+大小); ++ j){
if ((pointer + i)<(pointer + j)){
temp =(pointer + j);
(指针+ j)=(指针+ i);
(指针+ i)= temp;

}
}
}


}





提前致谢!

解决方案

仔细看看:

  for (i =指针; i < ;(指针+大小); ++ i){
^

这应该在这里吗?或者你的意思是=?







抱歉:)更新了帖子,我的意思是<冒号不应该在那里。



看看新版本,我是不完全确定你要做什么,但我能理解为什么编译器在抱怨:

  int  * i,* j; 
int temp;
...
temp =(指针+ j);

指针 j 都是指针到整数的变量 - 和你不能添加两个指针,因为没有什么明智的你得到!这有点像尝试添加东方和左 - 它不会给你带来任何有用的结果。



因为这是一个排序函数,我怀疑你最好用 i j 写作 int 而不是 int * 并使用它们添加到基础指针值。

并且还使用*(指针+ i)来访问值可能会有所帮助! :笑:


我相信的问题似乎源于你对指针的理解和使用。

你的代码指针+ i 真的没有任何意义。我将尝试用简单的图表来说明。



想象一下,你有一个包含一些数据的数组。我们假设*指针保存第一个元素的地址。我们还假设内存中的任意位置为0x4003000。



   -    --------------------  
| a | b | c | d | \0 |
- --------------------





所以,

'a'是0x4003000,

'b'位于0x4003001,

'c'位于0x4003002,

'd'位于0x4003003



NULL终止符位于0x4003004



所以,现在我们已经有了这个,让我们从你的第一个for循环中包含你的第3行 - 假设大小为4.



  for (i =指针; i < (指针+大小); ++ i){
for (j =指针+ 1 ; j < (指针+大小); ++ j){
if ((指针+ i)< (指针+ j)){





这将变为



< pre lang =c ++> for (i = 0x4003000; I< 0x4003000 + 4; ++ i)
{
for (j = 0x4003001; j< 0x4003000 + 4; ++ j)
{
if ((0x4003000 + 0x4003000)<(0x4003000 + 0x4003001))





开始看到问题了吗?!



解决方案是,我认为:(你的意图不是1000%清楚)

  void  sort_ver2( int  *指针, int  size)
{
int * i,* j;
int temp;
for (i =指针; i<(指针+大小); ++ i)
{
for (j =指针+ 1 ; j<(指针+大小); ++ j)
{
if (* i< * j)
{
temp = * j;
* j = * i;
* i = temp;
}
}
}
}





我假设你想要比较i和j指向的_values_,如果满足条件则交换它们?

一种真正可怕的(但是完全有效和合法的)使用指针的方式,在我的拙见中。 :)


原因是C ++不允许添加指针。



你对增加指针有什么期望?指针是内存地址。添加2个指针将指向(可能)整个不同位置的另一个内存地址。由于这是没用的,C ++禁止指针添加。



我认为你对将指针添加到指针感到困惑。以下代码在C ++中有效,并且有意义。

  int  arr [ 5 ] = { 1  2  3  4  5 }; 
int * p =& arr;
int * p_third =(p + 3 );

< br $>


arr 是一个包含5个整数的数组。现在, p 指向数组的第一个元素( arr [0] )。并且 p_third 指针被赋值为(p + 3)。什么是(p + 3)



在C ++中,上面代码的最后一行也可以写得像这样:

  int  * p_third =(p +  3  *  sizeof  int )); 





所以,(p + 3)实际上是(p + 3 * sizeof(int ))



最后, p_third 指向 arr [3]





提示:你应该像Java人一样停止编码。像这样的代码(它更具可读性和全面性):

  if ()
{
if ()
{
if ()
{
}
}
else
{
}
}




I'm pretty sure it's a simple error in the arithmetic but I just cant wrap my head around it..I keep getting this error: invalid operand to binary +(have int* and int*).

void sort_ver2(int *pointer, int size)
{
    int *i, *j;
    int temp;
    for(i = pointer; i < (pointer+size); ++i){
        for(j = pointer + 1; j < (pointer+size); ++j){
            if((pointer + i) < (pointer + j)){
                temp=(pointer + j);
                (pointer + j)=(pointer + i);
                (pointer + i)=temp;

            }
        }
    }


}



Thanks in advance!

解决方案

Have a close look:

for(i = pointer; i <; (pointer+size); ++i){
                    ^

Should this be here? Or did you mean "="?



"Sorry :) updated the post, what i meant was simply "<" the colon isnt't supposed to be there."

Looking at the new version, I'm not exactly sure what you are trying to do, but I can understand why the compiler is complaining:

int *i, *j;
int temp;
...
temp=(pointer + j);

Both pointer and j are pointer-to-integer variables - and you can't add two pointers, because there is nothing sensible you get! It's a bit like trying to add "East" and "Left" - it doesn't leave you anything useful as a result.

Since this is a sort function, I would suspect you would be better off writing it with i and j as int rather than int* and using them to add to your base pointer value.
And also using *(pointer+i) to access the values would probably help! :laugh:


The problem I believe, appears to stem from your understanding and use of pointers.
Your code pointer + i really doesn't make any sense. I'll try to illustrate with a simple diagram.

Imagine you had an array with some data in it. We'll assume that *pointer holds the address of the first element. We'll also assume an arbitrary location in memory of 0x4003000.

----------------------
| a | b | c | d | \0 |
----------------------



So,
'a' is at 0x4003000,
'b' is at 0x4003001,
'c' is at 0x4003002,
'd' is at 0x4003003
and the
NULL terminator is at 0x4003004

So, now we have that in place, lets run through the 3 lines from and including your first for loop - assuming size to be 4.

for(i = pointer; i < (pointer+size); ++i){
        for(j = pointer + 1; j < (pointer+size); ++j){
            if((pointer + i) < (pointer + j)){



This will turn into

for (i=0x4003000; i<0x4003000+4; ++i)
{
   for (j=0x4003001; j<0x4003000+4; ++j)
   {
       if ((0x4003000+0x4003000) < (0x4003000+0x4003001))



Starting to see the problem yet?!

The solution is, I think: (your intention isn't 1000% clear to me)

void sort_ver2(int *pointer, int size)
{
    int *i, *j;
    int temp;
    for(i = pointer; i < (pointer+size); ++i)
    {
        for(j = pointer + 1; j < (pointer+size); ++j)
        {
            if( *i < *j)
            {
                temp = *j;
                *j = *i;
                *i = temp;
            }
        }
    }
}



I assume you'd like to compare the _values_ that i and j point to, swapping them if a condition is met?
A truly horrid (but perfectly valid & legal) way to use pointers, in my humble opinion. :)


The reason is C++ does not allow pointer addition.

What do you expect by adding pointers ? A pointer is a memory address. Adding 2 pointers will point to (maybe) another memory address in an entire different location. Since this is no use, C++ prohibits pointer addition.

I think you are confused about adding integers to pointers. The following code is valid in C++, and makes sense.

int arr[5] = {1, 2, 3, 4, 5};
int* p = &arr;
int* p_third = (p + 3);



arr is an array of 5 integers. Now, p points to the array's first element(arr[0]). And p_third pointer is assigned to a value of (p + 3). So what's (p+3) ?

In C++, the last line of the above code can be also written like this:

int* p_third = (p + 3 * sizeof(int));



So, (p + 3) is really (p + 3 * sizeof(int)).

At last, p_third points to arr[3].


Tip: You should stop coding like Java people do. Code like this (It's more readable and comprehensive) :

if()
{
   if()
   {
      if()
      {
      }
   }
   else
   {
   }
}



这篇关于无效的操作数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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