I'm in my second semester, and have been assigned this monster project. The sources are 13 separate files. For now i'll just post where i think the problem is, but can attach more as necessary.
case TRIANGLE:
if (event.LeftDown()) {
if (corners == 0) { // first click
Drawing::Triangle triangle;
triangle.setFirstPoint(x, y);
corners++;
Refresh();
}
} // first vertex
if (event.LeftDown()) {
if (corners > 0) { // second click
triangle.setSecondPoint(x, y);
corners++;
Refresh();
}
} // second vertex
if (event.LeftDown()) {
if (corners > 1) { // third click
triangle.setThirdPoint(x, y);
triangleVector.push_back(triangle);
corners++;
Refresh();
}
} // third vertex
break;
Explanation: The goal here is to draw a triangle after the user clicks three times. event.LeftDown() is provided by wxWidgets and goes true when the mouse left button is pressed. The Triangle class setFirstPoint(), etc. are meant to collect those clicks so that lines can be drawn between the appropriate points. However, what i currently have doesn't compile:
$ g++ `wx-config --cppflags` -o Project *.cpp `wx-config --libs`
DrawingWindow.cpp:143:5: error: use of undeclared identifier 'triangle'
triangle.setSecondPoint(x, y);
^
DrawingWindow.cpp:151:5: error: use of undeclared identifier 'triangle'
triangle.setThirdPoint(x, y);
^
DrawingWindow.cpp:152:30: error: use of undeclared identifier 'triangle'
triangleVector.push_back(triangle);
^
So how can i rearrange the code to collect the three clicks in the same scope? Note: i tried moving the Drawing::Triangle line out of the if block, just under the case statement but that causes another problem.
The issue is that you have a variable initialization inside your switch/case. However, switch/case statements are built using jump tables (usually) so you are not allowed to do this:
1 2 3 4 5 6 7 8 9 10 11 12
switch(arg) {
case 1:
//create and init x
int x = 2;
x++;
break;
case 2:
//we are still in the same scope, so x exists here!
x++;
//but this is invalid, because we could skip the creation of x
break;
}
To fix this, simply create a new scope inside your case area:
1 2 3 4 5 6 7 8 9 10 11
switch(arg) {
case 1:
{
int x = 2;
x++;
}
break;
case 2:
//x++; now x doesn't exist here, it is created and destroyed in the scope we created
break;
}
@firedraco: thanks for the in-depth explanation, but i don't see how it applies to my situation. The code i posted above is all one case. What separates line 5 [which works] from line 12 is that they are in different 'if' blocks. Does my object go out of scope at the end of the first if block?