Hi,

I would like to define a function, but I get errors... What's wrong with this?

#include <cstdlib> 
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>

#define N 5
using namespace std;

const float M_2PIf = 6.283185307179586476925286766559f;
const float M_PIf  = 3.141592653589793238462643383279f;
const float epsilon = M_PIf/(1e-06f);

/* -----------------RANDOM_GENERATOR------------------------------- */

float rand(float min, float max)
{
	return min + (max - min) * rand() / (float)RAND_MAX;
}

//*********************************
struct vector { // random vector before the rotation
float x,y,z;
};

struct rotvector { // random vector after the rotation
float x,y,z;
};
//*********************************

//  >>>>>>>>> ROTATION <<<<<<<<<<<

    float RotMatrixX[3][3] = {
                             {1.0f, 0.0f, 0.0f},
                             {0.0f, cosf(epsilon), -sinf(epsilon)},
                             {0.0f, sinf(epsilon), cosf(epsilon)}
                             };
    
    float RotMatrixY[3][3] = {
                             {cosf(epsilon), 0.0f, sinf(epsilon)},
                             {0.0f, 1.0f, 0.0f},
                             {-sinf(epsilon), 0.0f, cosf(epsilon)}
                             };
    
    float RotMatrixZ[3][3] = {
                             {cosf(epsilon), -sinf(epsilon), 0.0f},
                             {sinf(epsilon), cosf(epsilon), 0.0f},
                             {0.0f, 0.0f, 1.0f}
                             };

    vector vec[N];
    rotvector rvec[N];
    
    // ROTATE AROUND 'X'
    float RotateX(float RvX, float RotmatrixX[][3], vector vec[], rotvector rvec[]){
          
    for (int i = 0; i < 3; i++){
          for (int k = 0; k < 3; k++){
              
              RvX += RotMatrixX[i][k] * vec[k];
              rvec[i] = RvX;
          }
    }
    return RvX;
    }


// main() function
int main(void)
{
    srand ( time(NULL) );  // initialize random seed
    
    for (int i = 0; i < N; i++)
    {
        
        float phi   = rand(0.0f, M_2PIf);
        float costheta = rand(-1.0f, 1.0f);

        float x = 1.0f * sqrtf(1-costheta*costheta) * cosf(phi);
        float y = 1.0f * sqrtf(1-costheta*costheta) * sinf(phi);
        float z = 1.0f * costheta;

        // store the initial vector
        vec[i].x = x;
        vec[i].y = y;
        vec[i].z = z;
        // store the rotated vector
        rvec[i].x = x;
        rvec[i].y = y;
        rvec[i].z = z;
        
        // call rand to get a value 0, 1, or 2 and use a switch statement
        int n = rand() % 3;
        
        switch(n)
        {
            case 0:
                cout << "Case 0" << endl;
                break;
            case 1:
                cout << "Case 1" << endl;
                break;
            case 2:
                cout << "Case 2" << endl;
                break;
        }
    }
 
   system("pause");
// end main() function
    return 0; 
}

ERRORS:

simple.cpp: In function `float RotateX(float, float (*)[3], vector*, rotvector*)':
simple.cpp:60: error: no match for 'operator*' in 'RotMatrixX[i][k] * *((+(((unsigned int)k) * 12u)) + vec)'
simple.cpp:61: error: no match for 'operator=' in '*((+(((unsigned int)i) * 12u)) + rvec) = RvX'

You have a type mismatch:

RvX += RotMatrixX[i][k] * vec[k];
rvec[i] = RvX;

In the first line, vec[k] is a vector, not a float. There's no defined conversion from vector to float in your code, and you don't define a specific operator* to handle (float * vector). Most likely you wanted to use a member of the vector:

RvX += RotMatrixX[i][k] * vec[k].x;

In the second line, you try to assign a float to a vector, which once again, is not a defined conversion. Perhaps you meant to assign to a single member of the vector:

rvec[i].x = RvX;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.