So I’m making a simple geometry program, and do a test compile.
For some reason, when i compile this code, i get the following error:
base.cc: In member function ‘void seg::init_seg(p, p)’:
base.cc:20:3: error: ‘mid’ was not declared in this scope
base.cc:22:3: error: ‘b’ was not declared in this scope
But interestingly, the error doesn’t appear for points 1 and 2, only mid and b.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct p{
float x=0.0f,y=0.0f;
void init_p(float sx, float sy){
x = sx;
y = sy;
}
};
struct seg{
p 1, 2, mid, b;
float length = 0.0f, m = 0.0f;
void init_seg(p p1, p p2){
1.init_p(p1.x, p1.y);
2.init_p(p2.x, p2.y);
length = sqrt((1.x - 2.x)^2 + (1.y - 2.y)^2);
mid.init_p((1.x + 2.x)/2, (1.y + 2.y)/2 );
m = ((1.y - 2.y)/(1.x - 2.x));
b.init_p(0, (1.y - (m*1.x)));
}
};
Why does this error appear, and why only for these two points?
Here is one group of errors:
Unlike Java and C#, you cannot do initialization like that in C++ prior to C++11. In your case it is also unnecessary: the only constructor that you have sets both
xandy, so the zeros that you are setting would be overwritten anyway.Here is another error:
You cannot use identifiers that do not start in a letter or an underscore. This should probably be