简体   繁体   中英

invalid operands of types ‘int’ and ‘int [3]’ to binary ‘operator*’

#include <iostream>
 
int main()
{
 int p[3];
 int q[3];
 int r[3];
 int x[3];
 int a=0;
 int b=0;

a=q-p;
b=r-p;
 
 float dx=0.02;
 float dy=0.02;
 
 for(int alpha=0;alpha<=2;alpha+=dx)
 {
     for (int beta=0;beta<=1;beta+=dy)
     {
         alpha=1-dx-dy;
         int gamma=1-alpha-beta;
         if(0<=alpha && alpha<=1 && 0<=beta && beta<=1 && 0<=alpha && alpha<=1);
         x=alpha*p+beta*q+gamma*r;
     }
     std::cout<<x;
 }
 
return 0;
}

Can anyone tell me what I'm doing wrong? This is the error

invalid operands of types 'int' and 'int [3]' to binary 'operator*'

Can anyone make it run and give me a working one?

a=qp; that's illegal. q is of type int[3] , so is p , as is: they are pointers to the first element of an array.
You need to dereference the pointers, or index into the array.

int main() {
  int array[5] = { 0, 1, 2, 4, 8, };
  int elem_0 = array[0]; // 0
  int elem_3 = array[3]; // 4
}

In general, you'll want to avoid C-Style arrays, and use std::array instead.
It works about the same…

#include <array>

int main() {
  std::array<int, 5> array = { 0, 1, 2, 4, 8, };
  int elem_3 = array[3]; // 4...
}

If you're going to do vector (not to be confused with std::vector which is a list… But not std::list , that's a doubly-linked list) operations, then I recommend std::valarray , which, of course, as the name implies, is a matrix.

You can find all the containers here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM