开发者

Ambiguity while including std class libraries

I have Visual studio 2005 . One of my header file has a enum like

typedef enum { scalar, array, set } increment;

When 开发者_如何学GoI try to include the header file I get an ambiguity error for "set". I am using the std::set in this cpp file . The issue is the compiler is unable to differentiate between the std::set and the set in the enum .

Any suggestions to resolve this ambiguity without declaring any new namespace


Don't import the std namespace into the global namespace. The STL set is in the std namespace so if you don't have the line using namespace std in your headers you shouldn't get a conflict. Beyond that, refactor your enum.


If you've got using namespace std; in your file (or any headers it includes), that could be the problem.


try prefixing your enum constant with something like "e"

enum E_increment { eScalar, eArray, eSet }


The usual solution to this problem in C++ is to use more descriptive value names. For example, look at the Qt enum GestureType. Its values are named like TapGesture and PanGesture, rather than just "Tap" and "Pan". In your case I suggest:

enum increment { scalar_increment, array_increment, set_increment };

In the next revision of C++ there will be a proper solution in the form of strong enums.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜