Reputation: 374
I have two structures one is basic, position_3d. The other one is ray.
typedef struct{
float x,y,z;
} position_3d;
typedef struct{
vector_3d direction;
position_3d startPosition;
} ray;
I have implemented a function which return a position_3d struct
position_3d getIntersectionPosition(ray r, sphere s){
position_3d pos;
//some code
pos.x = r.startPosition.x + t*r.direction.x;
pos.y = r.startPosition.y + t*r.direction.y;
pos.z = r.startPosition.z + t*r.direction.z;
return pos;
}
when I call
position_3d pos = getIntersectionPosition(r, s);
I got this error invalid initializer. I am using gcc. The compilation command is gcc prog.c -o prog.out -lm -lGL -lGLU -lglut
I am really stuck now! May anybody help?
Upvotes: 3
Views: 8051
Reputation: 374
The problem was because of improper order of functions. I mean I I need to define the signature of all functions before calling them.
Upvotes: 0
Reputation: 755006
There are a lot of gaps to fill in before we can see whether the code should be compilable:
sphere
?vector_3d
?t
?When we come up with plausible-looking hypotheses for these types, as in this code (file sphere.c
):
typedef struct
{
float x,y,z;
} position_3d;
typedef struct
{
float x, y, z;
} vector_3d;
typedef struct
{
vector_3d direction;
position_3d startPosition;
} ray;
typedef struct
{
float radius;
position_3d centre;
} sphere;
position_3d getIntersectionPosition(ray r, sphere s)
{
position_3d pos;
float t = s.radius;
pos.x = r.startPosition.x + t*r.direction.x;
pos.y = r.startPosition.y + t*r.direction.y;
pos.z = r.startPosition.z + t*r.direction.z;
return pos;
}
position_3d func(ray r, sphere s)
{
position_3d pos = getIntersectionPosition(r, s);
return pos;
}
Then I can compile using:
$ /usr/bin/gcc -O3 -g -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
-Wold-style-definition -c sphere.c
sphere.c:24: warning: no previous prototype for ‘getIntersectionPosition’
sphere.c:35: warning: no previous prototype for ‘func’
$
These warnings are correct; they are prompted by the -Wmissing-prototypes
options. Normally, there'd be a header declaring the types and the functions.
Thus, by demonstration, the code could be correct if written properly. In C, you cannot modify the code so that it reads:
ray r;
sphere s;
position_3d pos = getIntersectionPosition(r, s);
position_3d func(void)
{
return pos;
}
That then gives a warning:
sphere.c:43: error: initializer element is not constant
If you compile with g++
instead of gcc
, though (and remove the C-specific options, leaving):
$ g++ -O3 -g -Wall -Wextra -c sphere.c
$
Then it compiles cleanly — a demonstration, once more, that C and C++ are not the same languages, not that you were claiming this.
The error I get isn't the error you claim to be getting. That means I've not managed to guess accurately what you are doing in your code. So, please:
Upvotes: 1
Reputation: 72746
Is the line
position_3d pos = getIntersectionPosition(r, s);
at file scope (then that's an error because the initializer is not constant) or block scope (then it should work)?
Upvotes: 2