smallB
smallB

Reputation: 17148

Something wrong with enum

I've something like:

enum Direction{Forward,Backward};

template<Direction dir = Forward>
class X
{
private:

    Direction my_direction_;
public:

    void set_direction(Direction dir)//here I'm getting an error
    {
        my_direction_ = dir;
    }

};

error: declaration of 'Direction dir'
Any reason why? BTW, it does compile with VS2010.

Upvotes: 3

Views: 113

Answers (3)

Behnam Safari
Behnam Safari

Reputation: 3081

Because you defined dir before in template line

change its name to _dir in constructor

Upvotes: 2

Meysam
Meysam

Reputation: 18177

Change it to this:

 void set_direction(Direction _dir = dir)
 {
      my_direction_ = _dir;
 }

The method parameter should not have the same name as the template parameter name.

Upvotes: 2

Alok Save
Alok Save

Reputation: 206636

Change:

template<Direction dir = Forward>

to

template<Direction direction = Forward>

The error on gcc is more descriptive:

prog.cpp: In member function ‘void X<dir>::set_direction(Direction)’:
prog.cpp:11: error: declaration of ‘Direction dir’
prog.cpp:3: error:  shadows template parm ‘Direction dir’

Upvotes: 8

Related Questions