2

Quick overview of how I got to this.

  1. Created the structure
  2. Created the .cpp file
  3. Used CMake to create Make file
  4. Ran Make and received error

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?

Jack Kelly
  • 18,264
  • 2
  • 56
  • 81
E-rich
  • 9,243
  • 11
  • 48
  • 79
  • 1
    on `convertTo` you do not need the `;` on the closing `}`. You only need a `};` for the class `{}` –  Mar 03 '11 at 00:06
  • The code in the `try` block can throw no exceptions (you call no library functions, you do no dynamic allocation, and your code does not throw any exceptions). The `try` and `catch` blocks are wholly unnecessary. – James McNellis Mar 03 '11 at 05:13

6 Answers6

7
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).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Or for even more name fun, `class Meter Meter(1.0);`! – James McNellis Mar 03 '11 at 00:31
  • @James McNellis: If you keep using language like that, you're going to have to wash your mouth out with soap -- and worse, decipher `if if .eq. then then the = else else then = endif endif` for the next few years. Oh wait, you're writing C# now -- Fortran might be more fun. – Jerry Coffin Mar 03 '11 at 00:41
1

Couple of things that jumped out at me reading this sample

  1. The convertTo method does not return on all code paths
  2. The convertTo method has an ; after the closing }.
  3. The identifier Meter is listed twice: Enumeration value and class name.
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
1

As everyone has pointed out: Meter is both an enumeration and a class name.

A little trick that allows you to keep the same name:

class UnitType
{
    public:
       enum UnitType { Meter, Inch };
};


int main()
{
   // Meter enum is now inside the scope of UnitType

   UnitType::UnitType  type = UnitType::Meter;
   Meter               meter(1.0);

}
Martin York
  • 257,169
  • 86
  • 333
  • 562
0

Rename either your enum member "Meter" or class name "Meter"

Erik
  • 88,732
  • 13
  • 198
  • 189
0

Change "Meter" in the enum to something else because it conflicts with the class name "Meter".

Secondly, you need to add a return value for the function "convertTo" in the case that "unit != Inch".

Morten Kristensen
  • 7,412
  • 4
  • 32
  • 52
0

Change the enum type into METER and INCH because you even have the class name as Meter.

Raja
  • 442
  • 5
  • 17