"Expected Primary-Expression before 'float'", calling a function from another class

Asked

Viewed 88 times

0

I need to create a Florentzproton function in the Physics class that takes two vectors (v and B) and multiply them using another Productovetorial function, of the Mathematics class, storing the product in vector F (and then multiply by another value - q). The classes are defined in . h and are with the quoted functions declared in . cpp below.

Physics.cc

float Physics::FLorentzProton(double v[3], double B[3])
{   
    double q = 1.602*(10^-19);
    double F[3] = float Mathematics::ProdutoVetorial(double v[3], double B[3]); // <----- ERRO CITADO

    for(int i=0; i<=2 ;i++) 
    {
        F[i] = q*F[i];
    };
    
    return F[3];
}

Mathematics.cc

float Mathematics::ProdutoVetorial(double B[3], double C[3])
{
    double A[3];
    
    A[0]= (B[1]*C[2] - C[1]*B[2]);
    A[1]= (B[2]*C[0] - C[2]*B[0]);
    A[2]= (B[0]*C[1] - C[0]*B[1]);
    
    double moduloA= sqrt(pow(A[0], 2) + pow(A[1], 2) + pow(A[2], 2));
    
    return A[3];
}
  • Take a look at how to reference an array and an element of this array. They are different things. If you declare double A[3]; the elements of this array will be A[0], A[1] and A[2], there is no element A[3], this addresses a memory area outside the limits of the array. Also note that you have not assigned anything to this A[3] and therefore have no sense of this return other than not doing anything with the calculation assigned to moduloA.

  • Another detail I’ve seen is in: double q = 1.602*(10^-19);. The operator ^ is the XOR (Bitwise exclusive OR) and not the exponentiation operator, maybe you want: double q = 1.602E-19L; or double q = 1.602*pow((10, -19);.

  • Could you explain to me how to reference this vector so I can manipulate it? Ah and thank you for the exponential warning, I had not noticed.

1 answer

0

1 - Explaining vector in computation is not the same thing as vector in mathematics

The first thing that is usually explained to the students of exactas, is that "vectors" in programming languages are not the same thing as vectors in Fis/mat, computation vector is a sequence of elements of a single 'type', (where the elements have the same size in bytes in memory), can be integers, floating point numbers, names, screen components, web pages, images, etc... but all elements of the vector must have the same size in bytes, and must also occupy sequential memory positions. In a case where double has (usually) 8 bytes, double [3] occupies 24 bytes in memory, if it represents some greatness is irrelevant pro compiler.

These two factors: Same individual storage size and sequential allow the compiler to work with vectors very quickly, but counterintuitive: In C/C++ when the compiler passes an array from one function to another it passes only the memory address where the first vector element is, Whether it’s 1,3, 1,000 or 1,000,000 elements. Thus, the passage is always very fast, because it does not create copies of the elements, that is, to pass a vector of 1,000,000 doubles, which would occupy approximately 8 Megabytes, it passes only 4 bytes (in 32-bit systems) with the position where the vector starts and nothing else. How so nothing else ? And the size of the vector ? Well, C/C++ do not pass the size to the called function.

And why do you see in books or in lecture notes something like double[3] used to store a three-dimensional (mathematical) vector? Simply because a very convenient way ( in terms of processing ) to store 3 numbers is to put them in sequence in memory.

When you pass a double[6] with values 10, 20, 30, 40, 50, 60 for a function and this function receives it as parameter v, and just below you write a command like v[5] compiler calculates how much is 8(double size) x 5(chosen position) = 40 and sum with the value of v (which is a memory position, for example 1080), as v has no "values" it only represents a memory position, the compiler jumps immediately to the memory position v[5] that is 1120 = 1080 + 8 * 5 and there in that memory position will be the value 60 for your calculation.

Therefore, that when you try to use v[100] in a vector that is not that big the computer displays U.B. "Undefined Behaviuor", anything can happen, to present messages of "Segmentation fault", "the program crashes", "blue screen", "the computer catches fire", or the Cramunhão comes out of your nose ( Nasal Demons ) or something like that.

2 - Calm that has solution:

Usually encapsulates the vectors in structures

struct Vec3D {
   array<double, 3> v;
   Vec3D operator *( double d ) const {  // Scalar product
       return { v[0] * d, v[1] * d, v[2] * d };
   }
   Vec3D operator +( const Vec3D& o ) const { 
       return { v[0] + o.v[0], v[1] + o.v[1], v[2] + o.v[2] };
   }

   Vec3D operator *( const Vec3D& o ) const {  // Cross Product
       return { 
        v[1] * o.v[2] - v[2] * o.v[1], 
        v[2] * o.v[0] - v[0] * o.v[2], 
        v[0] * o.v[1] - v[1] * o.v[0] 
       };
   }

   double mod() const {
      return hypot( v[0], v[1], v[2] );
   }

   friend ostream& operator<<(ostream& o, const Vec3D& vec) {
      return o << '[' << vec.v[0] << ',' << vec.v[1] << ',' << vec.v[2] << "]";
   }
};

Simple example : https://godbolt.org/z/MYsjbs

Example with generic programming C++ with vectors of many dimensions: https://godbolt.org/z/ocGrbo

Browser other questions tagged

You are not signed in. Login or sign up in order to post.