Quick overview of how I got to this.
I'm trying to compile the following code:
#include <iostream>
using namespace std;
enum UnitType { Meter, Inch };
class Meter {
double value;
public:
Meter(double value) : value(value) {}
double convertTo(UnitType unit) {
if (unit == Inch) {
return value * 39.3700787;
}
};
};
int main (int argc, char *argv[])
{
try
{
Meter meter(1.0);
}
catch (int e) {
cout << "exception " << e << endl;
}
return 0;
}
but, I'm receiving the following error:
$ make [100%] Building CXX object CMakeFiles/convert-length.dir/convert-length.cpp.o /convert/length/convert-length.cpp: In function ‘int main(int, char**)’: /convert/length/convert-length.cpp:27: error: expected ‘;’ before ‘meter’ make[2]: *** [CMakeFiles/convert-length.dir/convert-length.cpp.o] Error 1 make[1]: *** [CMakeFiles/convert-length.dir/all] Error 2 make: *** [all] Error 2
I'm hoping this is a silly C++ syntax error somewhere that I'm missing, but I've spent a couple hours looking for it with no success. I have little C++ experience, but this code looks syntactically correct. Does anyone see or know what is wrong?
enum UnitType { Meter, Inch };
Here you've defined Meter as a enumeration value.
class Meter {
...but here (at the same scope) you're trying to re-define it as the name of a class. That's legal, but to make use of it later, you have to use class Meter, instead of just Meter:
class Meter meter(1.0);
IMO, even though you can use the same name for both, it's likely to lead to confusion and problems that are much better avoided by simply renaming one or the other (or maybe both).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With